新的应用平台采用国密SM4算法进行加解密
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.

61 lines
1.6 KiB

package initialization
import (
"appNewPlatform/appConstant"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
/*
*
@ 作者: 秦东
@ 时间: 2024-10-03 09:10:48
@ 功能: 使用viper读取配置文件
*/
func RunViper(configInfo interface{}, path ...string) *viper.Viper {
// fmt.Printf("你正在使用系统默认值。11111配置路径为:%v\n", path)
var config string
if len(path) == 0 {
config = appConstant.AppConfigPath
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)
} else {
// LoadDatabase()
if e.Name == "config\\configDatabase\\database.yaml" {
LoadingDatabase()
}
if e.Name == "config\\configNosql\\redis.yaml" {
LoadRedis()
}
}
})
//解析配置映射到切片
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
}