conf.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "go-common/library/log/infoc"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/rpc"
  14. "go-common/library/net/trace"
  15. "go-common/library/queue/databus"
  16. xtime "go-common/library/time"
  17. "github.com/BurntSushi/toml"
  18. )
  19. // global var
  20. var (
  21. confPath string
  22. client *conf.Client
  23. // Conf config
  24. Conf = &Config{}
  25. )
  26. // Config config set
  27. type Config struct {
  28. // base
  29. // elk
  30. Log *log.Config
  31. // http
  32. BM *HTTPServers
  33. // tracer
  34. Tracer *trace.Config
  35. // redis
  36. Redis *Redis
  37. // memcache
  38. Memcache *Memcache
  39. // db
  40. MySQL *MySQL
  41. // databus
  42. ReportDatabus *databus.Config
  43. // chan number
  44. ChanNum int64
  45. // chan db number
  46. ChanDBNum int64
  47. // ecode
  48. Ecode *ecode.Config
  49. // Report
  50. Report *databus.Config
  51. // client
  52. Consumer *Consumer
  53. // HTTPClient
  54. HTTPClient *bm.ClientConfig
  55. // HTTPUnicom
  56. HTTPUnicom *bm.ClientConfig
  57. // host
  58. Host *Host
  59. // unicom
  60. Unicom *Unicom
  61. // infoc2
  62. UnicomUserInfoc2 *infoc.Config
  63. UnicomPackInfoc *infoc.Config
  64. // tick time
  65. Tick xtime.Duration
  66. // monthly
  67. Monthly bool
  68. // seq
  69. SeqRPC *rpc.ClientConfig
  70. // Seq
  71. Seq *Seq
  72. }
  73. type Seq struct {
  74. BusinessID int64
  75. Token string
  76. }
  77. // HTTPServers Http Servers
  78. type HTTPServers struct {
  79. Outer *bm.ServerConfig
  80. Inner *bm.ServerConfig
  81. Local *bm.ServerConfig
  82. }
  83. type MySQL struct {
  84. Show *sql.Config
  85. }
  86. type Unicom struct {
  87. PackKeyExpired xtime.Duration
  88. KeyExpired xtime.Duration
  89. }
  90. type Memcache struct {
  91. Operator *struct {
  92. *memcache.Config
  93. Expire xtime.Duration
  94. }
  95. }
  96. type Redis struct {
  97. Feed *struct {
  98. *redis.Config
  99. }
  100. }
  101. type Consumer struct {
  102. Group string
  103. Topic string
  104. Offset string
  105. Brokers []string
  106. }
  107. type Host struct {
  108. APP string
  109. UnicomFlow string
  110. Unicom string
  111. }
  112. func init() {
  113. flag.StringVar(&confPath, "conf", "", "default config path")
  114. }
  115. // Init init conf
  116. func Init() error {
  117. if confPath != "" {
  118. return local()
  119. }
  120. return remote()
  121. }
  122. func local() (err error) {
  123. _, err = toml.DecodeFile(confPath, &Conf)
  124. return
  125. }
  126. func remote() (err error) {
  127. if client, err = conf.New(); err != nil {
  128. return
  129. }
  130. if err = load(); err != nil {
  131. return
  132. }
  133. go func() {
  134. for range client.Event() {
  135. log.Info("config reload")
  136. if load() != nil {
  137. log.Error("config reload error (%v)", err)
  138. }
  139. }
  140. }()
  141. return
  142. }
  143. func load() (err error) {
  144. var (
  145. s string
  146. ok bool
  147. tmpConf *Config
  148. )
  149. if s, ok = client.Toml2(); !ok {
  150. return errors.New("load config center error")
  151. }
  152. if _, err = toml.Decode(s, &tmpConf); err != nil {
  153. return errors.New("could not decode config")
  154. }
  155. *Conf = *tmpConf
  156. return
  157. }