forked from nixcn/nixcn-cms
Fix config module for unit test
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
@@ -7,28 +7,35 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func Init() {
|
||||
// Set config path by env
|
||||
confPath := os.Getenv("CONFIG_PATH")
|
||||
if confPath == "" {
|
||||
confPath = "config.yaml"
|
||||
func ConfigDir() string {
|
||||
env := os.Getenv("CONFIG_PATH")
|
||||
if env != "" {
|
||||
return env
|
||||
}
|
||||
return "."
|
||||
}
|
||||
|
||||
func Init() {
|
||||
// Read global config
|
||||
viper.SetConfigFile(confPath)
|
||||
viper.SetDefault("Server", serverDef)
|
||||
viper.SetDefault("Database", databaseDef)
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("yaml")
|
||||
viper.AddConfigPath(ConfigDir())
|
||||
|
||||
// Bind ENV
|
||||
viper.BindEnv("server.address", "SERVER_ADDRESS")
|
||||
viper.BindEnv("server.debug_mode", "SERVER_DEBUG_MODE")
|
||||
viper.BindEnv("server.file_logger", "SERVER_FILE_LOGGER")
|
||||
viper.BindEnv("server.jwt_secret", "SERVER_JWT_SECRET")
|
||||
viper.BindEnv("database.type", "DATABASE_TYPE")
|
||||
viper.BindEnv("database.host", "DATABASE_HOST")
|
||||
viper.BindEnv("database.name", "DATABASE_NAME")
|
||||
viper.BindEnv("database.username", "DATABASE_USERNAME")
|
||||
viper.BindEnv("database.password", "DATABASE_PASSWORD")
|
||||
|
||||
conf := &config{}
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
// Dont generate config when using dev mode
|
||||
if os.Getenv("GO_ENV") == "test" || os.Getenv("CONFIG_PATH") != "" {
|
||||
log.Fatalf("[Config] failed to read config %s: %v", confPath, err)
|
||||
}
|
||||
|
||||
log.Println("Can't read config, trying to modify!")
|
||||
if err := viper.WriteConfig(); err != nil {
|
||||
log.Fatal("[Config] Error writing config: ", err)
|
||||
}
|
||||
log.Fatalln("Can't read config!")
|
||||
}
|
||||
if err := viper.Unmarshal(conf); err != nil {
|
||||
log.Fatal(err)
|
||||
|
||||
@@ -1,71 +1,12 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func GetEnv(key string) string {
|
||||
_ = godotenv.Load()
|
||||
upperKey := strings.ToUpper(key)
|
||||
return os.Getenv(upperKey)
|
||||
}
|
||||
|
||||
func SetEnvConf(key string, sub string) {
|
||||
envJoin := strings.Join([]string{key, sub}, "_")
|
||||
env := GetEnv(envJoin)
|
||||
confJoin := strings.Join([]string{key, sub}, ".")
|
||||
orig := Get(confJoin)
|
||||
if env != "" {
|
||||
switch orig.(type) {
|
||||
case string:
|
||||
Set(confJoin, env)
|
||||
case int:
|
||||
conv, err := strconv.Atoi(env)
|
||||
if err != nil {
|
||||
log.Panic("[Config] Error converting string to int: ", err)
|
||||
}
|
||||
Set(confJoin, conv)
|
||||
case bool:
|
||||
switch env {
|
||||
case "true":
|
||||
Set(confJoin, true)
|
||||
case "false":
|
||||
Set(confJoin, false)
|
||||
}
|
||||
case []string:
|
||||
trim := strings.TrimSpace(env)
|
||||
trim = strings.TrimPrefix(trim, "[")
|
||||
trim = strings.TrimSuffix(trim, "]")
|
||||
var envArray []string
|
||||
for _, v := range strings.Split(trim, ",") {
|
||||
trimSub := strings.TrimPrefix(v, "\"")
|
||||
trimSub = strings.TrimSuffix(trimSub, "\"")
|
||||
envArray = append(envArray, trimSub)
|
||||
}
|
||||
Set(confJoin, envArray)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func EnvInit() {
|
||||
var dict = map[string][]string{
|
||||
"server": {"address", "debug_mode", "file_logger", "jwt_secret"},
|
||||
"database": {"type", "host", "name", "username", "password"},
|
||||
}
|
||||
for key, value := range dict {
|
||||
for _, sub := range value {
|
||||
SetEnvConf(key, sub)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TZ() string {
|
||||
tz := GetEnv("TZ")
|
||||
tz := os.Getenv("TZ")
|
||||
|
||||
if tz == "" {
|
||||
return "Asia/Shanghai"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user