79 lines
1.4 KiB
Go
79 lines
1.4 KiB
Go
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 Chat struct {
|
|
Token string `toml:"token"`
|
|
BaseUrl string `toml:"base_url"`
|
|
Model string `toml:"model"`
|
|
}
|
|
|
|
type Baidu struct {
|
|
TranslateApiKey string `toml:"translate_api_key"`
|
|
TranslateSecret string `toml:"translate_api_secret"`
|
|
}
|
|
|
|
type Config struct {
|
|
Chat *Chat `toml:"chat"`
|
|
Baidu *Baidu `toml:"baidu"`
|
|
}
|
|
|
|
func GetEnv() string {
|
|
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 = "prod"
|
|
}
|
|
|
|
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))
|
|
}
|