2023-04-06 13:46:19 +08:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/smbrave/goutil"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
config *Config
|
|
|
|
configEnv string
|
|
|
|
BuildTime string
|
|
|
|
CommitId string
|
|
|
|
)
|
|
|
|
|
|
|
|
type Mysql struct {
|
|
|
|
Host string `toml:"host"`
|
|
|
|
Port int `toml:"port"`
|
|
|
|
User string `toml:"user"`
|
|
|
|
Pass string `toml:"pass"`
|
|
|
|
Db string `toml:"db"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
Address string `toml:"address"`
|
|
|
|
LogLevel int `toml:"log_level"`
|
|
|
|
EnableDoc bool `toml:"enable_doc"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Redis struct {
|
|
|
|
Addr string `toml:"addr"`
|
|
|
|
Db int `toml:"db"`
|
|
|
|
Password string `toml:"password"`
|
|
|
|
}
|
2023-04-07 23:51:09 +08:00
|
|
|
|
|
|
|
type Weixin struct {
|
2023-04-09 11:38:19 +08:00
|
|
|
QiyeAppid string `toml:"qiye_appid"`
|
|
|
|
Qiyesecret string `toml:"qiye_secret"`
|
|
|
|
QiyeAgent string `toml:"qiye_agent"`
|
|
|
|
Receiver []string `toml:"receiver"`
|
2023-04-07 23:51:09 +08:00
|
|
|
}
|
|
|
|
|
2023-04-06 13:46:19 +08:00
|
|
|
type Film struct {
|
2023-04-08 01:38:13 +08:00
|
|
|
DadiToken string `toml:"dadi_token"`
|
|
|
|
HahaToken string `toml:"haha_token"`
|
2023-04-06 13:46:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Server *Server `toml:"server"`
|
|
|
|
Mysql *Mysql `toml:"mysql"`
|
|
|
|
Redis *Redis `toml:"redis"`
|
2023-04-07 23:51:09 +08:00
|
|
|
Weixin *Weixin `toml:"weixin"`
|
2023-04-06 13:46:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 LoadServerConfig() {
|
|
|
|
configEnv = os.Getenv("CONFIG_ENV")
|
|
|
|
if configEnv == "" {
|
|
|
|
configEnv = "dev"
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|