package config import ( "basic.com/valib/logger.git" "github.com/spf13/viper" "log" ) type LogConfig struct { Path string `mapstructure:"path"` //日志存储路径 Level int `mapstructure:"level"` //日志等级 MaxSize int `mapstructure:"maxSize"` //日志文件大小上限 MaxBackups int `mapstructure:"maxBackups"` //日志压缩包个数 MaxAge int `mapstructure:"maxAge"` //保留压缩包天数 } type database struct { Driver string `mapstructure: "driver"` Host string `mapstructure: "host"` Port string `mapstructure: "port"` Name string `mapstructure: "name"` Username string `mapstructure: "username"` Password string `mapstructure: "password"` } type elastic struct { Host string `mapstructure: "host"` Port string `mapstructure: "port"` Index string `mapstructure: "index"` Type string `mapstructure: "type"` DocumentSize int `mapstructure: "documentSize"` TopHitsSize int `mapstructure: "topHitsSize"` CameraSize int `mapstructure: "cameraSize"` TimeInterval int `mapstructure: "timeInterval"` } type app struct { Name string `mapstructure: "Annotation Service Application"` Port string `mapstructure: "port"` LogLevel string `mapstructure: "logLevel"` } type api struct { Host string `mapstructure: "host"` Port string `mapstructure: "port"` CsTimes int `mapstructure:"csTimes"` CsHours int `mapstructure:"csHours"` AInterval int `mapstructure:"aInterval"` } var LogConf = &LogConfig{} var DataBase = &database{} var Elastic = &elastic{} var App = &app{} var Api = &api{} var LogBasePath string var LogLevel int func Init(env string) { var err error viper.SetConfigType("yaml") viper.SetConfigName(env) viper.AddConfigPath("config") err = viper.ReadInConfig() if err != nil { log.Fatal("error on parsing configuration file", err) } viper.UnmarshalKey("elastic", Elastic) viper.UnmarshalKey("database", DataBase) viper.UnmarshalKey("app", App) viper.UnmarshalKey("api", Api) viper.UnmarshalKey("log", LogConf) logger.SetLevel(LogConf.Level) if viper.GetString("LogBasePath") != "" { LogBasePath = viper.GetString("LogBasePath") } else { LogBasePath = "./logger/" } if viper.IsSet("LogLevel") && viper.GetInt("LogLevel") >= logger.PanicLevel && viper.GetInt("LogLevel") <= logger.DebugLevel { LogLevel = viper.GetInt("LogLevel") } else { LogLevel = logger.DebugLevel } }