feat(backend, worker): add config watching and simplify error handling in InitEnv function

This commit is contained in:
keven
2025-10-19 15:23:42 +08:00
parent a4b3dad85e
commit 84c104be90
2 changed files with 17 additions and 8 deletions

View File

@@ -23,12 +23,14 @@ func InitEnv() {
v.AddConfigPath(".")
v.AddConfigPath("../")
v.AutomaticEnv()
v.WatchConfig()
err := v.ReadInConfig()
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
// 只有当错误不是"配置文件未找到"时才 panic
panic(err)
}
// if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
// // 只有当错误不是"配置文件未找到"时才 panic
// panic(err)
// }
}
}

View File

@@ -23,12 +23,14 @@ func InitEnv() {
v.AddConfigPath(".")
v.AddConfigPath("../")
v.AutomaticEnv()
v.WatchConfig()
err := v.ReadInConfig()
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
// 只有当错误不是"配置文件未找到"时才 panic
panic(err)
}
// if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
// // 只有当错误不是"配置文件未找到"时才 panic
// panic(err)
// }
}
}
@@ -44,3 +46,8 @@ func GetEnvWithDefault(key string, defaultValue string) string {
}
return value
}
func GetEnvMapString(key string) map[string]string {
InitEnv()
return v.GetStringMapString(key)
}