diff --git a/common/config/config.go b/common/config/config.go index 7d2634c2e..0535d1f92 100644 --- a/common/config/config.go +++ b/common/config/config.go @@ -20,9 +20,9 @@ func InitConf() { common.SysLog("running in debug mode") } - common.IsMasterNode = viper.GetString("NODE_TYPE") != "slave" - common.RequestInterval = time.Duration(viper.GetInt("POLLING_INTERVAL")) * time.Second - common.SessionSecret = common.GetOrDefault("SESSION_SECRET", common.SessionSecret) + common.IsMasterNode = viper.GetString("node_type") != "slave" + common.RequestInterval = time.Duration(viper.GetInt("polling_interval")) * time.Second + common.SessionSecret = common.GetOrDefault("session_secret", common.SessionSecret) } func setConfigFile() { diff --git a/common/redis.go b/common/redis.go index 8657dff12..9b29f546c 100644 --- a/common/redis.go +++ b/common/redis.go @@ -13,13 +13,13 @@ var RedisEnabled = false // InitRedisClient This function is called after init() func InitRedisClient() (err error) { - redisConn := viper.GetString("REDIS_CONN_STRING") + redisConn := viper.GetString("redis_conn_string") if redisConn == "" { SysLog("REDIS_CONN_STRING not set, Redis is not enabled") return nil } - if viper.GetInt("SYNC_FREQUENCY") == 0 { + if viper.GetInt("sync_frequency") == 0 { SysLog("SYNC_FREQUENCY not set, Redis is disabled") return nil } @@ -47,7 +47,7 @@ func InitRedisClient() (err error) { } func ParseRedisOption() *redis.Options { - opt, err := redis.ParseURL(viper.GetString("REDIS_CONN_STRING")) + opt, err := redis.ParseURL(viper.GetString("redis_conn_string")) if err != nil { FatalLog("failed to parse Redis connection string: " + err.Error()) } diff --git a/common/requester/http_client.go b/common/requester/http_client.go index a441a42fc..b118dc2e8 100644 --- a/common/requester/http_client.go +++ b/common/requester/http_client.go @@ -74,7 +74,7 @@ func InitHttpClient() { Transport: trans, } - relayTimeout := common.GetOrDefault("RELAY_TIMEOUT", 600) + relayTimeout := common.GetOrDefault("connect_timeout", 600) if relayTimeout != 0 { HTTPClient.Timeout = time.Duration(relayTimeout) * time.Second } diff --git a/common/requester/ws_client.go b/common/requester/ws_client.go index a4d22dfd7..2ea865d7a 100644 --- a/common/requester/ws_client.go +++ b/common/requester/ws_client.go @@ -14,7 +14,7 @@ import ( func GetWSClient(proxyAddr string) *websocket.Dialer { dialer := &websocket.Dialer{ - HandshakeTimeout: time.Duration(common.GetOrDefault("CONNECT_TIMEOUT", 5)) * time.Second, + HandshakeTimeout: time.Duration(common.GetOrDefault("connect_timeout", 5)) * time.Second, } if proxyAddr != "" { diff --git a/common/storage/storage_test.go b/common/storage/storage_test.go index 4a0a37054..80359457f 100644 --- a/common/storage/storage_test.go +++ b/common/storage/storage_test.go @@ -53,14 +53,3 @@ func TestImgurUpload(t *testing.T) { fmt.Println(err) assert.Nil(t, err) } - -// func TestSMMSUploadError(t *testing.T) { -// InitConfig() -// access_token := viper.GetString("notify.dingtalk.token") -// secret := "test" -// dingTalk := channel.NewDingTalk(access_token, secret) - -// err := dingTalk.Send(context.Background(), "Test Title", "*Test Message*") -// fmt.Println(err) -// assert.Error(t, err) -// } diff --git a/common/telegram/common.go b/common/telegram/common.go index 106138af0..f49c70695 100644 --- a/common/telegram/common.go +++ b/common/telegram/common.go @@ -28,7 +28,7 @@ func InitTelegramBot() { return } - botKey := viper.GetString("TG_BOT_API_KEY") + botKey := viper.GetString("tg.bot_api_key") if botKey == "" { common.SysLog("Telegram bot is not enabled") return @@ -48,7 +48,7 @@ func InitTelegramBot() { } func StartTelegramBot() { - botWebhook := viper.GetString("TG_WEBHOOK_SECRET") + botWebhook := viper.GetString("tg.webhook_secret") if botWebhook != "" { if common.ServerAddress == "" { common.SysLog("Telegram bot is not enabled: Server address is not set") @@ -57,7 +57,7 @@ func StartTelegramBot() { } TGWebHookSecret = botWebhook serverAddress := strings.TrimSuffix(common.ServerAddress, "/") - urlPath := fmt.Sprintf("/api/telegram/%s", viper.GetString("TG_BOT_API_KEY")) + urlPath := fmt.Sprintf("/api/telegram/%s", viper.GetString("tg.bot_api_key")) webHookOpts := &ext.AddWebhookOpts{ SecretToken: TGWebHookSecret, diff --git a/main.go b/main.go index f821fb2a2..fb89d0156 100644 --- a/main.go +++ b/main.go @@ -57,7 +57,7 @@ func main() { } func initMemoryCache() { - if viper.GetBool("MEMORY_CACHE_ENABLED") { + if viper.GetBool("memory_cache_enabled") { common.MemoryCacheEnabled = true } @@ -65,7 +65,7 @@ func initMemoryCache() { return } - syncFrequency := viper.GetInt("SYNC_FREQUENCY") + syncFrequency := viper.GetInt("sync_frequency") model.TokenCacheSeconds = syncFrequency common.SysLog("memory cache enabled") @@ -74,8 +74,8 @@ func initMemoryCache() { } func initSync() { - go controller.AutomaticallyUpdateChannels(viper.GetInt("CHANNEL_UPDATE_FREQUENCY")) - go controller.AutomaticallyTestChannels(viper.GetInt("CHANNEL_TEST_FREQUENCY")) + go controller.AutomaticallyUpdateChannels(viper.GetInt("channel.update_frequency")) + go controller.AutomaticallyTestChannels(viper.GetInt("channel.test_frequency")) } func initHttpServer() { @@ -92,7 +92,7 @@ func initHttpServer() { server.Use(sessions.Sessions("session", store)) router.SetRouter(server, buildFS, indexPage) - port := viper.GetString("PORT") + port := viper.GetString("port") err := server.Run(":" + port) if err != nil { diff --git a/middleware/rate-limit.go b/middleware/rate-limit.go index 65b358cba..ef0015aca 100644 --- a/middleware/rate-limit.go +++ b/middleware/rate-limit.go @@ -103,11 +103,11 @@ func rateLimitFactory(maxRequestNum int, duration int64, mark string) func(c *gi } func GlobalWebRateLimit() func(c *gin.Context) { - return rateLimitFactory(common.GetOrDefault("GLOBAL_WEB_RATE_LIMIT", GlobalWebRateLimitNum), GlobalWebRateLimitDuration, "GW") + return rateLimitFactory(common.GetOrDefault("global.web_rate_limit", GlobalWebRateLimitNum), GlobalWebRateLimitDuration, "GW") } func GlobalAPIRateLimit() func(c *gin.Context) { - return rateLimitFactory(common.GetOrDefault("GLOBAL_API_RATE_LIMIT", GlobalApiRateLimitNum), GlobalApiRateLimitDuration, "GA") + return rateLimitFactory(common.GetOrDefault("global.api_rate_limit", GlobalApiRateLimitNum), GlobalApiRateLimitDuration, "GA") } func CriticalRateLimit() func(c *gin.Context) { diff --git a/middleware/telegram.go b/middleware/telegram.go index 36a972b9f..e54dd2530 100644 --- a/middleware/telegram.go +++ b/middleware/telegram.go @@ -11,7 +11,7 @@ func Telegram() func(c *gin.Context) { return func(c *gin.Context) { token := c.Param("token") - if !telegram.TGEnabled || telegram.TGWebHookSecret == "" || token == "" || token != viper.GetString("TG_BOT_API_KEY") { + if !telegram.TGEnabled || telegram.TGWebHookSecret == "" || token == "" || token != viper.GetString("tg.bot_api_key") { c.String(404, "Page not found") c.Abort() return diff --git a/model/main.go b/model/main.go index 1b74c2511..a826f6e78 100644 --- a/model/main.go +++ b/model/main.go @@ -24,9 +24,9 @@ func SetupDB() { ChannelGroup.Load() common.RootUserEmail = GetRootUserEmail() - if viper.GetBool("BATCH_UPDATE_ENABLED") { + if viper.GetBool("batch_update_enabled") { common.BatchUpdateEnabled = true - common.BatchUpdateInterval = common.GetOrDefault("BATCH_UPDATE_INTERVAL", 5) + common.BatchUpdateInterval = common.GetOrDefault("batch_update_interval", 5) common.SysLog("batch update enabled with interval " + strconv.Itoa(common.BatchUpdateInterval) + "s") InitBatchUpdater() } @@ -56,8 +56,8 @@ func createRootAccountIfNeed() error { } func chooseDB() (*gorm.DB, error) { - if viper.IsSet("SQL_DSN") { - dsn := viper.GetString("SQL_DSN") + if viper.IsSet("sql_dsn") { + dsn := viper.GetString("sql_dsn") if strings.HasPrefix(dsn, "postgres://") { // Use PostgreSQL common.SysLog("using PostgreSQL as database") @@ -78,7 +78,7 @@ func chooseDB() (*gorm.DB, error) { // Use SQLite common.SysLog("SQL_DSN not set, using SQLite as database") common.UsingSQLite = true - config := fmt.Sprintf("?_busy_timeout=%d", common.GetOrDefault("SQLITE_BUSY_TIMEOUT", 3000)) + config := fmt.Sprintf("?_busy_timeout=%d", common.GetOrDefault("sqlite_busy_timeout", 3000)) return gorm.Open(sqlite.Open(viper.GetString("sqlite_path")+config), &gorm.Config{ PrepareStmt: true, // precompile SQL }) diff --git a/router/main.go b/router/main.go index 559747fec..e26118d70 100644 --- a/router/main.go +++ b/router/main.go @@ -15,7 +15,7 @@ func SetRouter(router *gin.Engine, buildFS embed.FS, indexPage []byte) { SetApiRouter(router) SetDashboardRouter(router) SetRelayRouter(router) - frontendBaseUrl := viper.GetString("FRONTEND_BASE_URL") + frontendBaseUrl := viper.GetString("frontend_base_url") if common.IsMasterNode && frontendBaseUrl != "" { frontendBaseUrl = "" common.SysLog("FRONTEND_BASE_URL is ignored on master node")