-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
328 lines (278 loc) · 7.82 KB
/
manager.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package config
// Imports needed list
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"log"
"os"
"sync"
"time"
)
// Mark: manager
// Manager object
type manager struct {
modules map[string]*ViperWrapper
modulesStatus map[string]bool
configBasePath string
configMode string
configRemoteAddress string
configRemoteInfra string
configRemoteDuration int64
quitCh chan bool
}
// MARK: Module variables
var providerInstance *manager = nil
var once sync.Once
// MARK: Module Initializer
func init() {
log.Println("Initializing Config Provider ...")
}
// MARK: Private Methods
// constructor - Constructor -> It initializes the config configuration params
func constructor(configBasePath string, configInitialMode string, configEnvPrefix string) error {
log.Println("Config Manager Initializer ...")
providerInstance.configMode = configInitialMode
providerInstance.configBasePath = configBasePath
viper.SetEnvPrefix(configEnvPrefix)
err := viper.BindEnv("mode")
if err != nil {
return err
}
err = viper.BindEnv("name")
if err != nil {
return err
}
err = viper.BindEnv("config_remote_addr")
if err != nil {
return err
}
err = viper.BindEnv("config_remote_infra")
if err != nil {
return err
}
err = viper.BindEnv("config_remote_duration")
if err != nil {
return err
}
mode := viper.Get("mode")
if mode != nil {
providerInstance.configMode = mode.(string)
}
viper.AddConfigPath(fmt.Sprintf("%s/configs/%s/", providerInstance.configBasePath, providerInstance.configMode))
viper.SetConfigName("base")
err = viper.ReadInConfig()
if err != nil {
return err
}
// Load all modules
configRemoteAddr := viper.GetString("config_remote_addr")
configRemoteInfra := viper.GetString("config_remote_infra")
configRemoteDuration := viper.GetInt64("config_remote_duration")
providerInstance.configRemoteInfra = configRemoteInfra
providerInstance.configRemoteAddress = configRemoteAddr
providerInstance.configRemoteDuration = configRemoteDuration
providerInstance.loadModules()
log.Printf("Read Base `%s` Configs", viper.GetString("name"))
mustWatched := viper.GetBool("config_must_watched")
if mustWatched {
viper.WatchConfig()
viper.OnConfigChange(func(in fsnotify.Event) {
log.Println("Configs Changed: ", in.Name)
})
}
return nil
}
// loadModules - Loads All Modules That is configured in "init" config file
func (p *manager) loadModules() {
log.Println("Load All Modules Config ...")
modules := viper.Get("modules")
for _, item2 := range modules.([]interface{}) {
item := item2.(map[string]interface{})
name := item["name"].(string)
w := &ViperWrapper{
ConfigPath: []string{fmt.Sprintf("%s/configs/%s/", p.configBasePath, p.configMode)},
ConfigName: item["name"].(string),
ConfigResourcePlace: item["type"].(string),
}
err := w.Load()
if err == nil {
p.modules[name] = w
p.modulesStatus[name] = true
} else {
p.modulesStatus[name] = false
}
}
//else if src == "remote" {
// // Start a goroutine
// for _, item := range modules {
// w := &ViperWrapper{
// ConfigName: item,
// }
//
// p.modules[item] = w
// p.modulesStatus[item] = false
// }
//
// // start remote loader as go routines
// go p.remoteConfigLoader()
//}
}
// MARK: Public Methods
// CreateManager - Create a new manager instance
func CreateManager(configBasePath string, configInitialMode string, configEnvPrefix string) error {
// once used for prevent race condition and manage critical section.
if providerInstance == nil {
var err error
once.Do(func() {
providerInstance = &manager{
modules: make(map[string]*ViperWrapper),
modulesStatus: make(map[string]bool),
quitCh: make(chan bool),
}
for item := range providerInstance.modulesStatus {
providerInstance.modulesStatus[item] = false
}
err = constructor(configBasePath, configInitialMode, configEnvPrefix)
})
return err
}
return nil
}
func GetManager() *manager {
return providerInstance
}
// GetConfigWrapper - returns Config Wrapper based on name
func (p *manager) GetConfigWrapper(category string) (*ViperWrapper, error) {
if val, ok := p.modules[category]; ok {
return val, nil
}
return nil, NewCategoryNotExistErr(category, nil)
}
// GetName - returns service instance name based on config
func (p *manager) GetName() string {
return viper.GetString("name")
}
// GetOperationType - returns operation type which could be `dev`, `prod`
func (p *manager) GetOperationType() string {
return p.configMode
}
// GetHostName - returns hostname based on config
func (p *manager) GetHostName() string {
return os.Getenv(fmt.Sprintf("%s_HOSTNAME", p.GetName()))
}
// Get - get value of the key in specific category
func (p *manager) Get(category string, name string) (interface{}, error) {
if val, ok := p.modules[category]; ok {
result, exist := val.Get(name, false)
if exist {
return result, nil
}
return nil, NewKeyNotExistErr(name, category, nil)
}
return nil, NewCategoryNotExistErr(name, nil)
}
// Set - set value in category by specified key.
func (p *manager) Set(category string, name string, value interface{}) error {
if val, ok := p.modules[category]; ok {
return val.Set(name, value, false)
}
return NewCategoryNotExistErr(category, nil)
}
// StopLoader - stop remote loader
func (p *manager) StopLoader() {
//if p.ConfigSrc == "remote" {
// p.quitCh <- true
//}
}
// IsInitialized - iterate over all config wrappers and see all initialised correctly
func (p *manager) IsInitialized() bool {
flag := true
for _, value := range p.modulesStatus {
if value == false {
flag = false
break
}
}
return flag
}
// GetAllInitializedModuleList - get list of names that initialized truly
func (p *manager) GetAllInitializedModuleList() []string {
var result []string
for key, val := range p.modulesStatus {
if val {
result = append(result, key)
}
}
return result
}
// remoteConfigLoader - get configs from remote
func (p *manager) remoteConfigLoader() {
for {
select {
case <-p.quitCh:
return
default:
for key := range p.modulesStatus {
data, err := p.remoteConfigLoad(key)
if err == nil {
err = p.modules[key].LoadFromRemote(data)
if err == nil {
p.modulesStatus[key] = true
} else {
log.Println(err.Error())
}
} else {
log.Println(err.Error())
}
}
}
time.Sleep(time.Duration(p.configRemoteDuration) * time.Second)
}
}
// remoteConfigLoad
func (p *manager) remoteConfigLoad(key string) ([]byte, error) {
//if p.ConfigRemoteAddress != "" {
// if p.ConfigRemoteInfra == "grpc" {
// conn, err := grpc.Dial(p.ConfigRemoteAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
// if err != nil {
// return nil, protoerror.GrpcDialError{Addr: p.ConfigRemoteAddress, Err: err}
// }
// defer conn.Close()
//
// localContext, cancel := context.WithTimeout(context.Background(), 2*time.Second)
// defer cancel()
//
// c := api.NewHelpClient(conn)
// response, err := c.GetServiceConfig(localContext, &api.ServiceConfigRequest{
// Section: key,
// Hostname: p.GetHostName(),
// ConstructorId: api.ConstructorId_V1901,
// ServiceName: p.GetName(),
// MsgId: timestamppb.Now()})
// if err != nil {
// return nil, RemoteResponseError{Err: err}
// }
//
// return []byte(response.Data), nil
// }
//}
return nil, NewRemoteLoadErr(key, nil)
}
// ManualLoadConfig - load manual config from the path and add to the current dict
func (p *manager) ManualLoadConfig(configBasePath string, configName string) error {
w := &ViperWrapper{
ConfigPath: []string{configBasePath},
ConfigName: configName,
ConfigResourcePlace: "",
}
err := w.Load()
if err == nil {
p.modules[configName] = w
p.modulesStatus[configName] = true
} else {
p.modulesStatus[configName] = false
return err
}
return nil
}