package conf import ( "aps_crm/pkg/convertx" "aps_crm/pkg/logx" "aps_crm/pkg/mysqlx" "aps_crm/pkg/redisx" "flag" "github.com/spf13/viper" "log" ) var ( // config file name configName = "aps-admin" // config file paths configPaths = []string{ "./", "../", "./conf", "../conf", } ) type ( Captcha struct { KeyLong int // 验证码长度 ImgWidth int // 验证码宽度 ImgHeight int // 验证码高度 OpenCaptcha int // 防爆破验证码开启此数,0代表每次登录都需要验证码,其他数字代表错误密码次数,如3代表错误三次后出现验证码 OpenCaptchaTimeOut int // 防爆破验证码超时时间,单位:s(秒) } JWT struct { SigningKey string // jwt签名 ExpiresTime string // 过期时间 BufferTime string // 缓冲时间 Issuer string // 签发者 } 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密码 } config struct { // 系统配置 System System // 日志 Log logx.Conf // mysql配置 Mysql mysqlx.Conf // redis配置 Redis redisx.Conf // 验证码 Captcha Captcha // JWT配置 JWT JWT } ) var ( Conf config Viper *viper.Viper ) func init() { var configFile string flag.StringVar(&configFile, "config", "", "config file in json") flag.Parse() if len(configFile) > 0 { configName = configFile } 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(" Captcha: %+v", Conf.Captcha) log.Printf(" JWT: %+v", Conf.JWT) log.Println("......................................................") }