From cf21e81016e8da2d4ea72871b18affc71ea54869 Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Sat, 25 Sep 2021 21:09:50 +0800 Subject: [PATCH] style: move flag parse to internal/base --- coolq/event.go | 5 -- global/config/config.go | 24 +++++---- internal/base/flag.go | 54 +++++++++++++++++--- main.go | 109 ++++++++++++++++------------------------ server/http.go | 3 -- server/pprof.go | 3 -- server/websocket.go | 6 --- 7 files changed, 102 insertions(+), 102 deletions(-) diff --git a/coolq/event.go b/coolq/event.go index 3cbc746..f00567b 100644 --- a/coolq/event.go +++ b/coolq/event.go @@ -17,11 +17,6 @@ import ( log "github.com/sirupsen/logrus" ) -// SetMessageFormat 设置消息上报格式,默认为string -func SetMessageFormat(f string) { - base.PostFormat = f -} - // ToFormattedMessage 将给定[]message.IMessageElement转换为通过coolq.SetMessageFormat所定义的消息上报格式 func ToFormattedMessage(e []message.IMessageElement, groupID int64, isRaw ...bool) (r interface{}) { if base.PostFormat == "string" { diff --git a/global/config/config.go b/global/config/config.go index c5defa6..85c93bb 100644 --- a/global/config/config.go +++ b/global/config/config.go @@ -26,20 +26,22 @@ var currentPath = getCurrentPath() // DefaultConfigFile 默认配置文件路径 var DefaultConfigFile = path.Join(currentPath, "config.yml") +type Reconnect struct { + Disabled bool `yaml:"disabled"` + Delay uint `yaml:"delay"` + MaxTimes uint `yaml:"max-times"` + Interval int `yaml:"interval"` +} + // Config 总配置文件 type Config struct { Account struct { - Uin int64 `yaml:"uin"` - Password string `yaml:"password"` - Encrypt bool `yaml:"encrypt"` - Status int32 `yaml:"status"` - ReLogin struct { - Disabled bool `yaml:"disabled"` - Delay uint `yaml:"delay"` - MaxTimes uint `yaml:"max-times"` - Interval int `yaml:"interval"` - } - UseSSOAddress bool `yaml:"use-sso-address"` + Uin int64 `yaml:"uin"` + Password string `yaml:"password"` + Encrypt bool `yaml:"encrypt"` + Status int `yaml:"status"` + ReLogin *Reconnect `yaml:"relogin"` + UseSSOAddress bool `yaml:"use-sso-address"` } `yaml:"account"` Heartbeat struct { diff --git a/internal/base/flag.go b/internal/base/flag.go index ad83a92..6150776 100644 --- a/internal/base/flag.go +++ b/internal/base/flag.go @@ -2,12 +2,23 @@ package base import ( + "flag" + "time" + log "github.com/sirupsen/logrus" "github.com/Mrs4s/go-cqhttp/global/config" ) -// flags +// command flags +var ( + LittleC string // config file + LittleD bool // daemon + LittleH bool // help + LittleWD string // working directory +) + +// config file flags var ( Debug bool // 是否开启 debug 模式 RemoveReplyAt bool // 是否删除reply后的at @@ -16,32 +27,59 @@ var ( SplitURL bool // 是否分割URL ForceFragmented bool // 是否启用强制分片 SkipMimeScan bool // 是否跳过Mime扫描 + UseSSOAddress bool // 是否使用服务器下发的新地址进行重连 + LogForceNew bool // 是否在每次启动时强制创建全新的文件储存日志 + FastStart bool // 是否为快速启动 - PostFormat = "string" // 上报格式 string or array - Proxy string // 存储 proxy_rewrite,用于设置代理 - PasswordHash [16]byte // 存储QQ密码哈希供登录使用 - AccountToken []byte // 存储AccountToken供登录使用 + PostFormat string // 上报格式 string or array + Proxy string // 存储 proxy_rewrite,用于设置代理 + PasswordHash [16]byte // 存储QQ密码哈希供登录使用 + AccountToken []byte // 存储 AccountToken 供登录使用 + Reconnect *config.Reconnect // 重连配置 + LogAging = time.Hour * 24 * 365 // 日志时效 ) -// Parse parses flags from config file +// Parse parse flags func Parse() { + flag.StringVar(&LittleC, "c", config.DefaultConfigFile, "configuration filename") + flag.BoolVar(&LittleD, "d", false, "running as a daemon") + flag.BoolVar(&LittleH, "h", false, "this help") + flag.StringVar(&LittleWD, "w", "", "cover the working directory") + d := flag.Bool("D", false, "debug mode") + flag.Parse() + + config.DefaultConfigFile = LittleC // cover config file + if *d { + Debug = true + } +} + +// Init read config from yml file +func Init() { conf := config.Get() { // bool config - Debug = conf.Output.Debug + if conf.Output.Debug { + Debug = true + } IgnoreInvalidCQCode = conf.Message.IgnoreInvalidCQCode SplitURL = conf.Message.FixURL RemoveReplyAt = conf.Message.RemoveReplyAt ExtraReplyData = conf.Message.ExtraReplyData ForceFragmented = conf.Message.ForceFragment SkipMimeScan = conf.Message.SkipMimeScan + UseSSOAddress = conf.Account.UseSSOAddress } - { // string + { // others Proxy = conf.Message.ProxyRewrite + Reconnect = conf.Account.ReLogin if conf.Message.PostFormat != "string" && conf.Message.PostFormat != "array" { log.Warnf("post-format 配置错误, 将自动使用 string") PostFormat = "string" } else { PostFormat = conf.Message.PostFormat } + if conf.Output.LogAging > 0 { + LogAging = time.Hour * 24 * time.Duration(conf.Output.LogAging) + } } } diff --git a/main.go b/main.go index d2205f6..d0f0cba 100644 --- a/main.go +++ b/main.go @@ -35,68 +35,42 @@ import ( "github.com/Mrs4s/go-cqhttp/server" ) -var ( - conf *config.Config - isFastStart = false - - // 允许通过配置文件设置的状态列表 - allowStatus = [...]client.UserOnlineStatus{ - client.StatusOnline, client.StatusAway, client.StatusInvisible, client.StatusBusy, - client.StatusListening, client.StatusConstellation, client.StatusWeather, client.StatusMeetSpring, - client.StatusTimi, client.StatusEatChicken, client.StatusLoving, client.StatusWangWang, client.StatusCookedRice, - client.StatusStudy, client.StatusStayUp, client.StatusPlayBall, client.StatusSignal, client.StatusStudyOnline, - client.StatusGaming, client.StatusVacationing, client.StatusWatchingTV, client.StatusFitness, - } -) +// 允许通过配置文件设置的状态列表 +var allowStatus = [...]client.UserOnlineStatus{ + client.StatusOnline, client.StatusAway, client.StatusInvisible, client.StatusBusy, + client.StatusListening, client.StatusConstellation, client.StatusWeather, client.StatusMeetSpring, + client.StatusTimi, client.StatusEatChicken, client.StatusLoving, client.StatusWangWang, client.StatusCookedRice, + client.StatusStudy, client.StatusStayUp, client.StatusPlayBall, client.StatusSignal, client.StatusStudyOnline, + client.StatusGaming, client.StatusVacationing, client.StatusWatchingTV, client.StatusFitness, +} func main() { - c := flag.String("c", config.DefaultConfigFile, "configuration filename") - d := flag.Bool("d", false, "running as a daemon") - h := flag.Bool("h", false, "this help") - wd := flag.String("w", "", "cover the working directory") - debug := flag.Bool("D", false, "debug mode") - flag.Parse() - // todo: maybe move flag to internal/base? - - switch { - case *h: - help() - case *d: - server.Daemon() - case *wd != "": - resetWorkDir(*wd) - } - - // 通过-c 参数替换 配置文件路径 - config.DefaultConfigFile = *c - conf = config.Get() - if *debug { - conf.Output.Debug = true - } base.Parse() - if base.Debug { - log.SetReportCaller(true) + switch { + case base.LittleH: + help() + case base.LittleD: + server.Daemon() + case base.LittleWD != "": + resetWorkDir() } + base.Init() + + // todo: do all config parse in internal/base? + conf := config.Get() rotateOptions := []rotatelogs.Option{ rotatelogs.WithRotationTime(time.Hour * 24), } - - if conf.Output.LogAging > 0 { - rotateOptions = append(rotateOptions, rotatelogs.WithMaxAge(time.Hour*24*time.Duration(conf.Output.LogAging))) - } else { - rotateOptions = append(rotateOptions, rotatelogs.WithMaxAge(time.Hour*24*365*10)) - } - if conf.Output.LogForceNew { + rotateOptions = append(rotateOptions, rotatelogs.WithMaxAge(base.LogAging)) + if base.LogForceNew { rotateOptions = append(rotateOptions, rotatelogs.ForceNewFile()) } - w, err := rotatelogs.New(path.Join("logs", "%Y-%m-%d.log"), rotateOptions...) if err != nil { log.Errorf("rotatelogs init err: %v", err) panic(err) } - log.AddHook(global.NewLocalHook(w, global.LogFormat{}, global.GetLogLevel(conf.Output.LogLevel)...)) mkCacheDir := func(path string, _type string) { @@ -129,11 +103,12 @@ func main() { para.Hide(p) } case "faststart": - isFastStart = true + base.FastStart = true } } } - if terminal.RunningByDoubleClick() && !isFastStart { + + if !base.FastStart && terminal.RunningByDoubleClick() { err := terminal.NoMoreDoubleClick() if err != nil { log.Errorf("遇到错误: %v", err) @@ -144,7 +119,7 @@ func main() { if (conf.Account.Uin == 0 || (conf.Account.Password == "" && !conf.Account.Encrypt)) && !global.PathExists("session.token") { log.Warn("账号密码未配置, 将使用二维码登录.") - if !isFastStart { + if !base.FastStart { log.Warn("将在 5秒 后继续.") time.Sleep(time.Second * 5) } @@ -153,6 +128,7 @@ func main() { log.Info("当前版本:", base.Version) if base.Debug { log.SetLevel(log.DebugLevel) + log.SetReportCaller(true) log.Warnf("已开启Debug模式.") log.Debugf("开发交流群: 192548878") } @@ -221,7 +197,7 @@ func main() { } else if len(conf.Account.Password) > 0 { base.PasswordHash = md5.Sum([]byte(conf.Account.Password)) } - if !isFastStart { + if !base.FastStart { log.Info("Bot将在5秒后登录并开始信息处理, 按 Ctrl+C 取消.") time.Sleep(time.Second * 5) } @@ -304,19 +280,19 @@ func main() { return } log.Warnf("Bot已离线: %v", e.Message) - time.Sleep(time.Second * time.Duration(conf.Account.ReLogin.Delay)) + time.Sleep(time.Second * time.Duration(base.Reconnect.Delay)) for { - if conf.Account.ReLogin.Disabled { + if base.Reconnect.Disabled { log.Warnf("未启用自动重连, 将退出.") os.Exit(1) } - if times > conf.Account.ReLogin.MaxTimes && conf.Account.ReLogin.MaxTimes != 0 { + if times > base.Reconnect.MaxTimes && base.Reconnect.MaxTimes != 0 { log.Fatalf("Bot重连次数超过限制, 停止") } times++ - if conf.Account.ReLogin.Interval > 0 { - log.Warnf("将在 %v 秒后尝试重连. 重连次数:%v/%v", conf.Account.ReLogin.Interval, times, conf.Account.ReLogin.MaxTimes) - time.Sleep(time.Second * time.Duration(conf.Account.ReLogin.Interval)) + if base.Reconnect.Interval > 0 { + log.Warnf("将在 %v 秒后尝试重连. 重连次数:%v/%v", base.Reconnect.Interval, times, base.Reconnect.MaxTimes) + time.Sleep(time.Second * time.Duration(base.Reconnect.Interval)) } else { time.Sleep(time.Second) } @@ -353,17 +329,17 @@ func main() { log.Infof("开始加载群列表...") global.Check(cli.ReloadGroupList(), true) log.Infof("共加载 %v 个群.", len(cli.GroupList)) - if conf.Account.Status >= int32(len(allowStatus)) || conf.Account.Status < 0 { + if uint(conf.Account.Status) >= uint(len(allowStatus)) { conf.Account.Status = 0 } - cli.SetOnlineStatus(allowStatus[int(conf.Account.Status)]) + cli.SetOnlineStatus(allowStatus[conf.Account.Status]) bot := coolq.NewQQBot(cli, conf) for _, m := range conf.Servers { if h, ok := m["http"]; ok { hc := new(config.HTTPServer) if err := h.Decode(hc); err != nil { log.Warn("读取http配置失败 :", err) - } else { + } else if !hc.Disabled { go server.RunHTTPServerAndClients(bot, hc) } } @@ -371,7 +347,7 @@ func main() { sc := new(config.WebsocketServer) if err := s.Decode(sc); err != nil { log.Warn("读取正向Websocket配置失败 :", err) - } else { + } else if !sc.Disabled { go server.RunWebSocketServer(bot, sc) } } @@ -379,7 +355,7 @@ func main() { rc := new(config.WebsocketReverse) if err := c.Decode(rc); err != nil { log.Warn("读取反向Websocket配置失败 :", err) - } else { + } else if !rc.Disabled { go server.RunWebSocketClient(bot, rc) } } @@ -387,7 +363,7 @@ func main() { pc := new(config.PprofServer) if err := p.Decode(pc); err != nil { log.Warn("读取pprof配置失败 :", err) - } else { + } else if !pc.Disabled { go server.RunPprofServer(pc) } } @@ -395,7 +371,7 @@ func main() { lc := new(config.LambdaServer) if err := p.Decode(lc); err != nil { log.Warn("读取pprof配置失败 :", err) - } else { + } else if !lc.Disabled { go server.RunLambdaClient(bot, lc) } } @@ -455,7 +431,8 @@ Options: os.Exit(0) } -func resetWorkDir(wd string) { +func resetWorkDir() { + wd := base.LittleWD args := make([]string, 0, len(os.Args)) for i := 1; i < len(os.Args); i++ { if os.Args[i] == "-w" { @@ -480,7 +457,7 @@ func resetWorkDir(wd string) { func newClient() *client.QQClient { c := client.NewClientEmpty() c.OnServerUpdated(func(bot *client.QQClient, e *client.ServerUpdatedEvent) bool { - if !conf.Account.UseSSOAddress { + if !base.UseSSOAddress { log.Infof("收到服务器地址更新通知, 根据配置文件已忽略.") return false } diff --git a/server/http.go b/server/http.go index 8caf1e9..e7e1c3d 100644 --- a/server/http.go +++ b/server/http.go @@ -151,9 +151,6 @@ func checkAuth(req *http.Request, token string) int { // RunHTTPServerAndClients 启动HTTP服务器与HTTP上报客户端 func RunHTTPServerAndClients(bot *coolq.CQBot, conf *config.HTTPServer) { - if conf.Disabled { - return - } var ( s = new(httpServer) addr string diff --git a/server/pprof.go b/server/pprof.go index 0b6b95a..d6202c6 100644 --- a/server/pprof.go +++ b/server/pprof.go @@ -14,9 +14,6 @@ import ( // RunPprofServer 启动 pprof 性能分析服务器 func RunPprofServer(conf *config.PprofServer) { - if conf.Disabled { - return - } addr := fmt.Sprintf("%s:%d", conf.Host, conf.Port) mux := http.NewServeMux() mux.HandleFunc("/debug/pprof/", pprof.Index) diff --git a/server/websocket.go b/server/websocket.go index af3c47f..3e7273e 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -56,9 +56,6 @@ var upgrader = websocket.Upgrader{ // RunWebSocketServer 运行一个正向WS server func RunWebSocketServer(b *coolq.CQBot, conf *config.WebsocketServer) { - if conf.Disabled { - return - } s := &webSocketServer{ bot: b, conf: conf, @@ -82,9 +79,6 @@ func RunWebSocketServer(b *coolq.CQBot, conf *config.WebsocketServer) { // RunWebSocketClient 运行一个正向WS client func RunWebSocketClient(b *coolq.CQBot, conf *config.WebsocketReverse) { - if conf.Disabled { - return - } c := &websocketClient{ bot: b, conf: conf,