-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
80 lines (66 loc) · 1.4 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
package config
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
var Config = new(config)
func init() {
readConfig()
}
func readConfig() {
v := viper.New()
viper.AutomaticEnv()
v.SetConfigName("app")
v.SetConfigType("toml")
v.AddConfigPath(".")
v.SetDefault("log.info_file", "./log")
v.SetDefault("log.error_file", "./log")
if err := v.ReadInConfig(); err != nil {
fmt.Printf("v.ReadConfig error:%v", err)
panic(err)
}
if err := v.Unmarshal(Config); err != nil {
fmt.Printf("v.Unmarshal error:%v", err)
panic(err)
}
v.OnConfigChange(func(in fsnotify.Event) {
//配置文件变化
conf := new(config)
if err := v.Unmarshal(conf); err != nil {
//todo 这个panic需要recovery
panic(err)
}
Config = conf
})
v.WatchConfig()
}
type config struct {
TestMode bool `mapstructure:"test_mode"`
SwaggerFlag string `mapstructure:"swagger_flag"`
JwtFlag bool `mapstructure:"jwt_flag"`
IrisLoggerLevel string `mapstructure:"iris_logger_level"`
Database struct {
Host string
Port int
User string
Password string
DBName string `mapstructure:"db_name"`
}
Redis struct {
Addr string
Password string
DB int
}
Web struct {
Port int
PageSize int
}
Secret struct {
Jwt string
}
Log struct {
InfoFile string `mapstructure:"info_file"`
ErrorFile string `mapstructure:"error_file"`
}
}