-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
170 lines (144 loc) · 3.92 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
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
package service
import (
"encoding/json"
"fmt"
"os"
)
type GoogleConfig struct {
Project string
LogName string
}
type BaseConfig struct {
ServiceName string
ServerType ServerType
ConfigName string
Version string
Google GoogleConfig
SentryDsn *string
}
type IBaseConfig interface {
GetServerType() ServerType
SetServerType(string)
GetVersionString() string
IsProductionServer() bool
}
func (c *BaseConfig) GetServerType() ServerType {
if c == nil {
return Development
} else {
return c.ServerType
}
}
func (c *BaseConfig) SetServerType(envServerType string) {
if c == nil {
return
}
switch envServerType {
case "production", "prod":
c.ServerType = Production
case "staging", "stage":
c.ServerType = Staging
case "livetest":
c.ServerType = LiveTest
case "uat":
c.ServerType = UAT
case "local":
c.ServerType = Local
default:
c.ServerType = Development
}
}
func (c *BaseConfig) GetVersionString() string {
if c == nil {
return ""
} else {
return c.Version + "-" + c.ConfigName
}
}
func (c *BaseConfig) IsProductionServer() bool {
switch c.ServerType {
case Production, LiveTest:
return true
}
return false
}
func ReadConfig(config interface{}, envServerType string, configPathBuilder func(string) string) error {
baseConfig, isBaseConfig := config.(IBaseConfig)
if isBaseConfig {
baseConfig.SetServerType(envServerType)
}
configPath := configPathBuilder("config.json")
var mergedConfig = make(map[string]interface{})
var overlayConfig = make(map[string]interface{})
// Read base config for merging
if err := readConfigPath(configPath, &mergedConfig); err != nil {
return err
}
// Merge specific configuration if applicable
var variantConfigPaths []string
switch baseConfig.GetServerType() {
case Staging:
variantConfigPaths = []string{"config-stage.json", "config-staging.json"}
case Production:
variantConfigPaths = []string{"config-pro.json", "config-prod.json", "config-production.json"}
case Development:
variantConfigPaths = []string{"config-dev.json", "config-development.json"}
case UAT:
variantConfigPaths = []string{"config-uat.json"}
case LiveTest:
variantConfigPaths = []string{"config-livetest.json"}
case Local:
variantConfigPaths = []string{"config-local.json"}
}
var configReadError error
for _, variantConfigFile := range variantConfigPaths {
variantConfigPath := configPathBuilder(variantConfigFile)
if _, err := os.Stat(variantConfigPath); os.IsNotExist(err) {
// Skip if missing
continue
}
if configReadError = readConfigPath(variantConfigPath, &overlayConfig); configReadError == nil {
break
}
}
if configReadError != nil {
return configReadError
}
// Merge an overlay configuration with the base configuration if present
if len(overlayConfig) > 0 {
for key, value := range overlayConfig {
if baseValue, ok := mergedConfig[key]; !ok {
// Missing base key, add it
mergedConfig[key] = value
} else if baseMap, isMapBase := baseValue.(map[string]interface{}); !isMapBase {
// Base config is not a map, overwrite with overlay value
mergedConfig[key] = value
} else if overlayMap, isMapOverlay := value.(map[string]interface{}); isMapBase && isMapOverlay {
// Merge base and overlay map
for subKey, subValue := range overlayMap {
baseMap[subKey] = subValue
}
} else {
// Base config is a map, overwrite with a non-map overlay value
mergedConfig[key] = value
}
}
}
// Read the merged configuration to config struct, requires translating back and forth from json
var mergedJson []byte
var err error
if mergedJson, err = json.Marshal(mergedConfig); err == nil {
err = json.Unmarshal(mergedJson, config)
}
return err
}
func readConfigPath(path string, config interface{}) error {
file, err := os.Open(path)
if err != nil {
err = fmt.Errorf("ReadConfig path error: %v", err)
return err
}
decoder := json.NewDecoder(file)
err = decoder.Decode(config)
return err
}