package conf import ( "apsClient/pkg/convertx" "apsClient/pkg/logx" "apsClient/pkg/mysqlx" "apsClient/pkg/redisx" "apsClient/pkg/sqlitex" "github.com/spf13/viper" "log" ) var ( // config file name configName = "apsClient" // config file paths configPaths = []string{ "./", "../", "./conf", "../conf", } ) type ( System struct { Env string // 环境值 develop test public Port int // 端口 DbType string // 数据库类型 UseMultipoint bool // 多点登录拦截 UseRedis bool // 使用redis LimitCountIP int LimitTimeIP int RouterPrefix string // 路由前缀 SudoPassword string // sudo密码 DeviceId string //设备id } Etcd struct { Endpoints []string // etcd地址 DialTimeout int // etcd超时时间 KeyPrefix string // etcd key前缀 Tls Tls // etcd tls配置 } Tls struct { CertFile string // 证书文件 KeyFile string // 私钥文件 CaFile string // ca文件 } Rancher struct { Url string // rancher地址 Token string // rancher token } K8s struct { Image string // k8s deployment 镜像 DBHost string // db host ALHost string // al host Host string // host IP string // tmp ip } Services struct { ApsServer string } nsqConf struct { NodeId string NsqdAddr string NsqlookupdAddr string } config struct { // 系统配置 System System // 日志 Log logx.Conf // mysql配置 Mysql mysqlx.Conf // mysql配置 Sqlite sqlitex.Conf // redis配置 Redis redisx.Conf // etcd配置 Etcd Etcd // rancher配置 Rancher Rancher // k8s配置 K8s K8s //Services Address Services Services NsqConf nsqConf } ) var ( Conf config Viper *viper.Viper ) func init() { Viper = viper.New() Viper.SetConfigName(configName) for _, path := range configPaths { Viper.AddConfigPath(path) } if err := Viper.ReadInConfig(); err != nil { log.Fatalf("ReadInConfig err:%v", err) } if err := Viper.Unmarshal(&Conf); err != nil { log.Fatalf("Unmarshal err:%v", err) } ShowConfig() } // WriteConfig 回写配置 func (c *config) WriteConfig() error { cs := convertx.StructToMap(Conf) for k, v := range cs { Viper.Set(k, v) } return Viper.WriteConfig() } func ShowConfig() { log.Println("......................................................") log.Printf(" System: %+v", Conf.System) log.Printf(" Log: %+v", Conf.Log) log.Printf(" Mysql: %+v", Conf.Mysql) log.Printf(" etcd: %+v", Conf.Etcd) log.Printf(" rancher: %+v", Conf.Rancher) log.Printf(" k8s: %+v", Conf.K8s) log.Println("......................................................") }