conf.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/permit"
  13. "go-common/library/net/rpc"
  14. "go-common/library/net/rpc/warden"
  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. // Conf info.
  22. var (
  23. ConfPath string
  24. Conf = &Config{}
  25. client *conf.Client
  26. )
  27. // Config struct.
  28. type Config struct {
  29. // bm
  30. BM *HTTPServers
  31. // db
  32. DB *DB
  33. // base
  34. // elk
  35. Xlog *log.Config
  36. // httpClinet
  37. HTTPClient *HTTPClient
  38. // tracer
  39. Tracer *trace.Config
  40. // Ecode
  41. Ecode *ecode.Config
  42. // host
  43. Host *Host
  44. // ArchStatus
  45. ArchStatus map[string]string
  46. // Redis
  47. Redis *Redis
  48. // Databus
  49. Env string
  50. Consume bool
  51. IsTest bool
  52. UpSub *upSub
  53. ChanSize int64
  54. Monitor *Monitor
  55. // rpc client2
  56. ArticleRPC *rpc.ClientConfig
  57. // identify
  58. App *blademaster.App
  59. Memcache *MC
  60. //API HOST
  61. API string
  62. Live string
  63. // rpc server
  64. RPCServer *rpc.ServerConfig
  65. GRPCServer *warden.ServerConfig
  66. // auth
  67. Auth *permit.Config
  68. // hbase
  69. HBase *HBaseConfig
  70. // Manager
  71. ManagerReport *databus.Config
  72. // GRPCClient
  73. GRPCClient *GRPC
  74. }
  75. // HBaseConfig combine with hbase.Config add ReadTimeout, WriteTimeout
  76. type HBaseConfig struct {
  77. hbase.Config
  78. // extra config
  79. ReadTimeout time.Duration
  80. ReadsTimeout time.Duration
  81. WriteTimeout time.Duration
  82. WritesTimeout time.Duration
  83. }
  84. //UpSub upsub config
  85. type upSub struct {
  86. *databus.Config
  87. UpChanSize int
  88. ConsumeLimit int
  89. RoutineLimit int
  90. SpecialAddDBLimit int
  91. }
  92. // GRPC .
  93. type GRPC struct {
  94. Archive *warden.ClientConfig
  95. Account *warden.ClientConfig
  96. }
  97. //HTTPServers for http server.
  98. type HTTPServers struct {
  99. Inner *blademaster.ServerConfig
  100. }
  101. // DB conf.
  102. type DB struct {
  103. // Creative db
  104. Creative *sql.Config
  105. Manager *sql.Config
  106. UpCRM *sql.Config
  107. ArcResult *sql.Config
  108. Archive *sql.Config
  109. }
  110. // Redis redis config
  111. type Redis struct {
  112. Up *struct {
  113. *redis.Config
  114. UpExpire time.Duration
  115. }
  116. }
  117. // HTTPClient conf.
  118. type HTTPClient struct {
  119. Normal *blademaster.ClientConfig
  120. Slow *blademaster.ClientConfig
  121. }
  122. // Host conf.
  123. type Host struct {
  124. API string
  125. Live string
  126. Search string
  127. Manager string
  128. }
  129. // Monitor conf.
  130. type Monitor struct {
  131. Host string
  132. Moni string
  133. UserName string
  134. AppSecret string
  135. AppToken string
  136. IntervalAlarm time.Duration
  137. }
  138. //App for key secret.
  139. type App struct {
  140. Key string
  141. Secret string
  142. }
  143. //MC memcache
  144. type MC struct {
  145. UpExpire time.Duration
  146. UpSpecialExpire time.Duration
  147. Up *memcache.Config
  148. }
  149. func init() {
  150. flag.StringVar(&ConfPath, "conf", "", "default config path")
  151. }
  152. // Init conf.
  153. func Init() (err error) {
  154. if ConfPath != "" {
  155. return local()
  156. }
  157. return remote()
  158. }
  159. func local() (err error) {
  160. _, err = toml.DecodeFile(ConfPath, &Conf)
  161. return
  162. }
  163. func remote() (err error) {
  164. if client, err = conf.New(); err != nil {
  165. return
  166. }
  167. if err = load(); err != nil {
  168. return
  169. }
  170. go func() {
  171. for range client.Event() {
  172. log.Info("config reload")
  173. if load() != nil {
  174. log.Error("config reload error (%v)", err)
  175. }
  176. }
  177. }()
  178. return
  179. }
  180. func load() (err error) {
  181. var (
  182. tomlStr string
  183. ok bool
  184. tmpConf *Config
  185. )
  186. if tomlStr, ok = client.Toml2(); !ok {
  187. return errors.New("load config center error")
  188. }
  189. if _, err = toml.Decode(tomlStr, &tmpConf); err != nil {
  190. return errors.New("could not decode toml config")
  191. }
  192. *Conf = *tmpConf
  193. return
  194. }