package config
|
|
import (
|
"fmt"
|
|
"basic.com/valib/logger.git"
|
"github.com/fsnotify/fsnotify"
|
"github.com/spf13/viper"
|
)
|
|
type mainConfig struct {
|
ServePort int `mapstructure:"servePort"`
|
WorkerNum int `mapstructure:"workerNum"`
|
MysqlAddr string `mapstructure:"mysqlAddr"`
|
Username string `mapstructure:"username"`
|
Password string `mapstructure:"password"`
|
Database string `mapstructure:"database"`
|
PersonTable string `mapstructure:"personTable"`
|
}
|
|
var MainConf = &mainConfig{}
|
|
type logConfig struct {
|
Path string `mapstructure:"path"` //日志存储路径
|
Level int `mapstructure:"level"` //日志等级
|
MaxSize int `mapstructure:"maxSize"` //日志文件大小上限
|
MaxBackups int `mapstructure:"maxBackups"` //日志压缩包个数
|
MaxAge int `mapstructure:"maxAge"` //保留压缩包天数
|
}
|
|
var LogConf = &logConfig{}
|
|
func Init() error {
|
var err error
|
v := viper.New()
|
v.SetConfigType("yaml")
|
v.SetConfigName("compare")
|
v.AddConfigPath("./")
|
v.AddConfigPath("./config/")
|
v.AddConfigPath("../config/")
|
err = v.ReadInConfig()
|
if err != nil {
|
fmt.Printf("error on parsing configuration file, %s, config file compare.yaml\n", err.Error())
|
return err
|
}
|
|
read2Conf(v)
|
v.WatchConfig()
|
v.OnConfigChange(func(in fsnotify.Event) {
|
read2Conf(v)
|
})
|
|
return nil
|
}
|
|
func read2Conf(v *viper.Viper) {
|
v.UnmarshalKey("main", MainConf)
|
v.UnmarshalKey("log", LogConf)
|
|
logger.SetLevel(LogConf.Level)
|
|
if MainConf.WorkerNum == 0 {
|
MainConf.WorkerNum = 30
|
}
|
}
|