-
Notifications
You must be signed in to change notification settings - Fork 42
/
default_viper_configuration.go
177 lines (152 loc) 路 4.34 KB
/
default_viper_configuration.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package abstractions
import (
"flag"
"github.com/jinzhu/copier"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/yoyofx/yoyogo/abstractions/xlog"
"github.com/yoyofx/yoyogo/utils"
"path"
"sync"
)
type Configuration struct {
context *ConfigurationContext
configMap map[string]interface{}
gRWLock *sync.RWMutex
config *viper.Viper
log xlog.ILogger
}
func NewConfiguration(configContext *ConfigurationContext) *Configuration {
log := xlog.GetXLogger("Configuration")
log.SetCustomLogFormat(nil)
defaultConfig := viper.New()
if configContext.enableEnv {
defaultConfig.AutomaticEnv()
defaultConfig.SetEnvPrefix("YYG")
}
if configContext.enableFlag {
flag.String("app", "", "application name")
flag.String("port", "", "application port")
flag.String("profile", configContext.profile, "application profile")
flag.String("f", "", "config file path")
flag.String("conf", ".", "config dir")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
_ = defaultConfig.BindPFlags(pflag.CommandLine)
}
if pf := defaultConfig.GetString("profile"); pf != "" {
configContext.profile = pf
}
if cf := defaultConfig.GetString("conf"); cf != "" {
configContext.configDir = cf
}
if configFile := defaultConfig.GetString("f"); configFile != "" {
configContext.configFile = configFile
}
configFilePath := configContext.configFile
if configFilePath == "" {
configName := configContext.configName + "_" + configContext.profile
configFilePath = path.Join(configContext.configDir, configName+"."+configContext.ConfigType)
exists, _ := utils.PathExists(configFilePath)
if !exists {
configName = configContext.configName
}
defaultConfig.AddConfigPath(configContext.configDir)
defaultConfig.SetConfigName(configName)
defaultConfig.SetConfigType(configContext.ConfigType)
} else {
defaultConfig.SetConfigFile(configFilePath)
}
if err := defaultConfig.ReadInConfig(); err != nil {
panic(err)
return nil
}
log.Debug(configFilePath)
configuration := &Configuration{
context: configContext,
config: defaultConfig,
gRWLock: new(sync.RWMutex),
configMap: make(map[string]interface{}),
log: log,
}
if configContext.EnableRemote {
defaultConfig = configContext.RemoteProvider.GetProvider(defaultConfig)
if defaultConfig.ConfigFileUsed() == "" {
_ = defaultConfig.BindPFlags(pflag.CommandLine)
//remote config
configuration.config = defaultConfig
configuration.OnWatchRemoteConfigChanged()
log.Info("remote config is ready , on changed notify listening ......")
} else {
log.Error("remote config is not ready , switch local.")
}
}
return configuration
}
func (c *Configuration) OnWatchRemoteConfigChanged() {
respChan := c.context.RemoteProvider.WatchRemoteConfigOnChannel(c.config)
go func(rc <-chan bool) {
for {
<-rc
c.RefreshAll()
c.log.Info("sync remote config")
}
}(respChan)
}
func (c *Configuration) Get(name string) interface{} {
return c.config.Get(name)
}
func (c *Configuration) GetString(name string) string {
return c.config.GetString(name)
}
func (c *Configuration) GetBool(name string) bool {
return c.config.GetBool(name)
}
func (c *Configuration) GetInt(name string) int {
return c.config.GetInt(name)
}
func (c *Configuration) GetSection(name string) IConfiguration {
section := c.config.Sub(name)
if section != nil {
return &Configuration{config: section}
}
return nil
}
func (c *Configuration) Unmarshal(obj interface{}) {
err := c.config.Unmarshal(obj)
if err != nil {
c.log.Error("unmarshal config is failed, err:", err)
}
}
func (c *Configuration) GetProfile() string {
return c.context.profile
}
func (c *Configuration) GetConfDir() string {
return c.context.configDir
}
func (c *Configuration) GetConfigObject(configTag string, configObject interface{}) {
c.gRWLock.RLock()
object := c.configMap[configTag]
c.gRWLock.RUnlock()
if object == nil {
// need lock
Section := c.GetSection(configTag)
Section.Unmarshal(configObject)
object = configObject
c.gRWLock.Lock()
c.configMap[configTag] = object
c.gRWLock.Unlock()
} else {
_ = copier.Copy(configObject, object)
}
}
func (c *Configuration) RefreshAll() {
c.gRWLock.Lock()
c.configMap = make(map[string]interface{})
c.gRWLock.Unlock()
}
func (c *Configuration) RefreshBy(name string) {
c.gRWLock.Lock()
delete(c.configMap, name)
c.gRWLock.Unlock()
}