-
Notifications
You must be signed in to change notification settings - Fork 13
/
config.go
81 lines (69 loc) · 1.58 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package core
import (
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/panco95/go-garden/core/log"
"github.com/spf13/viper"
)
type routeCfg struct {
Type string
Path string
Limiter string
Fusing string
Timeout int
}
type serviceCfg struct {
Debug bool
ServiceName string
ServiceIp string
HttpOut bool
HttpPort string
AllowCors bool
RpcOut bool
RpcPort string
CallKey string
CallRetry string
EtcdKey string
EtcdAddress []string
TracerDrive string
ZipkinAddress string
JaegerAddress string
PushGatewayAddress string
}
type cfg struct {
Service serviceCfg
Routes map[string]map[string]routeCfg
RuntimePath string
ConfigsPath string
}
func (g *Garden) GetCfg() cfg {
return g.cfg
}
func (g *Garden) bootConfig(fileType string) {
viper.AddConfigPath(g.cfg.ConfigsPath)
viper.SetConfigType(fileType)
viper.SetConfigName("config")
if err := viper.ReadInConfig(); err != nil {
log.Fatal("config", err)
}
viper.SetConfigName("routes")
if err := viper.MergeInConfig(); err != nil {
log.Fatal("config", err)
}
g.unmarshalConfig()
// watch config file 'routes.yml' change
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
filename := filepath.Base(e.Name)
if strings.Compare(filename, "routes.yml") == 0 {
g.unmarshalConfig()
g.sendRoutes()
}
})
}
func (g *Garden) unmarshalConfig() {
if err := viper.Unmarshal(&g.cfg); err != nil {
log.Error("config", err)
}
}