Skip to content

Commit

Permalink
🐛 fix: config file loading error
Browse files Browse the repository at this point in the history
  • Loading branch information
MartialBE committed Apr 18, 2024
1 parent 303fe33 commit 0c5ad81
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 36 deletions.
6 changes: 3 additions & 3 deletions common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions common/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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())
}
Expand Down
2 changes: 1 addition & 1 deletion common/requester/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion common/requester/ws_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
11 changes: 0 additions & 11 deletions common/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
// }
6 changes: 3 additions & 3 deletions common/telegram/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ func main() {
}

func initMemoryCache() {
if viper.GetBool("MEMORY_CACHE_ENABLED") {
if viper.GetBool("memory_cache_enabled") {
common.MemoryCacheEnabled = true
}

if !common.MemoryCacheEnabled {
return
}

syncFrequency := viper.GetInt("SYNC_FREQUENCY")
syncFrequency := viper.GetInt("sync_frequency")
model.TokenCacheSeconds = syncFrequency

common.SysLog("memory cache enabled")
Expand All @@ -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() {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions middleware/rate-limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion middleware/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions model/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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")
Expand All @@ -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
})
Expand Down
2 changes: 1 addition & 1 deletion router/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 0c5ad81

Please sign in to comment.