diff --git a/coolq/bot.go b/coolq/bot.go index b654acb..f5507a4 100644 --- a/coolq/bot.go +++ b/coolq/bot.go @@ -25,7 +25,7 @@ import ( "github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/internal/base" - "github.com/Mrs4s/go-cqhttp/internal/config" + "github.com/Mrs4s/go-cqhttp/modules/config" ) // CQBot CQBot结构体,存储Bot实例相关配置 diff --git a/internal/base/flag.go b/internal/base/flag.go index c96eebe..85c5ef1 100644 --- a/internal/base/flag.go +++ b/internal/base/flag.go @@ -3,12 +3,14 @@ package base import ( "flag" + "os" + "path" "time" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" - "github.com/Mrs4s/go-cqhttp/internal/config" + "github.com/Mrs4s/go-cqhttp/modules/config" ) // command flags @@ -49,14 +51,15 @@ var ( // Parse parse flags func Parse() { - flag.StringVar(&LittleC, "c", config.DefaultConfigFile, "configuration filename") + wd, _ := os.Getwd() + dc := path.Join(wd, "config.yml") + flag.StringVar(&LittleC, "c", dc, "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 } @@ -64,7 +67,7 @@ func Parse() { // Init read config from yml file func Init() { - conf := config.Get() + conf := config.Parse(LittleC) { // bool config if conf.Output.Debug { Debug = true diff --git a/main.go b/main.go index 9e1b5ff..cb71b6c 100644 --- a/main.go +++ b/main.go @@ -30,8 +30,8 @@ import ( "github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/global/terminal" "github.com/Mrs4s/go-cqhttp/internal/base" - "github.com/Mrs4s/go-cqhttp/internal/config" "github.com/Mrs4s/go-cqhttp/internal/selfupdate" + "github.com/Mrs4s/go-cqhttp/modules/config" "github.com/Mrs4s/go-cqhttp/server" ) diff --git a/internal/config/config.go b/modules/config/config.go similarity index 61% rename from internal/config/config.go rename to modules/config/config.go index 9292da9..31c4462 100644 --- a/internal/config/config.go +++ b/modules/config/config.go @@ -6,10 +6,8 @@ import ( _ "embed" // embed the default config file "fmt" "os" - "path" "strconv" "strings" - "sync" "github.com/Mrs4s/go-cqhttp/internal/param" @@ -21,11 +19,6 @@ import ( //go:embed default_config.yml var defaultConfig string -var currentPath = getCurrentPath() - -// DefaultConfigFile 默认配置文件路径 -var DefaultConfigFile = path.Join(currentPath, "config.yml") - // Reconnect 重连配置 type Reconnect struct { Disabled bool `yaml:"disabled"` @@ -144,113 +137,97 @@ type LevelDBConfig struct { Enable bool `yaml:"enable"` } -var ( - config *Config - once sync.Once -) +// Parse 从默认配置文件路径中获取 +func Parse(path string) *Config { + fromEnv := os.Getenv("GCQ_UIN") != "" -// Get 从默认配置文件路径中获取 -func Get() *Config { - once.Do(func() { - hasEnvironmentConf := os.Getenv("GCQ_UIN") != "" - - file, err := os.Open(DefaultConfigFile) - config = &Config{} - if err == nil { - defer func() { _ = file.Close() }() - if err = yaml.NewDecoder(file).Decode(config); err != nil && !hasEnvironmentConf { - log.Fatal("配置文件不合法!", err) - } - } else if !hasEnvironmentConf { - generateConfig() - os.Exit(0) + file, err := os.Open(path) + config := &Config{} + if err == nil { + defer func() { _ = file.Close() }() + if err = yaml.NewDecoder(file).Decode(config); err != nil && !fromEnv { + log.Fatal("配置文件不合法!", err) } - if hasEnvironmentConf { - // type convert tools - toInt64 := func(str string) int64 { - i, _ := strconv.ParseInt(str, 10, 64) - return i - } - - // load config from environment variable - param.SetAtDefault(&config.Account.Uin, toInt64(os.Getenv("GCQ_UIN")), int64(0)) - param.SetAtDefault(&config.Account.Password, os.Getenv("GCQ_PWD"), "") - param.SetAtDefault(&config.Account.Status, int32(toInt64(os.Getenv("GCQ_STATUS"))), int32(0)) - param.SetAtDefault(&config.Account.ReLogin.Disabled, !param.EnsureBool(os.Getenv("GCQ_RELOGIN_DISABLED"), true), false) - param.SetAtDefault(&config.Account.ReLogin.Delay, uint(toInt64(os.Getenv("GCQ_RELOGIN_DELAY"))), uint(0)) - param.SetAtDefault(&config.Account.ReLogin.MaxTimes, uint(toInt64(os.Getenv("GCQ_RELOGIN_MAX_TIMES"))), uint(0)) - dbConf := &LevelDBConfig{Enable: param.EnsureBool(os.Getenv("GCQ_LEVELDB"), true)} - if config.Database == nil { - config.Database = make(map[string]yaml.Node) - } - config.Database["leveldb"] = func() yaml.Node { - n := &yaml.Node{} - _ = n.Encode(dbConf) - return *n - }() - accessTokenEnv := os.Getenv("GCQ_ACCESS_TOKEN") - if os.Getenv("GCQ_HTTP_PORT") != "" { - node := &yaml.Node{} - httpConf := &HTTPServer{ - Host: "0.0.0.0", - Port: 5700, - MiddleWares: MiddleWares{ - AccessToken: accessTokenEnv, - }, - } - param.SetExcludeDefault(&httpConf.Disabled, param.EnsureBool(os.Getenv("GCQ_HTTP_DISABLE"), false), false) - param.SetExcludeDefault(&httpConf.Host, os.Getenv("GCQ_HTTP_HOST"), "") - param.SetExcludeDefault(&httpConf.Port, int(toInt64(os.Getenv("GCQ_HTTP_PORT"))), 0) - if os.Getenv("GCQ_HTTP_POST_URL") != "" { - httpConf.Post = append(httpConf.Post, struct { - URL string `yaml:"url"` - Secret string `yaml:"secret"` - }{os.Getenv("GCQ_HTTP_POST_URL"), os.Getenv("GCQ_HTTP_POST_SECRET")}) - } - _ = node.Encode(httpConf) - config.Servers = append(config.Servers, map[string]yaml.Node{"http": *node}) - } - if os.Getenv("GCQ_WS_PORT") != "" { - node := &yaml.Node{} - wsServerConf := &WebsocketServer{ - Host: "0.0.0.0", - Port: 6700, - MiddleWares: MiddleWares{ - AccessToken: accessTokenEnv, - }, - } - param.SetExcludeDefault(&wsServerConf.Disabled, param.EnsureBool(os.Getenv("GCQ_WS_DISABLE"), false), false) - param.SetExcludeDefault(&wsServerConf.Host, os.Getenv("GCQ_WS_HOST"), "") - param.SetExcludeDefault(&wsServerConf.Port, int(toInt64(os.Getenv("GCQ_WS_PORT"))), 0) - _ = node.Encode(wsServerConf) - config.Servers = append(config.Servers, map[string]yaml.Node{"ws": *node}) - } - if os.Getenv("GCQ_RWS_API") != "" || os.Getenv("GCQ_RWS_EVENT") != "" || os.Getenv("GCQ_RWS_UNIVERSAL") != "" { - node := &yaml.Node{} - rwsConf := &WebsocketReverse{ - MiddleWares: MiddleWares{ - AccessToken: accessTokenEnv, - }, - } - param.SetExcludeDefault(&rwsConf.Disabled, param.EnsureBool(os.Getenv("GCQ_RWS_DISABLE"), false), false) - param.SetExcludeDefault(&rwsConf.API, os.Getenv("GCQ_RWS_API"), "") - param.SetExcludeDefault(&rwsConf.Event, os.Getenv("GCQ_RWS_EVENT"), "") - param.SetExcludeDefault(&rwsConf.Universal, os.Getenv("GCQ_RWS_UNIVERSAL"), "") - _ = node.Encode(rwsConf) - config.Servers = append(config.Servers, map[string]yaml.Node{"ws-reverse": *node}) - } - } - }) - return config -} - -// getCurrentPath 获取当前文件的路径,直接返回string -func getCurrentPath() string { - cwd, e := os.Getwd() - if e != nil { - panic(e) + } else if !fromEnv { + generateConfig() + os.Exit(0) } - return cwd + if fromEnv { + // type convert tools + toInt64 := func(str string) int64 { + i, _ := strconv.ParseInt(str, 10, 64) + return i + } + + // load config from environment variable + param.SetAtDefault(&config.Account.Uin, toInt64(os.Getenv("GCQ_UIN")), int64(0)) + param.SetAtDefault(&config.Account.Password, os.Getenv("GCQ_PWD"), "") + param.SetAtDefault(&config.Account.Status, int32(toInt64(os.Getenv("GCQ_STATUS"))), int32(0)) + param.SetAtDefault(&config.Account.ReLogin.Disabled, !param.EnsureBool(os.Getenv("GCQ_RELOGIN_DISABLED"), true), false) + param.SetAtDefault(&config.Account.ReLogin.Delay, uint(toInt64(os.Getenv("GCQ_RELOGIN_DELAY"))), uint(0)) + param.SetAtDefault(&config.Account.ReLogin.MaxTimes, uint(toInt64(os.Getenv("GCQ_RELOGIN_MAX_TIMES"))), uint(0)) + dbConf := &LevelDBConfig{Enable: param.EnsureBool(os.Getenv("GCQ_LEVELDB"), true)} + if config.Database == nil { + config.Database = make(map[string]yaml.Node) + } + config.Database["leveldb"] = func() yaml.Node { + n := &yaml.Node{} + _ = n.Encode(dbConf) + return *n + }() + accessTokenEnv := os.Getenv("GCQ_ACCESS_TOKEN") + if os.Getenv("GCQ_HTTP_PORT") != "" { + node := &yaml.Node{} + httpConf := &HTTPServer{ + Host: "0.0.0.0", + Port: 5700, + MiddleWares: MiddleWares{ + AccessToken: accessTokenEnv, + }, + } + param.SetExcludeDefault(&httpConf.Disabled, param.EnsureBool(os.Getenv("GCQ_HTTP_DISABLE"), false), false) + param.SetExcludeDefault(&httpConf.Host, os.Getenv("GCQ_HTTP_HOST"), "") + param.SetExcludeDefault(&httpConf.Port, int(toInt64(os.Getenv("GCQ_HTTP_PORT"))), 0) + if os.Getenv("GCQ_HTTP_POST_URL") != "" { + httpConf.Post = append(httpConf.Post, struct { + URL string `yaml:"url"` + Secret string `yaml:"secret"` + }{os.Getenv("GCQ_HTTP_POST_URL"), os.Getenv("GCQ_HTTP_POST_SECRET")}) + } + _ = node.Encode(httpConf) + config.Servers = append(config.Servers, map[string]yaml.Node{"http": *node}) + } + if os.Getenv("GCQ_WS_PORT") != "" { + node := &yaml.Node{} + wsServerConf := &WebsocketServer{ + Host: "0.0.0.0", + Port: 6700, + MiddleWares: MiddleWares{ + AccessToken: accessTokenEnv, + }, + } + param.SetExcludeDefault(&wsServerConf.Disabled, param.EnsureBool(os.Getenv("GCQ_WS_DISABLE"), false), false) + param.SetExcludeDefault(&wsServerConf.Host, os.Getenv("GCQ_WS_HOST"), "") + param.SetExcludeDefault(&wsServerConf.Port, int(toInt64(os.Getenv("GCQ_WS_PORT"))), 0) + _ = node.Encode(wsServerConf) + config.Servers = append(config.Servers, map[string]yaml.Node{"ws": *node}) + } + if os.Getenv("GCQ_RWS_API") != "" || os.Getenv("GCQ_RWS_EVENT") != "" || os.Getenv("GCQ_RWS_UNIVERSAL") != "" { + node := &yaml.Node{} + rwsConf := &WebsocketReverse{ + MiddleWares: MiddleWares{ + AccessToken: accessTokenEnv, + }, + } + param.SetExcludeDefault(&rwsConf.Disabled, param.EnsureBool(os.Getenv("GCQ_RWS_DISABLE"), false), false) + param.SetExcludeDefault(&rwsConf.API, os.Getenv("GCQ_RWS_API"), "") + param.SetExcludeDefault(&rwsConf.Event, os.Getenv("GCQ_RWS_EVENT"), "") + param.SetExcludeDefault(&rwsConf.Universal, os.Getenv("GCQ_RWS_UNIVERSAL"), "") + _ = node.Encode(rwsConf) + config.Servers = append(config.Servers, map[string]yaml.Node{"ws-reverse": *node}) + } + } + return config } // generateConfig 生成配置文件 diff --git a/internal/config/default_config.yml b/modules/config/default_config.yml similarity index 100% rename from internal/config/default_config.yml rename to modules/config/default_config.yml diff --git a/server/http.go b/server/http.go index b5ba048..767c02c 100644 --- a/server/http.go +++ b/server/http.go @@ -21,7 +21,7 @@ import ( "github.com/tidwall/gjson" "github.com/Mrs4s/go-cqhttp/coolq" - "github.com/Mrs4s/go-cqhttp/internal/config" + "github.com/Mrs4s/go-cqhttp/modules/config" ) type httpServer struct { diff --git a/server/pprof.go b/server/pprof.go index d1e30b1..5ea0640 100644 --- a/server/pprof.go +++ b/server/pprof.go @@ -9,7 +9,7 @@ import ( log "github.com/sirupsen/logrus" - "github.com/Mrs4s/go-cqhttp/internal/config" + "github.com/Mrs4s/go-cqhttp/modules/config" ) // RunPprofServer 启动 pprof 性能分析服务器 diff --git a/server/scf.go b/server/scf.go index 3a4beff..68f6da5 100644 --- a/server/scf.go +++ b/server/scf.go @@ -16,7 +16,7 @@ import ( "github.com/Mrs4s/go-cqhttp/coolq" "github.com/Mrs4s/go-cqhttp/global" - "github.com/Mrs4s/go-cqhttp/internal/config" + "github.com/Mrs4s/go-cqhttp/modules/config" ) type lambdaClient struct { diff --git a/server/websocket.go b/server/websocket.go index 6d1000c..a7e3f1c 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -12,7 +12,7 @@ import ( "github.com/Mrs4s/go-cqhttp/coolq" "github.com/Mrs4s/go-cqhttp/global" - "github.com/Mrs4s/go-cqhttp/internal/config" + "github.com/Mrs4s/go-cqhttp/modules/config" "github.com/Mrs4s/MiraiGo/utils" "github.com/gorilla/websocket"