-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
100 lines (83 loc) · 1.7 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
package config
import (
"flag"
"time"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
var config = new(Config)
type Config struct {
MySQL struct {
Base struct {
MaxOpenConn int
MaxIdleConn int
ConnMaxLifeTime time.Duration
Addr string
User string
Pass string
Name string
}
}
ETCD struct {
Endpoints []string
Username string
Password string
DialTimeout time.Duration
DialKeepAlive time.Duration
DialKeepAliveTimeout time.Duration
}
JWT struct {
Secret string
ExpireDuration time.Duration
Type string
RefreshDuration time.Duration
}
Log struct {
LogPath string
Level string
Stdout bool
JsonFormat bool
}
Server struct {
ServerName string
Host string
Pprof bool
HistoryListLen int64
}
Jaeger struct {
UdpEndpoint string
StdOut bool
}
SDK struct {
ConfigFilePath string
LogLevel string
MergeConfig bool
MergeConfigFile string
}
}
var configPath string
func InitConfig() {
flag.StringVar(&configPath, "config_path", ".", "path of cfg.toml")
flag.Parse()
viper.SetConfigName("cfg")
viper.SetConfigType("toml")
// 本地开发配置
viper.AddConfigPath("./internal/service/sail/config")
// 容器配置
viper.AddConfigPath(configPath)
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
if err := viper.Unmarshal(config); err != nil {
panic(err)
}
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
if err := viper.Unmarshal(config); err != nil {
panic(err)
}
})
}
func Get() Config {
return *config
}