-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
45 lines (34 loc) · 974 Bytes
/
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
/**
* Created GoLand.
* User: liyw<634482545@qq.com>
* Date: 2023-08-10
* File: yaml_config.go
* Desc: yaml config
*/
package config
import (
"github.com/fsnotify/fsnotify"
"github.com/go-mumu/cs-go/library/common/flags"
"github.com/go-mumu/cs-go/library/log"
"github.com/spf13/viper"
_ "github.com/spf13/viper/remote"
)
var V *viper.Viper
func init() {
v := viper.New()
v.SetConfigType("yaml")
if err := v.AddRemoteProvider("consul", flags.ConsulAddr, flags.ConsulConfigPath); err != nil {
log.Cli.Error("add consul remote provider failed", "error", err)
}
if err := v.ReadRemoteConfig(); err != nil {
log.Cli.Error("read consul remote config failed", "error", err)
}
v.OnConfigChange(func(e fsnotify.Event) {
log.Cli.Info("config path is change:", e.Name, e.String())
})
if err := v.WatchRemoteConfigOnChannel(); err != nil {
log.Cli.Error("watch remote config error", "error", err)
}
log.Cli.Info("init config success.")
V = v
}