KPI绩效考核系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.4 KiB

package initialization
import (
"encoding/json"
"fmt"
"key_performance_indicators/overall"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
//使用viper 处理包解决配置读取问题
func RunViper(configInfo interface{}, path ...string) *viper.Viper {
var config string
if len(path) == 0 {
config = overall.ConfigFilePathConstant
fmt.Printf("你正在使用系统默认值。配置路径为:%v\n", config)
} else {
config = path[0]
fmt.Printf("你正在使用RunViper传值。配置路径为:%v\n", config)
}
v := viper.New()
// 设置配置文件信息
v.SetConfigFile(config)
v.SetConfigType("yaml")
//读取配置信息
err := v.ReadInConfig()
if err != nil {
panic(fmt.Errorf("配置文件读取失败?原因:%s\n", err))
}
// 监控配置和重新获取配置
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
fmt.Printf("配置文件已经更改:%v\n", e.Name)
if errSet := v.Unmarshal(&configInfo); errSet != nil {
fmt.Printf("新配置文件解析失败!系统继续使用原配置!失败原因:%s\n", errSet)
}
})
//解析配置映射到切片
if errStruct := v.Unmarshal(&configInfo); errStruct != nil {
fmt.Printf("配置解析失败!原因:%s\n", errStruct)
}
json.Marshal(configInfo)
// cfi, _ := json.Marshal(configInfo)
// fmt.Printf("============>%v\n", string(cfi))
return v
}