2 Star 0 Fork 0

江苏艾雨文承养老机器人有限公司/aywc_judge

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
PropConfig.go 2.06 KB
一键复制 编辑 原始数据 按行查看 历史
dtal 提交于 2021-06-02 09:26 +08:00 . init
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/aywc_1/aywc_judge.git
git@gitee.com:aywc_1/aywc_judge.git
aywc_1
aywc_judge
aywc_judge
v0.6.4

搜索帮助