conf.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/net/rpc/warden"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/orm"
  9. "go-common/library/database/sql"
  10. ecode "go-common/library/ecode/tip"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/http/blademaster/middleware/permit"
  14. "go-common/library/net/rpc"
  15. "go-common/library/net/trace"
  16. "go-common/library/queue/databus"
  17. "go-common/library/time"
  18. "go-common/library/database/hbase.v2"
  19. "github.com/BurntSushi/toml"
  20. )
  21. var (
  22. confPath string
  23. //Conf .
  24. Conf = &Config{}
  25. client *conf.Client
  26. )
  27. //Config .
  28. type Config struct {
  29. Env string
  30. // base
  31. // host
  32. Host *Host
  33. // channal len
  34. ChanSize int64
  35. // log
  36. Xlog *log.Config
  37. // http
  38. BM *bm.ServerConfig
  39. // Auth
  40. Auth *permit.Config
  41. // tracer
  42. Tracer *trace.Config
  43. // tick load pgc
  44. Tick time.Duration
  45. // db
  46. DB *DB
  47. // db
  48. ORMArchive *orm.Config
  49. // databus
  50. VideoupPub *databus.Config
  51. UpCreditPub *databus.Config
  52. // redis
  53. Redis *Redis
  54. // hbase
  55. HBase *hbaseConf
  56. // http client test
  57. HTTPClient HTTPClient
  58. // rpc
  59. AccountRPC *warden.ClientConfig
  60. UpsRPC *warden.ClientConfig
  61. TagDisRPC *rpc.ClientConfig
  62. Ecode *ecode.Config
  63. ManagerReport *databus.Config
  64. }
  65. type hbaseConf struct {
  66. hbase.Config
  67. ReadTimeout time.Duration
  68. ReadsTimeout time.Duration
  69. WriteTimeout time.Duration
  70. WritesTimeout time.Duration
  71. }
  72. //Host .
  73. type Host struct {
  74. API string
  75. MngSearch string
  76. Manager string
  77. Data string
  78. Account string
  79. Task string
  80. Archive string
  81. }
  82. //DB .
  83. type DB struct {
  84. Archive *sql.Config
  85. ArchiveRead *sql.Config
  86. Manager *sql.Config
  87. Oversea *orm.Config
  88. Creative *sql.Config
  89. }
  90. //Redis .
  91. type Redis struct {
  92. Track *struct {
  93. *redis.Config
  94. Expire time.Duration
  95. }
  96. Secondary *struct {
  97. *redis.Config
  98. Expire time.Duration
  99. }
  100. }
  101. // HTTPClient test
  102. type HTTPClient struct {
  103. Read *bm.ClientConfig
  104. Write *bm.ClientConfig
  105. Search *bm.ClientConfig
  106. }
  107. func init() {
  108. flag.StringVar(&confPath, "conf", "", "default config path")
  109. }
  110. //Init .
  111. func Init() (err error) {
  112. if confPath != "" {
  113. return local()
  114. }
  115. return remote()
  116. }
  117. func local() (err error) {
  118. _, err = toml.DecodeFile(confPath, &Conf)
  119. return
  120. }
  121. func remote() (err error) {
  122. if client, err = conf.New(); err != nil {
  123. return
  124. }
  125. if err = load(); err != nil {
  126. return
  127. }
  128. go func() {
  129. for range client.Event() {
  130. log.Info("config reload")
  131. if load() != nil {
  132. log.Error("config reload error (%v)", err)
  133. }
  134. }
  135. }()
  136. return
  137. }
  138. func load() (err error) {
  139. var (
  140. s string
  141. ok bool
  142. tmpConf *Config
  143. )
  144. if s, ok = client.Toml2(); !ok {
  145. return errors.New("load config center error")
  146. }
  147. if _, err = toml.Decode(s, &tmpConf); err != nil {
  148. return errors.New("could not decode config")
  149. }
  150. *Conf = *tmpConf
  151. return
  152. }