-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.go
212 lines (187 loc) · 5.15 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
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
package kuu
import (
"github.com/buger/jsonparser"
"io/ioutil"
"os"
"sync"
)
var (
configData []byte
configInst *Config
parsePathCache sync.Map
fromParamKeys = make(map[string]bool)
)
func parseKuuJSON(filePath ...string) (data []byte) {
var configFile string
if len(filePath) > 0 && filePath[0] != "" {
configFile = filePath[0]
}
if configFile == "" {
if v := os.Getenv("KUU_CONFIG"); v != "" {
configFile = v
}
}
if configFile == "" {
configFile = "kuu.json"
}
if _, err := os.Stat(configFile); os.IsNotExist(err) {
return
}
data, _ = ioutil.ReadFile(configFile)
return
}
type Config struct {
data []byte
}
func mergeConfig(r, n map[string]interface{}) map[string]interface{} {
if r == nil {
r = make(map[string]interface{})
}
if n == nil {
n = make(map[string]interface{})
}
for k, v := range n {
r[k] = v
}
return r
}
func C(newConfig ...map[string]interface{}) *Config {
if configInst == nil {
configData = parseKuuJSON()
configInst = &Config{data: configData}
}
if len(newConfig) > 0 {
src := make(map[string]interface{})
_ = json.Unmarshal(configData, &src)
src = mergeConfig(src, newConfig[0])
configData, _ = json.Marshal(src)
configInst.data = configData
}
return configInst
}
func ReloadC(jsonStr string) *Config {
if len(jsonStr) > 0 {
src := make(map[string]interface{})
_ = json.Unmarshal(configData, &src)
//src = mergeConfig(src, newConfig[0])
configData, _ = json.Marshal(src)
configInst.data = configData
}
return configInst
}
func (c *Config) Get(path string) (val []byte, exists bool) {
keys := ParseJSONPath(path)
if v, _, _, err := jsonparser.Get(c.data, keys...); err == nil {
exists = true
val = v
}
return
}
func (c *Config) Has(path string) bool {
keys := ParseJSONPath(path)
_, _, _, err := jsonparser.Get(c.data, keys...)
return err == nil
}
// GetInterface returns the value associated with the key.
func (c *Config) GetInterface(path string, out interface{}) {
keys := ParseJSONPath(path)
value, _, _, err := jsonparser.Get(c.data, keys...)
if err == nil {
_ = json.Unmarshal(value, out)
}
}
// GetString returns the value associated with the key as a string.
func (c *Config) GetString(path string) (s string) {
keys := ParseJSONPath(path)
s, _ = jsonparser.GetString(c.data, keys...)
return
}
// DefaultGetString returns the value associated with the key as a string.
func (c *Config) DefaultGetString(path string, defaultValue string) string {
keys := ParseJSONPath(path)
if v, err := jsonparser.GetString(c.data, keys...); err != nil {
return defaultValue
} else {
return v
}
}
// GetInt64 returns the value associated with the key as an integer.
func (c *Config) GetInt64(path string) int64 {
keys := ParseJSONPath(path)
value, _ := jsonparser.GetInt(c.data, keys...)
return value
}
// GetInt returns the value associated with the key as an integer.
func (c *Config) GetInt(path string) (i int) {
v := c.GetInt64(path)
return int(v)
}
// GetInt64 returns the value associated with the key as an integer.
func (c *Config) GetInt32(path string) int32 {
v := c.GetInt64(path)
return int32(v)
}
// DefaultGetInt returns the value associated with the key as a integer.
func (c *Config) DefaultGetInt(path string, defaultValue int) int {
keys := ParseJSONPath(path)
if v, err := jsonparser.GetInt(c.data, keys...); err != nil {
return defaultValue
} else {
return int(v)
}
}
// GetFloat64 returns the value associated with the key as a float64.
func (c *Config) GetFloat64(path string) (f64 float64) {
keys := ParseJSONPath(path)
f64, _ = jsonparser.GetFloat(c.data, keys...)
return
}
// GetFloat64 returns the value associated with the key as a float32.
func (c *Config) GetFloat32(path string) float32 {
v := c.GetFloat64(path)
return float32(v)
}
// DefaultGetInt returns the value associated with the key as a float64.
func (c *Config) DefaultGetFloat64(path string, defaultValue float64) float64 {
keys := ParseJSONPath(path)
if v, err := jsonparser.GetFloat(c.data, keys...); err != nil {
return defaultValue
} else {
return v
}
}
// GetBool returns the value associated with the key as a boolean.
func (c *Config) GetBool(path string) (b bool) {
keys := ParseJSONPath(path)
b, _ = jsonparser.GetBoolean(c.data, keys...)
return
}
// DefaultGetBool returns the value associated with the key as a boolean.
func (c *Config) DefaultGetBool(path string, defaultValue bool) bool {
keys := ParseJSONPath(path)
if v, err := jsonparser.GetBoolean(c.data, keys...); err != nil {
return defaultValue
} else {
return v
}
}
func (c *Config) LoadFromParams(keys ...string) {
// 缓存key用于监听当前应用需要的key
for _, key := range keys {
fromParamKeys[key] = true
}
var params []Param
DB().Model(&Param{}).Where("code in (?)", keys).Find(¶ms)
for _, param := range params {
INFO("Load Config From param: %s(%s)", param.Code, param.Name)
if param.Type == "json" {
DefaultCache.HSet(BuildKey("config", "params"), param.Code, param.Value)
var err error
c.data, err = jsonparser.Set(c.data, []byte(param.Value), "params", param.Code)
if err != nil {
ERROR("%s: 加载失败: %w", param.Code, err.Error())
continue
}
}
}
}