package setting import ( "fmt" "strconv" "time" "github.com/fsnotify/fsnotify" "github.com/spf13/viper" ) var Conf = new(Config) type Config struct { ServerSetting *Server `mapstructure:"server"` AppSetting *App `mapstructure:"app"` DatabaseSetting *Database `mapstructure:"database"` Redis *RedisConfig `mapstructure:"redis"` LogSetting *Log `mapstructure:"log"` Sms *SmsInternationalConfig `mapstructure:"sms"` UploadImage *UploadImage `mapstructure:"uploadimage"` Oss *Oss `mapstructure:"oss"` AliCloud *AliCloud `mapstructure:"alicloud"` Esign *Esign `mapstructure:"esign"` JwtSecret string `mapstructure:"jwtsecret"` DesKey string `mapstructure:"deskey"` //SmsInternational *SmsInternationalConfig `mapstructure:"newsms"` //GateWay *GateWayDetail `mapstructure:"gateway"` //GateWayDev *GateWayDevDetail `mapstructure:"gatewaydev"` //Mongo *MongoConfig `mapstructure:"mongo"` //PayUrl *PayUrlDetail `mapstructure:"payurl"` } type App struct { Name string `mapstructure:"name"` Version string `mapstructure:"version"` TimeFormat string `mapstructure:"timeformat""` } type Log struct { LogSavePath string `mapstructure:"logsavepath"` LogSaveName string `mapstructure:"logsavename"` LogFileExt string `mapstructure:"logfileext"` Level string `mapstructure:"level"` MaxSize int `mapstructure:"max_size"` MaxAge int `mapstructure:"max_age"` MaxBackups int `mapstructure:"max_backups"` } // Server 服务 type Server struct { RunMode string `mapstructure:"runmode"` HttpPort int `mapstructure:"httpport"` ReadTimeout time.Duration `mapstructure:"readtimeout"` WriteTimeout time.Duration `mapstructure:"writetimeout"` } // Database DB type Database struct { Type string `mapstructure:"type""` ShopDB string `mapstructure:"shopdb"` SystemDB string `mapstructure:"systemdb"` UserDB string `mapstructure:"userdb"` SecondDB string `mapstructure:"seconddb"` AccountDB string `mapstructure:"accountdb"` PayDB string `mapstructure:"paydb"` //MaxOpenConns int `mapstructure:"max_open_conns"` //MaxIdleConns int `mapstructure:"max_idle_conns"` //Host string `mapstructure:"host"` //User string `mapstructure:"user""` //Password string `mapstructure:"password""` //Port int `mapstructure:"port"` //DB string `mapstructure:"dbname"` } // RedisConfig redis type RedisConfig struct { Host string `mapstructure:"host"` Password string `mapstructure:"password"` Port int `mapstructure:"port"` DB int `mapstructure:"db"` PoolSize int `mapstructure:"pool_size"` MinIdleConns int `mapstructure:"min_idle_conns"` } // MongoConfig mongo设置 //type MongoConfig struct { // DBUrl string `mapstructure:"dburl"` // MaxOpenConns uint64 `mapstructure:"max_open_conns"` //} // SmsInternationalConfig 国际化短信 type SmsInternationalConfig struct { SmsKey string `mapstructure:"smskey"` SendSmsUrl string `mapstructure:"sendsmsurl"` SmsContent string `mapstructure:"smscontent"` } // //// GateWayDetail 网关 //type GateWayDetail struct { // URL string `mapstructure:"url"` // Port string `mapstructure:"port"` //} // //// GateWayDevDetail 网关 //type GateWayDevDetail struct { // URL string `mapstructure:"url"` // Port string `mapstructure:"port"` //} // //// PayUrlDetail 网关 //type PayUrlDetail struct { // CheckOrder string `mapstructure:"checkorder"` // OrderState string `mapstructure:"orderstate"` //} type UploadImage struct { UploadDir string `mapstructure:"upload_dir"` MaxFileSize int `mapstructure:"max_file_size"` ImageTypes string `mapstructure:"image_types""` AcceptFileTypes string `mapstructure:"accept_file_types""` } type Oss struct { OssUrl string `mapstructure:"oss_url"` AccessKeyID string `mapstructure:"accesskeyid"` AccessKeySecret string `mapstructure:"accesskeysecret""` EndPoint string `mapstructure:"endpoint""` Bucket string `mapstructure:"bucket""` } type AliCloud struct { AppCode string `mapstructure:"appcode"` TimeDuration int `mapstructure:"time_duration"` } //e签宝 type Esign struct { ProjectId string `mapstructure:"project_id"` ProjectSecret string `mapstructure:"project_secret"` OrganAuthUrl string `mapstructure:"organ_auth_url"` InfoAuthUrl string `mapstructure:"info_auth_url"` } // Init 支持热修改的viper设置 func Init() error { viper.SetConfigFile("conf/dev/config.yaml") // 指定配置文件路径 err := viper.ReadInConfig() // 读取配置信息 if err != nil { // 读取配置信息失败 fmt.Printf("viper.ReadInConfig failed, new_error:%v\n", err) return err } // 将读取的配置信息保存至全局变量Conf if err = viper.Unmarshal(Conf); err != nil { fmt.Printf("viper.Unmarshal failed, new_error:%v\n", err) } // 监控配置文件变化 viper.WatchConfig() // 注意!!!配置文件发生变化后要同步到全局变量Conf viper.OnConfigChange(func(in fsnotify.Event) { fmt.Println("配置文件被修改啦...") if err = viper.Unmarshal(Conf); err != nil { fmt.Printf("viper.Unmarshal failed, new_error:%v\n", err) } //fmt.Println(Conf) }) return err } func GetPort() string { s := strconv.Itoa(Conf.ServerSetting.HttpPort) return ":" + s }