/
conf.go
executable file
·301 lines (273 loc) · 6.88 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
package lib
import (
"bytes"
"database/sql"
dlog "github.com/e421083458/golang_common/log"
"github.com/spf13/viper"
"gorm.io/gorm"
"io/ioutil"
"os"
"strings"
"time"
)
type BaseConf struct {
DebugMode string `mapstructure:"debug_mode"`
TimeLocation string `mapstructure:"time_location"`
Log LogConfig `mapstructure:"log"`
Base struct {
DebugMode string `mapstructure:"debug_mode"`
TimeLocation string `mapstructure:"time_location"`
} `mapstructure:"base"`
}
type LogConfFileWriter struct {
On bool `mapstructure:"on"`
LogPath string `mapstructure:"log_path"`
RotateLogPath string `mapstructure:"rotate_log_path"`
WfLogPath string `mapstructure:"wf_log_path"`
RotateWfLogPath string `mapstructure:"rotate_wf_log_path"`
}
type LogConfConsoleWriter struct {
On bool `mapstructure:"on"`
Color bool `mapstructure:"color"`
}
type LogConfig struct {
Level string `mapstructure:"log_level"`
FW LogConfFileWriter `mapstructure:"file_writer"`
CW LogConfConsoleWriter `mapstructure:"console_writer"`
}
type MysqlMapConf struct {
List map[string]*MySQLConf `mapstructure:"list"`
}
type MySQLConf struct {
DriverName string `mapstructure:"driver_name"`
DataSourceName string `mapstructure:"data_source_name"`
MaxOpenConn int `mapstructure:"max_open_conn"`
MaxIdleConn int `mapstructure:"max_idle_conn"`
MaxConnLifeTime int `mapstructure:"max_conn_life_time"`
}
type RedisMapConf struct {
List map[string]*RedisConf `mapstructure:"list"`
}
type RedisConf struct {
ProxyList []string `mapstructure:"proxy_list"`
Password string `mapstructure:"password"`
Db int `mapstructure:"db"`
ConnTimeout int `mapstructure:"conn_timeout"`
ReadTimeout int `mapstructure:"read_timeout"`
WriteTimeout int `mapstructure:"write_timeout"`
}
//全局变量
var ConfBase *BaseConf
var DBMapPool map[string]*sql.DB
var GORMMapPool map[string]*gorm.DB
var DBDefaultPool *sql.DB
var GORMDefaultPool *gorm.DB
var ConfRedis *RedisConf
var ConfRedisMap *RedisMapConf
var ViperConfMap map[string]*viper.Viper
//获取基本配置信息
func GetBaseConf() *BaseConf {
return ConfBase
}
func InitBaseConf(path string) error {
ConfBase = &BaseConf{}
err := ParseConfig(path, ConfBase)
if err != nil {
return err
}
if ConfBase.DebugMode == "" {
if ConfBase.Base.DebugMode != "" {
ConfBase.DebugMode = ConfBase.Base.DebugMode
} else {
ConfBase.DebugMode = "debug"
}
}
if ConfBase.TimeLocation == "" {
if ConfBase.Base.TimeLocation != "" {
ConfBase.TimeLocation = ConfBase.Base.TimeLocation
} else {
ConfBase.TimeLocation = "Asia/Chongqing"
}
}
if ConfBase.Log.Level == "" {
ConfBase.Log.Level = "trace"
}
//配置日志
logConf := dlog.LogConfig{
Level: ConfBase.Log.Level,
FW: dlog.ConfFileWriter{
On: ConfBase.Log.FW.On,
LogPath: ConfBase.Log.FW.LogPath,
RotateLogPath: ConfBase.Log.FW.RotateLogPath,
WfLogPath: ConfBase.Log.FW.WfLogPath,
RotateWfLogPath: ConfBase.Log.FW.RotateWfLogPath,
},
CW: dlog.ConfConsoleWriter{
On: ConfBase.Log.CW.On,
Color: ConfBase.Log.CW.Color,
},
}
if err := dlog.SetupDefaultLogWithConf(logConf); err != nil {
panic(err)
}
dlog.SetLayout("2006-01-02T15:04:05.000")
return nil
}
//
//func InitLogger(path string) error {
// if err := dlog.SetupDefaultLogWithFile(path); err != nil {
// panic(err)
// }
// dlog.SetLayout("2006-01-02T15:04:05.000")
// return nil
//}
func InitRedisConf(path string) error {
ConfRedis := &RedisMapConf{}
err := ParseConfig(path, ConfRedis)
if err != nil {
return err
}
ConfRedisMap = ConfRedis
return nil
}
//初始化配置文件
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("toml")
v.ReadConfig(bytes.NewBuffer(bts))
pathArr := strings.Split(f0.Name(), ".")
if ViperConfMap == nil {
ViperConfMap = make(map[string]*viper.Viper)
}
ViperConfMap[pathArr[0]] = v
}
}
return nil
}
//获取get配置信息
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
}
//获取get配置信息
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
}
//获取get配置信息
func GetConf(key string) interface{} {
keys := strings.Split(key, ".")
if len(keys) < 2 {
return nil
}
v := ViperConfMap[keys[0]]
conf := v.Get(strings.Join(keys[1:len(keys)], "."))
return conf
}
//获取get配置信息
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
}
//获取get配置信息
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
}
//获取get配置信息
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
}
//获取get配置信息
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
}
//获取get配置信息
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
}
//获取get配置信息
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
}
//获取时间阶段长度
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
}
//是否设置了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
}