conf.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. ecode "go-common/library/ecode/tip"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/middleware/verify"
  12. "go-common/library/net/rpc/warden"
  13. "go-common/library/net/trace"
  14. xtime "go-common/library/time"
  15. "github.com/BurntSushi/toml"
  16. )
  17. var (
  18. confPath string
  19. client *conf.Client
  20. // Conf config
  21. Conf = &Config{}
  22. )
  23. // Config .
  24. type Config struct {
  25. Log *log.Config
  26. Tracer *trace.Config
  27. BM *bm.ServerConfig
  28. Verify *verify.Config
  29. Memcache *memcache.Config
  30. MySQL *sql.Config
  31. Ecode *ecode.Config
  32. LocalCache *LocalCache
  33. ArchiveGRPC *warden.ClientConfig
  34. AccountGRPC *warden.ClientConfig
  35. CacheTTL *CacheTTL
  36. Biz *Biz
  37. }
  38. // Biz .
  39. type Biz struct {
  40. ElecAVRankSize int
  41. ElecUPRankSize int
  42. RAMAVIDs []int64
  43. RAMUPIDs []int64
  44. ReloadDuration xtime.Duration
  45. }
  46. // LocalCache prop.
  47. type LocalCache struct {
  48. ElecAVRankSize int
  49. ElecAVRankTTL xtime.Duration
  50. ElecUPRankSize int
  51. ElecUPRankTTL xtime.Duration
  52. }
  53. // CacheTTL .
  54. type CacheTTL struct {
  55. ElecUPRankTTL int32
  56. ElecAVRankTTL int32
  57. ElecPrepUPRankTTL int32
  58. ElecPrepAVRankTTL int32
  59. ElecUserSettingTTL int32
  60. }
  61. func init() {
  62. flag.StringVar(&confPath, "conf", "", "default config path")
  63. }
  64. // Init init conf
  65. func Init() error {
  66. if confPath != "" {
  67. return local()
  68. }
  69. return remote()
  70. }
  71. func local() (err error) {
  72. _, err = toml.DecodeFile(confPath, &Conf)
  73. return
  74. }
  75. func remote() (err error) {
  76. if client, err = conf.New(); err != nil {
  77. return
  78. }
  79. if err = load(); err != nil {
  80. return
  81. }
  82. go func() {
  83. for range client.Event() {
  84. log.Info("config reload")
  85. if load() != nil {
  86. log.Error("config reload error (%v)", err)
  87. }
  88. }
  89. }()
  90. return
  91. }
  92. func load() (err error) {
  93. var (
  94. s string
  95. ok bool
  96. tmpConf *Config
  97. )
  98. if s, ok = client.Toml2(); !ok {
  99. return errors.New("load config center error")
  100. }
  101. if _, err = toml.Decode(s, &tmpConf); err != nil {
  102. return errors.New("could not decode config")
  103. }
  104. *Conf = *tmpConf
  105. return
  106. }