zhangzengfei
2024-04-08 56acb65a68fbe85e3ab8fc3b66e512eb4fcd8a30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package config
 
import (
    "github.com/fsnotify/fsnotify"
    "github.com/spf13/viper"
    "log"
)
 
type common struct {
    EsUrl     string `mapstructure:"esUrl"`
    ServerUrl string `mapstructure:"serverUrl"`
    OrgName   string `mapstructure:"orgName"`
    Interval  int    `mapstructure:"pushInterval"`
}
 
type logConfig struct {
    Path       string `mapstructure:"path"`       //日志存储路径
    Level      int    `mapstructure:"level"`      //日志等级
    MaxSize    int    `mapstructure:"maxSize"`    //日志文件大小上限
    MaxBackups int    `mapstructure:"maxBackups"` //日志压缩包个数
    MaxAge     int    `mapstructure:"maxAge"`     //保留压缩包天数
}
 
var Options = &common{}
var LogConf = &logConfig{}
 
func Init() {
    var err error
    viper.SetConfigType("yaml")
    viper.SetConfigName("esSync")
    viper.AddConfigPath("../config")
    viper.AddConfigPath("config")
    viper.AddConfigPath("")
    err = viper.ReadInConfig()
    if err != nil {
        log.Fatal("error on parsing configuration file", err)
    }
 
    viper.UnmarshalKey("common", Options)
    viper.UnmarshalKey("log", LogConf)
 
    viper.WatchConfig()
    viper.OnConfigChange(func(in fsnotify.Event) {
        viper.UnmarshalKey("common", Options)
    })
}