conf.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/sql"
  9. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/rpc"
  13. "go-common/library/net/rpc/warden"
  14. "go-common/library/net/trace"
  15. "go-common/library/queue/databus"
  16. "go-common/library/time"
  17. "github.com/BurntSushi/toml"
  18. )
  19. // Conf info.
  20. var (
  21. confPath string
  22. Conf = &Config{}
  23. client *conf.Client
  24. )
  25. // Config struct.
  26. type Config struct {
  27. // base
  28. // tick
  29. Tick time.Duration
  30. // max assist count
  31. MaxAssCnt int
  32. MaxTypeCnt int64
  33. // app
  34. App *bm.App
  35. // host
  36. Host *Host
  37. // elk
  38. Xlog *log.Config
  39. // tracer
  40. Tracer *trace.Config
  41. BM *bm.ServerConfig
  42. // rpc server2
  43. RPCServer *rpc.ServerConfig
  44. // http client
  45. HTTPClient *HTTPClient
  46. // db
  47. DB *DB
  48. // ecode
  49. Ecode *ecode.Config
  50. // rpc client2
  51. ArchiveRPC *rpc.ClientConfig
  52. // mc
  53. Memcache *Memcache
  54. // redis
  55. Redis *Redis
  56. // databus sub
  57. RelationSub *databus.Config
  58. AccClient *warden.ClientConfig
  59. }
  60. // HTTPServers Http Servers
  61. type HTTPServers struct {
  62. Inner *bm.ServerConfig
  63. }
  64. // Memcache conf
  65. type Memcache struct {
  66. Assist *struct {
  67. *memcache.Config
  68. SubmitExpire time.Duration
  69. }
  70. }
  71. // Redis conf
  72. type Redis struct {
  73. Assist *struct {
  74. *redis.Config
  75. Expire time.Duration
  76. }
  77. }
  78. // Host conf.
  79. type Host struct {
  80. Message string
  81. Account string
  82. }
  83. // DB conf.
  84. type DB struct {
  85. Assist *sql.Config
  86. }
  87. // HTTPClient conf.
  88. type HTTPClient struct {
  89. Normal *bm.ClientConfig
  90. Slow *bm.ClientConfig
  91. }
  92. func init() {
  93. flag.StringVar(&confPath, "conf", "", "default config path")
  94. }
  95. // Init conf.
  96. func Init() (err error) {
  97. if confPath != "" {
  98. return local()
  99. }
  100. return remote()
  101. }
  102. func local() (err error) {
  103. _, err = toml.DecodeFile(confPath, &Conf)
  104. return
  105. }
  106. func remote() (err error) {
  107. if client, err = conf.New(); err != nil {
  108. return
  109. }
  110. if err = load(); err != nil {
  111. return
  112. }
  113. go func() {
  114. for range client.Event() {
  115. log.Info("config reload")
  116. if load() != nil {
  117. log.Error("config reload error (%v)", err)
  118. }
  119. }
  120. }()
  121. return
  122. }
  123. func load() (err error) {
  124. var (
  125. s string
  126. ok bool
  127. tmpConf *Config
  128. )
  129. if s, ok = client.Toml2(); !ok {
  130. return errors.New("load config center error")
  131. }
  132. if _, err = toml.Decode(s, &tmpConf); err != nil {
  133. return errors.New("could not decode config")
  134. }
  135. *Conf = *tmpConf
  136. return
  137. }