config.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/core/logs"
  5. "github.com/beego/beego/v2/server/web"
  6. "github.com/siddontang/go/log"
  7. )
  8. const (
  9. SPLIT = "."
  10. )
  11. type Config interface {
  12. InitConfig()
  13. }
  14. type BaseConfig struct {
  15. prefix string
  16. }
  17. func (b *BaseConfig) GetString(key string) string {
  18. value, err := web.AppConfig.String(fmt.Sprintf("%s%s%s", b.prefix, SPLIT, key))
  19. if err != nil {
  20. return ""
  21. }
  22. return value
  23. }
  24. func (b *BaseConfig) GetInt(key string) int {
  25. value, err := web.AppConfig.Int(fmt.Sprintf("%s%s%s", b.prefix, SPLIT, key))
  26. if err != nil {
  27. return 0
  28. }
  29. return value
  30. }
  31. type Instance func() Config
  32. var configs = make(map[string]Instance)
  33. func GetConfig(name string) (config Config) {
  34. instanceFunc, ok := configs[name]
  35. if !ok {
  36. logs.Error("配置信息不存在", name)
  37. return nil
  38. }
  39. config = instanceFunc()
  40. config.InitConfig()
  41. return config
  42. }
  43. func Register(name string, adapter Instance) {
  44. if adapter == nil {
  45. panic("缓存类型不存在")
  46. }
  47. if _, ok := configs[name]; ok {
  48. panic("当前缓存类型已注册:" + name)
  49. }
  50. configs[name] = adapter
  51. }
  52. func init() {
  53. tmpRunMode, err := web.AppConfig.String("run_mode")
  54. if err != nil {
  55. panic(any("配置文件读取run_mode错误 " + err.Error()))
  56. }
  57. fmt.Println("RunMode:", tmpRunMode)
  58. if tmpRunMode == "" {
  59. configPath := "/home/code/config/eta_mini_ht_api/conf/app.conf"
  60. err = web.LoadAppConfig("ini", configPath)
  61. if err != nil {
  62. log.Fatalf("web.LoadAppConfig Err:%v", err)
  63. }
  64. }
  65. }