You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1 KiB
Go

package initialize
import (
"Lee-WineList/config"
"fmt"
"git.echol.cn/loser/logger/log"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
var localVp *viper.Viper
func initLocaConfig() {
localVp = viper.New()
localVp.SetConfigName("config") // 文件名-无需文件后缀
localVp.SetConfigType("yaml") // 文件扩展名
localVp.AddConfigPath(".")
// 处理读取配置文件的错误
if err := localVp.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
panic(fmt.Errorf("未找到配置文件,请检查路径: %s \n", err))
} else {
panic(fmt.Errorf("文件已找到,但读取文件时出错: %s \n", err))
}
}
// 解析配置文件为结构体
if err := localVp.Unmarshal(&config.Scd); err != nil {
log.Panic(err)
}
localVp.WatchConfig() // 监听配置文件变化
localVp.OnConfigChange(func(e fsnotify.Event) {
log.Info("配置文件发生变动:", e.Name)
fmt.Printf("配置文件发生变动:%s", e.Name)
if err := localVp.Unmarshal(&config.Scd); err != nil {
log.Panic(err)
}
// 配置文件发生变动,重新初始化一下连接信息
initLocaConfig()
})
}