测试代理是否可用
This commit is contained in:
@@ -1,54 +1,27 @@
|
||||
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]
|
||||
}
|
||||
|
||||
func InitViper() {
|
||||
v := viper.New()
|
||||
v.SetConfigFile(confPath)
|
||||
v.SetConfigType("json")
|
||||
v.SetConfigFile("config.yaml")
|
||||
v.SetConfigType("yaml")
|
||||
|
||||
err := v.ReadInConfig()
|
||||
if err != nil {
|
||||
fmt.Printf("[-]读取配置文件错误: %s \n", err)
|
||||
os.Exit(0)
|
||||
panic(fmt.Errorf("读取配置文件失败: %s", err))
|
||||
}
|
||||
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)
|
||||
|
||||
config := &common.Config{}
|
||||
err = v.Unmarshal(config)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("解析配置文件失败: %s", err))
|
||||
}
|
||||
fmt.Println("[+]加载配置文件完成")
|
||||
|
||||
//dump.P(global.AppConf)
|
||||
|
||||
global.Conf = config
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user