yintai2/base/global/load.go

88 lines
2.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package global
import (
"strings"
"github.com/go-viper/mapstructure/v2"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/spf13/viper"
)
var (
config *Config
)
type Mysql struct {
Host string `toml:"host"`
Port int `toml:"port"`
User string `toml:"user"`
Pass string `toml:"pass"`
Db string `toml:"db"`
Threshold int `toml:"threshold"`
//链接池配置
MaxIdle int `toml:"max_idle"`
MaxOpen int `toml:"max_open"`
MaxLifeTime int `toml:"max_life_time"`
}
type Redis struct {
Address string `toml:"address"` //服务redis的地址,不配置这个不初始化
}
//type QiniuConf struct {
// Bucket string `toml:"bucket"`
// Domain string `toml:"domain"`
// AccessKey string `toml:"access_key"`
// SecretKey string `toml:"secret_key"`
//}
type Storage struct {
QiniuBucket string `toml:"qiniu_bucket"`
QiniuDomain string `toml:"qiniu_domain"`
QiniuAccessKey string `toml:"qiniu_access_key"`
QiniuSecretKey string `toml:"qiniu_secret_key"`
MinioBucket string `toml:"minio_bucket"`
MinioDomain string `toml:"minio_domain"`
MinioAccessKey string `toml:"minio_access_key"`
MinioSecretKey string `toml:"minio_secret_key"`
}
type Server struct {
Address string `toml:"address"`
LogLevel int `toml:"log_level"` //日志级别
}
type Config struct {
Server *Server `toml:"server"`
Mysql *Mysql `toml:"mysql"`
Redis *Redis `toml:"redis"`
Storage *Storage `toml:"storage"`
}
func LoadConfig(file string) {
viper.SetConfigFile(file)
viper.SetConfigType("toml")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("conf")
var envConfig Config
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))
}
func GetConfig() *Config {
return config
}