/
conf.go
349 lines (303 loc) · 8.74 KB
/
conf.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package settings
import (
"bytes"
"io/ioutil"
"os"
"strings"
"time"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
mylog "github.com/captainlee1024/go-gateway/internal/gateway/log"
)
// 配置信息全局变量
var (
ConfBase = new(BaseConfig)
//ConfMySQL = new(MySQLConfig)
ConfMySQLMap = new(MySQLMapConfig)
//ConfRedis = new(RedisConfig)
ConfRedisMap = new(RedisMapConfig)
ViperConfMap map[string]*viper.Viper
)
// BaseConfig 应用程序配置信息
type BaseConfig struct {
Name string `mapstructure:"name"`
Mode string `mapstructure:"mode"`
Version string `mapstructure:"version"`
StartTime string `mapstructure:"start_time"`
TimeLocation string `mapstructure:"time_location"`
MachineID int64 `mapstructure:"machine_id"`
Port int `mapstructure:"port"`
*LogConfig `mapstructure:"log"`
*HttpConfig `mapstructure:"http"`
*SessionConfig `mapstructure:"session"`
*SwagConfig `mapstructure:"swagger"`
*ClusterConfig `mapstructure:"cluster"`
}
// LogConfig Zap 配置信息
type LogConfig struct {
Level string `mapstructure:"level"`
Filename string `mapstructure:"filename"`
MaxSize int `mapstructure:"max_size"`
MaxAge int `mapstructure:"max_age"`
MaxBackups int `mapstructure:"max_backup"`
}
// HttpConfig 是项目服务参数信息
type HttpConfig struct {
ReadTime int `mapstructure:"read_time"`
WriteTime int `mapstructure:"write_time"`
MaxHeaderBytes int `mapstructure:"max_header_bytes"`
AllowIP []string `mapstructure:"allow_ip"`
}
// SessionConfig 是 session 存储配置信息
type SessionConfig struct {
RedisServer string `mapstructure:"redis_server"`
RedisPassword string `mapstructure:"redis_password"`
}
// SwagConfig 是 swagger 文档配置信息
type SwagConfig struct {
Title string `mapstructure:"title"`
Describe string `mapstructure:"desc"`
Host string `mapstructure:"host"`
BasePath string `mapstructure:"base_path"`
}
type ClusterConfig struct {
ClusterIP string `mapstructure:"cluster_ip"`
ClusterPost string `mapstructure:"cluster_port"`
ClusterSslPort string `mapstructure:"cluster_ssl_port"`
}
// MySQLMapConfig 数据库列表
type MySQLMapConfig struct {
List map[string]*MySQLConfig `mapstructure:"list"`
}
// MySQLConfig MySQL 配置信息
type MySQLConfig struct {
Host string `mapstructure:"host"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
DbName string `mapstructure:"dbname"`
Port int `mapstructure:"port"`
MaxOpenConns int `mapstructure:"max_open_conns"`
MaxIdleConns int `mapstructure:"max_idel_conns"`
}
// RedisMapConfig Redis 列表
type RedisMapConfig struct {
List map[string]*RedisConfig `mapstructure:"list"`
}
// RedisConfig Redis 配置信息
type RedisConfig struct {
Host string `mapstructure:"host"`
Password string `mapstructure:"password"`
Port int `mapstructure:"port"`
DB int `mapstructure:"db"`
PoolSize int `mapstructure:"pool_size"`
}
// GetBaseConf 获取基本配置信息
func GetBaseConf() *BaseConfig {
return ConfBase
}
// 读取基本配置
func initBaseConf(path string) (err error) {
err = ParseConfig(path, "base", ConfBase)
if err != nil {
return err
}
if ConfBase.Mode == "" {
ConfBase.Mode = "debug"
}
if ConfBase.TimeLocation == "" {
ConfBase.TimeLocation = "Asiz/Shanghai"
}
return
}
// initRedisConf 初始化 Redis 配置信息
func initRedisConf(path string, fileName string) (err error) {
err = ParseConfig(path, fileName, ConfRedisMap)
if err != nil {
return err
}
//ConfRedis = ConfRedisMap.List["default"]
return nil
}
// initMySQLConf 初始化数据库信息
func initMySQLConf(path string, fileName string) (err error) {
err = ParseConfig(path, fileName, ConfMySQLMap)
if err != nil {
return err
}
//ConfMySQL = ConfMySQLMap.List["default"]
return
}
// initLog 初始化 Zap logger
// 配置自己的 logger 并替换 zap 中定义的全局变量 logger
// func initLog(cfg *LogConfig, mode string) (err error) {
func initLog(cfg *LogConfig) (err error) {
writerSyncer := mylog.GetLogWriter(
cfg.Filename,
cfg.MaxSize,
cfg.MaxBackups,
cfg.MaxAge,
)
encoder := mylog.GetEncoder()
// 把 yaml 配置文件中的 string 类型的 level 配置,解析成 zap 中的 level 类型
var l = new(zapcore.Level)
//err = l.UnmarshalText([]byte(viper.GetString("log.level")))
err = l.UnmarshalText([]byte(viper.GetString(cfg.Level)))
if err != nil {
return
}
var core zapcore.Core
if cfg.Level == "debug" {
// 开发模式,输出日志到终端
consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
core = zapcore.NewTee( // 指定两个输出位置
// 第一个输出 和下面的配置一样,以 JSON 方式写入到日志文件里面
zapcore.NewCore(encoder, writerSyncer, l),
// 第二个输出
// consoleEncoder 指定console编码器
// zapcore.Lock(os.Stdout) 指定输出位置是标准输出,给它转换成符合条件的 WriteSyncer
// zapcore.DebugLevel 指定输出日志级别
zapcore.NewCore(consoleEncoder, zapcore.Lock(os.Stdout), zapcore.DebugLevel),
)
} else {
//
core = zapcore.NewCore(encoder, writerSyncer, zapcore.DebugLevel)
}
// 生成配置的 logger
mylog.Log = new(mylog.Logger)
mylog.Log.L = zap.New(core, zap.AddCaller())
return
}
// initViperConf 初始化配置文件
func initViperConf() error {
f, err := os.Open(ConfEnvPath + "/")
if err != nil {
return err
}
fileList, err := f.Readdir(1024)
if err != nil {
return err
}
for _, f0 := range fileList {
if !f0.IsDir() {
bts, err := ioutil.ReadFile(ConfEnvPath + "/" + f0.Name())
if err != nil {
return err
}
v := viper.New()
v.SetConfigType("yaml")
v.ReadConfig(bytes.NewBuffer(bts))
pathArr := strings.Split(f0.Name(), ".")
if ViperConfMap == nil {
ViperConfMap = make(map[string]*viper.Viper)
}
ViperConfMap[pathArr[0]] = v
}
}
// fmt.Printf("%v\n%v\n%v\n", ViperConfMap["base"],
// ViperConfMap["mysql"], ViperConfMap["redis"])
return nil
}
// GetStringConf 获取 string 类型配置信息
func GetStringConf(key string) string {
keys := strings.Split(key, ".")
if len(keys) < 2 {
return ""
}
v, ok := ViperConfMap[keys[0]]
if !ok {
return ""
}
confString := v.GetString(strings.Join(keys[1:len(keys)], "."))
return confString
}
// GetStringMapConf 获取 string 为 key 的 map 类型信息
func GetStringMapConf(key string) map[string]interface{} {
keys := strings.Split(key, ".")
if len(keys) < 2 {
return nil
}
v := ViperConfMap[keys[0]]
conf := v.GetStringMap(strings.Join(keys[1:len(keys)], "."))
return conf
}
// GetBoolConf 获取 bool 类型配置信息
func GetBoolConf(key string) bool {
keys := strings.Split(key, ".")
if len(keys) < 2 {
return false
}
v := ViperConfMap[keys[0]]
conf := v.GetBool(strings.Join(keys[1:len(keys)], "."))
return conf
}
// GetFloat64Conf 获取 float64 类型配置信息
func GetFloat64Conf(key string) float64 {
keys := strings.Split(key, ".")
if len(keys) < 2 {
return 0
}
v := ViperConfMap[keys[0]]
conf := v.GetFloat64(strings.Join(keys[1:len(keys)], "."))
return conf
}
// GetIntConf 获取 int 类型配置信息
func GetIntConf(key string) int {
keys := strings.Split(key, ".")
if len(keys) < 2 {
return 0
}
v := ViperConfMap[keys[0]]
conf := v.GetInt(strings.Join(keys[1:len(keys)], "."))
return conf
}
// GetStringMapStringConf 获取 map[string]string 类型配置信息
func GetStringMapStringConf(key string) map[string]string {
keys := strings.Split(key, ".")
if len(keys) < 2 {
return nil
}
v := ViperConfMap[keys[0]]
conf := v.GetStringMapString(strings.Join(keys[1:len(keys)], "."))
return conf
}
// GetStringSliceConf 获取 []string 类型配置信息
func GetStringSliceConf(key string) []string {
keys := strings.Split(key, ".")
if len(keys) < 2 {
return nil
}
v := ViperConfMap[keys[0]]
conf := v.GetStringSlice(strings.Join(keys[1:len(keys)], "."))
return conf
}
// GetTimeConf 获取 time.Time 类型配置信息
func GetTimeConf(key string) time.Time {
keys := strings.Split(key, ".")
if len(keys) < 2 {
return time.Now()
}
v := ViperConfMap[keys[0]]
conf := v.GetTime(strings.Join(keys[1:len(keys)], "."))
return conf
}
// GetDurationConf 获取时间阶段长度
func GetDurationConf(key string) time.Duration {
keys := strings.Split(key, ".")
if len(keys) < 2 {
return 0
}
v := ViperConfMap[keys[0]]
conf := v.GetDuration(strings.Join(keys[1:len(keys)], "."))
return conf
}
// IsSetConf 是否设置了key
func IsSetConf(key string) bool {
keys := strings.Split(key, ".")
if len(keys) < 2 {
return false
}
v := ViperConfMap[keys[0]]
conf := v.IsSet(strings.Join(keys[1:len(keys)], "."))
return conf
}