2023-08-04 11:13:41 +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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Redis struct {
|
|
|
|
Addr string `toml:"addr"`
|
|
|
|
Db int `toml:"db"`
|
|
|
|
Password string `toml:"password"`
|
|
|
|
}
|
|
|
|
|
2023-08-04 18:27:37 +08:00
|
|
|
type QyWeixin struct {
|
2024-01-24 16:48:04 +08:00
|
|
|
Corpid string `toml:"corpid"`
|
2023-08-09 22:00:55 +08:00
|
|
|
EnterpriseAgent string `toml:"enterprise_agent"`
|
|
|
|
EnterpriseSecret string `toml:"enterprise_secret"`
|
2024-01-24 16:59:36 +08:00
|
|
|
PaySecret string `toml:"pay_secret"`
|
|
|
|
PayAgent string `toml:"pay_agent"`
|
2023-08-13 21:24:54 +08:00
|
|
|
ApproveAgent string `toml:"approve_agent"`
|
|
|
|
ApproveSecret string `toml:"approve_secret"`
|
2023-08-08 23:54:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type WxPay struct {
|
|
|
|
PayKeyPem string `toml:"pay_key_pem"`
|
|
|
|
PayCertPem string `toml:"pay_cert_pem"`
|
|
|
|
PayMchId string `toml:"pay_mchid"`
|
|
|
|
PaySerialNumber string `toml:"pay_serial_number"`
|
|
|
|
PayApiKeyV3 string `toml:"pay_api_key_v3"`
|
|
|
|
PayApiKeyV2 string `toml:"pay_api_key_v2"`
|
2023-08-04 18:27:37 +08:00
|
|
|
}
|
|
|
|
|
2023-08-04 11:13:41 +08:00
|
|
|
type Config struct {
|
2023-08-04 18:27:37 +08:00
|
|
|
Server *Server `toml:"server"`
|
|
|
|
Mysql *Mysql `toml:"mysql"`
|
|
|
|
Redis *Redis `toml:"redis"`
|
|
|
|
QyWeixin *QyWeixin `toml:"qyweixin"`
|
2023-08-08 23:54:30 +08:00
|
|
|
WxPay *WxPay `toml:"wxpay"`
|
2023-08-04 11:13:41 +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 = "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))
|
|
|
|
}
|