-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
44 lines (36 loc) · 991 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
42
43
44
package main
import (
"fmt"
"goshop/service-order/pkg/utils"
"github.com/fsnotify/fsnotify"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
//初始化配置文件
func InitConfig() {
buf := &utils.Config{}
UnmarshalYaml(fmt.Sprintf("./conf/%s.app.yaml", gin.Mode()), buf)
utils.C = buf
//app解析
baseInfo := &utils.Base{}
UnmarshalYaml("./conf/app.yaml", baseInfo)
utils.C.Base = baseInfo
}
func UnmarshalYaml(fileName string, data interface{}) {
v := viper.New()
v.SetConfigType("yaml")
v.SetConfigFile(fileName)
if err := v.ReadInConfig(); err != nil {
panic(fmt.Errorf("读取配置文件失败, err: %v", err))
}
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
fmt.Printf("%s文件发生改动哦 \n", e.Name)
if err := v.Unmarshal(data); err != nil {
panic(fmt.Sprintf("解析配置文件出错, err: %v", err))
}
})
if err := v.Unmarshal(data); err != nil {
panic(fmt.Sprintf("解析配置文件出错, err: %v", err))
}
}