Files
check-https-proxy-node/common/viper/viper.go
2025-01-08 14:41:07 +08:00

55 lines
1.1 KiB
Go

package viper
import (
"flag"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"httppp/common"
"httppp/global"
"os"
)
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(common.AppEnv); AppEnv == "" {
confPath = common.EnvConfig
} else {
confPath = AppEnv
}
} else {
}
} else {
confPath = path[0]
}
v := viper.New()
v.SetConfigFile(confPath)
v.SetConfigType("json")
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.Conf); err != nil {
fmt.Printf("[-]重新解析配置文件失败: %s \n", err)
os.Exit(0)
}
fmt.Println("[+]重新加载配置文件完成")
})
if err = v.Unmarshal(&global.Conf); err != nil {
fmt.Printf("[-]解析配置文件失败: %s \n", err)
os.Exit(0)
}
fmt.Println("[+]加载配置文件完成")
//dump.P(global.AppConf)
}