-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
64 lines (53 loc) · 1.1 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
package xconfig
func NewConfig(obj interface{}, opts ...ConfigOption) error {
cfg := &innerCfg{}
for _, opt := range opts {
if err := opt(cfg); err != nil {
return err
}
}
//解析配置对象
apss, err := newParser().parse(obj)
if err != nil {
return err
}
//启动apollo
for key, aps := range apss {
if cli, err := NewClient(
AppIp(key),
IP(cfg.ApolloIp),
Cluster(cfg.ApolloCluster),
ChangeListener(NewWatcher(aps, key))); err != nil {
return err
} else if err := cli.Init(aps); err != nil {
return err
}
}
return nil
}
type innerCfg struct {
ApolloIp string
ApolloCluster string
LocalConfig string
}
type ConfigOption func(cfg *innerCfg) error
func ApolloIp(ip string) ConfigOption {
return func(cfg *innerCfg) error {
cfg.ApolloIp = ip
return nil
}
}
func ApolloCluster(cluster string) ConfigOption {
return func(cfg *innerCfg) error {
if len(cluster) == 0 {
cluster = "default"
}
cfg.ApolloCluster = cluster
return nil
}
}
func LocalConfig(lc string) ConfigOption {
return func(cfg *innerCfg) error {
return loadLocalConfig(lc)
}
}