package config import ( "os" "strings" "github.com/go-viper/mapstructure/v2" log "github.com/sirupsen/logrus" "github.com/smbrave/goutil" "github.com/spf13/viper" ) var ( config *Config configEnv string ) type Config struct { } func GetEnv() string { configEnv = os.Getenv("CONFIG_ENV") if configEnv == "" { configEnv = "prod" } return configEnv } func IsProdEnv() bool { return configEnv == "prod" } func IsDevEnv() bool { return configEnv == "dev" } func IsTestEnv() bool { return configEnv == "test" } func GetConfig() *Config { return config } func LoadConfig() { configEnv = os.Getenv("CONFIG_ENV") if configEnv == "" { configEnv = "pro" } var envConfig Config viper.SetConfigFile("conf/server.conf." + configEnv) viper.SetConfigType("toml") viper.AutomaticEnv() viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.SetEnvPrefix("conf") if err := viper.ReadInConfig(); err != nil { panic(err) } if err := viper.Unmarshal(&envConfig, func(decoderConfig *mapstructure.DecoderConfig) { decoderConfig.TagName = "toml" }); err != nil { panic(err) } config = &envConfig log.Infof("load real config[%s] ", goutil.EncodeJSONIndent(config)) }