config.go 1004 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/core/logs"
  5. "github.com/beego/beego/v2/server/web"
  6. )
  7. const (
  8. SPLIT = "."
  9. )
  10. type Config interface {
  11. GetConfig() interface{}
  12. InitConfig()
  13. }
  14. type BaseConfig struct {
  15. prefix string
  16. config map[string]string
  17. }
  18. func (b *BaseConfig) GetString(key string) string {
  19. value, err := web.AppConfig.String(fmt.Sprintf("%s%s%s", b.prefix, SPLIT, key))
  20. if err != nil {
  21. return ""
  22. }
  23. return value
  24. }
  25. type Instance func() Config
  26. var configs = make(map[string]Instance)
  27. func GetConfig(name string) (config Config) {
  28. instanceFunc, ok := configs[name]
  29. if !ok {
  30. logs.Error("cache: unknown adapter name %q (forgot to import?)", name)
  31. return nil
  32. }
  33. config = instanceFunc()
  34. config.InitConfig()
  35. return config
  36. }
  37. func Register(name string, adapter Instance) {
  38. if adapter == nil {
  39. panic("cache: Register adapter is nil")
  40. }
  41. if _, ok := configs[name]; ok {
  42. panic("cache: Register called twice for adapter " + name)
  43. }
  44. configs[name] = adapter
  45. }