From 449ae96c8f48b0f8d91b4fdf3b82cdb1454e9c16 Mon Sep 17 00:00:00 2001 From: Mrs4s Date: Wed, 15 Sep 2021 13:43:39 +0800 Subject: [PATCH] fix env conf load. --- global/config/config.go | 134 +++++++++++++++++++++------------------- 1 file changed, 69 insertions(+), 65 deletions(-) diff --git a/global/config/config.go b/global/config/config.go index 164b3bc..3455cf0 100644 --- a/global/config/config.go +++ b/global/config/config.go @@ -161,76 +161,80 @@ func Get() *Config { os.Exit(0) } - // type convert tools - toInt64 := func(str string) int64 { - i, _ := strconv.ParseInt(str, 10, 64) - return i - } + if hasEnvironmentConf { + // type convert tools + toInt64 := func(str string) int64 { + i, _ := strconv.ParseInt(str, 10, 64) + return i + } - // load config from environment variable - global.SetAtDefault(&config.Account.Uin, toInt64(os.Getenv("GCQ_UIN")), int64(0)) - global.SetAtDefault(&config.Account.Password, os.Getenv("GCQ_PWD"), "") - global.SetAtDefault(&config.Account.Status, int32(toInt64(os.Getenv("GCQ_STATUS"))), int32(0)) - global.SetAtDefault(&config.Account.ReLogin.Disabled, !global.EnsureBool(os.Getenv("GCQ_RELOGIN"), false), false) - global.SetAtDefault(&config.Account.ReLogin.Delay, uint(toInt64(os.Getenv("GCQ_RELOGIN_DELAY"))), uint(0)) - global.SetAtDefault(&config.Account.ReLogin.MaxTimes, uint(toInt64(os.Getenv("GCQ_RELOGIN_MAX_TIMES"))), uint(0)) - dbConf := &LevelDBConfig{Enable: global.EnsureBool(os.Getenv("GCQ_LEVELDB"), true)} - 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, - }, + // load config from environment variable + global.SetAtDefault(&config.Account.Uin, toInt64(os.Getenv("GCQ_UIN")), int64(0)) + global.SetAtDefault(&config.Account.Password, os.Getenv("GCQ_PWD"), "") + global.SetAtDefault(&config.Account.Status, int32(toInt64(os.Getenv("GCQ_STATUS"))), int32(0)) + global.SetAtDefault(&config.Account.ReLogin.Disabled, !global.EnsureBool(os.Getenv("GCQ_RELOGIN"), false), false) + global.SetAtDefault(&config.Account.ReLogin.Delay, uint(toInt64(os.Getenv("GCQ_RELOGIN_DELAY"))), uint(0)) + global.SetAtDefault(&config.Account.ReLogin.MaxTimes, uint(toInt64(os.Getenv("GCQ_RELOGIN_MAX_TIMES"))), uint(0)) + dbConf := &LevelDBConfig{Enable: global.EnsureBool(os.Getenv("GCQ_LEVELDB"), true)} + if config.Database == nil { + config.Database = make(map[string]yaml.Node) } - global.SetExcludeDefault(&httpConf.Disabled, global.EnsureBool(os.Getenv("GCQ_HTTP_DISABLE"), false), false) - global.SetExcludeDefault(&httpConf.Host, os.Getenv("GCQ_HTTP_HOST"), "") - global.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")}) + 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, + }, + } + global.SetExcludeDefault(&httpConf.Disabled, global.EnsureBool(os.Getenv("GCQ_HTTP_DISABLE"), false), false) + global.SetExcludeDefault(&httpConf.Host, os.Getenv("GCQ_HTTP_HOST"), "") + global.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}) } - _ = 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, - }, + if os.Getenv("GCQ_WS_PORT") != "" { + node := &yaml.Node{} + wsServerConf := &WebsocketServer{ + Host: "0.0.0.0", + Port: 6700, + MiddleWares: MiddleWares{ + AccessToken: accessTokenEnv, + }, + } + global.SetExcludeDefault(&wsServerConf.Disabled, global.EnsureBool(os.Getenv("GCQ_WS_DISABLE"), false), false) + global.SetExcludeDefault(&wsServerConf.Host, os.Getenv("GCQ_WS_HOST"), "") + global.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}) } - global.SetExcludeDefault(&wsServerConf.Disabled, global.EnsureBool(os.Getenv("GCQ_WS_DISABLE"), false), false) - global.SetExcludeDefault(&wsServerConf.Host, os.Getenv("GCQ_WS_HOST"), "") - global.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, - }, + 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, + }, + } + global.SetExcludeDefault(&rwsConf.Disabled, global.EnsureBool(os.Getenv("GCQ_RWS_DISABLE"), false), false) + global.SetExcludeDefault(&rwsConf.API, os.Getenv("GCQ_RWS_API"), "") + global.SetExcludeDefault(&rwsConf.Event, os.Getenv("GCQ_RWS_EVENT"), "") + global.SetExcludeDefault(&rwsConf.Universal, os.Getenv("GCQ_RWS_UNIVERSAL"), "") + _ = node.Encode(rwsConf) + config.Servers = append(config.Servers, map[string]yaml.Node{"ws-reverse": *node}) } - global.SetExcludeDefault(&rwsConf.Disabled, global.EnsureBool(os.Getenv("GCQ_RWS_DISABLE"), false), false) - global.SetExcludeDefault(&rwsConf.API, os.Getenv("GCQ_RWS_API"), "") - global.SetExcludeDefault(&rwsConf.Event, os.Getenv("GCQ_RWS_EVENT"), "") - global.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