zhangzengfei
2024-02-23 b4f52495ea8c990bba30efcb3f36a6167cc85bab
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
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"`
}
 
var Options = &common{}
 
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.WatchConfig()
    viper.OnConfigChange(func(in fsnotify.Event) {
        viper.UnmarshalKey("common", Options)
    })
}