Migrating configuration files from json to yaml

This commit is contained in:
Granty1
2020-04-05 20:35:52 +08:00
parent 81a54b5a44
commit 7c52c05280
4 changed files with 96 additions and 91 deletions

View File

@@ -7,16 +7,17 @@ import (
"github.com/spf13/viper"
)
const defaultConfigFile = "config.yaml"
func init() {
v := viper.New()
v.SetConfigName("config") // 设置配置文件名 (不带后缀)
v.AddConfigPath("./") // 第一个搜索路径
v.SetConfigType("json")
err := v.ReadInConfig() // 搜索路径,并读取配置数据
v.SetConfigFile(defaultConfigFile)
err := v.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("config file changed:", e.Name)
if err := v.Unmarshal(&global.GVA_CONFIG); err != nil {