-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
62 lines (50 loc) · 1.02 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
package config
import (
"fmt"
"sync"
)
var manager *Manager
type Manager struct {
mutex sync.RWMutex
mapKeyToConfig map[KeyType]Config
}
func init() {
manager = &Manager{
mapKeyToConfig: make(map[KeyType]Config),
}
}
type KeyType string
const (
Common KeyType = "common"
AIPaper KeyType = "aipaper"
)
func Register(key KeyType, mapEnvToConfig map[EnvType]Param, env EnvType) error {
param, ok := mapEnvToConfig[env]
if !ok {
return fmt.Errorf("invalid env:%v", env)
}
var cfg Config
var err error
cfg, err = NewApolloClient(
WithAppID(param.APPID),
WithCluster(param.Cluster),
WithIP(param.IP),
WithNamespace(param.Namespace),
WithIsBackupConfig(param.IsBackupConfig),
WithSecret(param.Secret),
)
if err != nil {
return err
}
manager.mutex.Lock()
defer manager.mutex.Unlock()
manager.mapKeyToConfig[key] = cfg
return nil
}
func Get(key KeyType) (Config, error) {
cfg, ok := manager.mapKeyToConfig[key]
if !ok {
return nil, fmt.Errorf("key:%v not exist", key)
}
return cfg, nil
}