增加定时任务 处理异常
This commit is contained in:
59
core/viper/viper.go
Normal file
59
core/viper/viper.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package viper
|
||||
|
||||
import (
|
||||
"energy-management-system/config"
|
||||
"energy-management-system/global"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
"os"
|
||||
)
|
||||
|
||||
// InitViper 解析yaml格式文件
|
||||
func InitViper(path ...string) {
|
||||
var confPath string
|
||||
if len(path) == 0 {
|
||||
flag.StringVar(&confPath, "c", "", "choose config file.")
|
||||
flag.Parse()
|
||||
if confPath == "" {
|
||||
if AppEnv := os.Getenv(config.AppEnv); AppEnv == "" {
|
||||
confPath = config.EnvConfig
|
||||
} else {
|
||||
confPath = AppEnv
|
||||
}
|
||||
} else {
|
||||
}
|
||||
} else {
|
||||
confPath = path[0]
|
||||
}
|
||||
|
||||
v := viper.New()
|
||||
// 指定配置文件路径
|
||||
v.SetConfigFile(confPath)
|
||||
// 如果配置文件的名称中没有扩展名,则需要配置此项
|
||||
v.SetConfigType("yaml")
|
||||
// 查找并读取配置文件
|
||||
err := v.ReadInConfig()
|
||||
if err != nil {
|
||||
fmt.Printf("[-]读取配置文件错误: %s \n", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
// 监控并重新读取配置文件
|
||||
v.WatchConfig()
|
||||
v.OnConfigChange(func(e fsnotify.Event) {
|
||||
if err = v.Unmarshal(&global.AppConf); err != nil {
|
||||
fmt.Printf("[-]重新解析配置文件失败: %s \n", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
fmt.Println("[+]重新加载配置文件完成")
|
||||
})
|
||||
if err = v.Unmarshal(&global.AppConf); err != nil {
|
||||
fmt.Printf("[-]解析配置文件失败: %s \n", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
fmt.Println("[+]加载配置文件完成")
|
||||
|
||||
//dump.P(global.AppConf)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user