代码拉取完成,页面将自动刷新
package configuration
import (
"flag"
"fmt"
"github.com/shenyisyn/goft-gin/goft"
Injector "github.com/shenyisyn/goft-ioc"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
)
func init() {
var configFilePath = ""
dir, _ := os.Getwd()
defaultFile := dir + "/application.yaml"
flag.StringVar(&configFilePath, "conf", defaultFile, "config file Path")
flag.Parse()
fmt.Println(configFilePath)
CreateProp(configFilePath)
}
var Properties *Prop
type Prop struct {
ConfigFilePath string
}
func CreateProp(configFilePath string) {
Properties = &Prop{ConfigFilePath: configFilePath}
}
func GetSysConfigValue(key string, defaultValue string) string {
if sysConfig := Injector.BeanFactory.Get((*goft.SysConfig)(nil)); sysConfig != nil {
config := sysConfig.(*goft.SysConfig).Config
if _, ok := config[key]; ok {
return config[key].(string)
} else {
return defaultValue
}
} else {
return defaultValue
}
}
//递归读取用户配置文件
func GetConfigValue(m map[interface{}]interface{}, prefix []string, index int) interface{} {
key := prefix[index]
if v, ok := m[key]; ok {
if index == len(prefix)-1 { //到了最后一个
return v
} else {
index = index + 1
if mv, ok := v.(map[interface{}]interface{}); ok { //值必须是UserConfig类型
return GetConfigValue(mv, prefix, index)
} else {
return nil
}
}
}
return nil
}
//读取配置文件
func LoadConfigFile(filepath string) []byte {
b, err := ioutil.ReadFile(filepath)
if err != nil {
log.Error(err)
return nil
}
return b
}
func GetYamlValue(prefixs []string, index int, defaultValue interface{}) interface{} {
m := make(map[interface{}]interface{}, 0)
if b := LoadConfigFile(Properties.ConfigFilePath); b != nil {
err := yaml.Unmarshal(b, m)
if err != nil {
log.Fatal(err)
}
}
value := GetConfigValue(m, prefixs, index)
if value != nil {
return value
}
return defaultValue
}
func (this *Prop) SetConfigFilePath(filePath string) {
this.ConfigFilePath = filePath
}
func (this *Prop) GetConfigFilePath() string {
return this.ConfigFilePath
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。