package config import ( "fmt" "github.com/beego/beego/v2/core/logs" "github.com/beego/beego/v2/server/web" "github.com/siddontang/go/log" ) const ( SPLIT = "." ) type Config interface { InitConfig() } type BaseConfig struct { prefix string } func (b *BaseConfig) GetString(key string) string { value, err := web.AppConfig.String(fmt.Sprintf("%s%s%s", b.prefix, SPLIT, key)) if err != nil { return "" } return value } func (b *BaseConfig) GetInt(key string) int { value, err := web.AppConfig.Int(fmt.Sprintf("%s%s%s", b.prefix, SPLIT, key)) if err != nil { return 0 } return value } type Instance func() Config var configs = make(map[string]Instance) func GetConfig(name string) (config Config) { instanceFunc, ok := configs[name] if !ok { logs.Error("配置信息不存在", name) return nil } config = instanceFunc() config.InitConfig() return config } func Register(name string, adapter Instance) { if adapter == nil { panic("缓存类型不存在") } if _, ok := configs[name]; ok { panic("当前缓存类型已注册:" + name) } configs[name] = adapter } func init() { tmpRunMode, err := web.AppConfig.String("run_mode") if err != nil { panic(any("配置文件读取run_mode错误 " + err.Error())) } fmt.Println("RunMode:", tmpRunMode) if tmpRunMode == "" { configPath := "/home/code/config/eta_mini_ht_api/conf/app.conf" err = web.LoadAppConfig("ini", configPath) if err != nil { log.Fatalf("web.LoadAppConfig Err:%v", err) } } }