github地址:github.com/spf13/viper
Viper 是一个完整的 Go 应用程序配置解决方案,优势就在于开发项目中你不必去操心配置文件的格式而是让你腾出手来专注于项目的开发。其特性如下:
-
支持 JSON/TOML/YAML/HCL/envfile/Java properties 等多种格式的配置文件;
-
可以设置监听配置文件的修改,修改时自动加载新的配置;
-
从环境变量、命令行选项和io.Reader中读取配置;
-
从远程配置系统中读取和监听修改,如 etcd/Consul;
-
代码逻辑中显示设置键值
注:Viper让需要重启服务器才能使配置生效的日子一去不复返!!!这才是VIper最大的魅力
//global
var ( GVA_DB *gorm.DB GVA_DBList map[string]*gorm.DB GVA_REDIS *redis.Client GVA_CONFIG config.Server GVA_VP *viper.Viper // GVA_LOG *oplogging.Logger GVA_LOG *zap.Logger GVA_Concurrency_Control = &singleflight.Group{}
BlackCache local_cache.Cache lock sync.RWMutex)
// config.Server
type Server struct { JWT JWT `mapstructure:"jwt" json:"jwt" yaml:"jwt"` Zap Zap `mapstructure:"zap" json:"zap" yaml:"zap"` Redis Redis `mapstructure:"redis" json:"redis" yaml:"redis"` Email Email `mapstructure:"email" json:"email" yaml:"email"` Casbin Casbin `mapstructure:"casbin" json:"casbin" yaml:"casbin"` System System `mapstructure:"system" json:"system" yaml:"system"` Captcha Captcha `mapstructure:"captcha" json:"captcha" yaml:"captcha"` // auto AutoCode Autocode `mapstructure:"autocode" json:"autocode" yaml:"autocode"` // gorm Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"` Pgsql Pgsql `mapstructure:"pgsql" json:"pgsql" yaml:"pgsql"` DBList []DB `mapstructure:"db-list" json:"db-list" yaml:"db-list"` // oss Local Local `mapstructure:"local" json:"local" yaml:"local"` Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"` AliyunOSS AliyunOSS `mapstructure:"aliyun-oss" json:"aliyun-oss" yaml:"aliyun-oss"` HuaWeiObs HuaWeiObs `mapstructure:"hua-wei-obs" json:"hua-wei-obs" yaml:"hua-wei-obs"` TencentCOS TencentCOS `mapstructure:"tencent-cos" json:"tencent-cos" yaml:"tencent-cos"` AwsS3 AwsS3 `mapstructure:"aws-s3" json:"aws-s3" yaml:"aws-s3"`
Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"` Timer Timer `mapstructure:"timer" json:"timer" yaml:"timer"`
// 跨域配置 Cors CORS `mapstructure:"cors" json:"cors" yaml:"cors"`}
下方是使用方法
func Viper(path ...string) {//一般放在初始化 init中执行 v := viper.New()//创建新的 if len(path) <= 0 {//判断是否传入配置文件地址 v.AddConfigPath("./")//没有地址就用默认的地址 } else { v.AddConfigPath(path[0])//使用第一个参数作为配置文件地址 } //路径 v.SetConfigType("yml")//设置配置文件的类型 v.SetConfigName("config")//设置配置文件的名称 err := v.ReadInConfig() //读取配置文件 if err != nil { panic(err) } if err := v.Unmarshal(&global.GVA_CONFIG); err != nil {//将读取的配置文件信息赋值给全局配置文件 panic(err) } v.WatchConfig()//监听配置文件是否有变动 v.OnConfigChange(func(e fsnotify.Event) { //fsnotify.Event 文件变更事件 fmt.Println("config file changed:", e.Name)//事件名称 if err := v.Unmarshal(&global.GVA_CONFIG); err != nil { fmt.Println(err) } }) global.GVA_VP = v}
在Viper中有多个获取配置文件内容的方法,可根据值的类型进行选择:
-
Get(key string) : interface{}
-
GetBool(key string) : bool
-
GetFloat64(key string) : float64
-
GetInt(key string) : int
-
GetIntSlice(key string) : []int
-
GetString(key string) : string
-
GetStringMap(key string) : map[string]interface{}
-
GetStringMapString(key string) : map[string]string
-
GetStringSlice(key string) : []string
-
GetTime(key string) : time.Time
-
GetDuration(key string) : time.Duration
-
IsSet(key string) : bool
-
AllSettings() : map[string]interface{}



