/
config.go
62 lines (51 loc) · 1.5 KB
/
config.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// description:
// @author renshiwei
// Date: 2022/10/5 17:33
package config
import (
"bytes"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
func InitConfig(configFile ...string) {
// viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv() // read in environment variables that match
viper.SetConfigType("yaml")
assetStr := "../conf/config-default.yaml"
defaultConfig, err := Asset(assetStr)
if err != nil {
panic(fmt.Errorf("conf.Asset default config file: %s err:%+v\n", assetStr, err))
}
if err := viper.ReadConfig(bytes.NewBuffer(defaultConfig)); err != nil {
panic(fmt.Errorf("Fatal error default config file: %s err:%+v\n", defaultConfig, err))
}
// viper.SetConfigFile("../conf/config-default.yaml")
//
// if err := viper.ReadInConfig(); err != nil {
// panic(fmt.Errorf("Fatal error default config file: %s err:%+v\n", "defaultConfig", err))
// }
if len(configFile) > 0 {
for _, s := range configFile {
if s != "" {
viper.SetConfigFile(s)
}
}
if configFile[0] != "" {
err := viper.MergeInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file err:%+v\n", err))
}
viper.WatchConfig()
}
}
if err := viper.Unmarshal(&GlobalConfig); err != nil {
panic(fmt.Errorf("read config file err. file: %s %s", "defaultConfig", configFile))
}
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("config file changed:", e.Name)
if err := viper.Unmarshal(&GlobalConfig); err != nil {
fmt.Println(err)
}
})
}