-
Notifications
You must be signed in to change notification settings - Fork 49
/
config.go
41 lines (36 loc) · 990 Bytes
/
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
package bootstrap
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/jassue/jassue-gin/global"
"github.com/spf13/viper"
"os"
)
func InitializeConfig() *viper.Viper {
// 设置配置文件路径
config := "config.yaml"
if configEnv := os.Getenv("VIPER_CONFIG"); configEnv != "" {
config = configEnv
}
// 初始化 viper
v := viper.New()
v.SetConfigFile(config)
v.SetConfigType("yaml")
if err := v.ReadInConfig(); err != nil {
panic(fmt.Errorf("read config failed: %s \n", err))
}
// 监听配置文件
v.WatchConfig()
v.OnConfigChange(func(in fsnotify.Event) {
fmt.Println("config file changed:", in.Name)
// 重载配置
if err := v.Unmarshal(&global.App.Config); err != nil {
fmt.Println(err)
}
})
// 将配置赋值给全局变量
if err := v.Unmarshal(&global.App.Config); err != nil {
fmt.Println(err)
}
return v
}