conf.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/bfs"
  8. "go-common/library/database/elastic"
  9. "go-common/library/database/sql"
  10. ecode "go-common/library/ecode/tip"
  11. "go-common/library/log"
  12. "go-common/library/log/infoc"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/net/http/blademaster/middleware/permit"
  15. "go-common/library/net/http/blademaster/middleware/verify"
  16. "go-common/library/net/rpc"
  17. "go-common/library/net/rpc/warden"
  18. "go-common/library/net/trace"
  19. "go-common/library/queue/databus"
  20. "github.com/BurntSushi/toml"
  21. )
  22. //Conf config var
  23. var (
  24. ConfPath string
  25. client *conf.Client
  26. Conf = &Config{}
  27. )
  28. //Config Config
  29. type Config struct {
  30. // base
  31. // ecode
  32. Ecode *ecode.Config
  33. // log
  34. Xlog *log.Config
  35. Infoc2 *infoc.Config
  36. InfocBak *infoc.Config
  37. Verify *verify.Config
  38. // rpc client
  39. AccountRPC *warden.ClientConfig
  40. // rpc client
  41. ArchiveRPC *rpc.ClientConfig
  42. // http server config
  43. HTTPServer *bm.ServerConfig
  44. // db
  45. DB *DB
  46. // memcache
  47. Memcache *Memcache
  48. // http client
  49. HTTPClient *HTTPClient
  50. // http client of search
  51. HTTPSearch *HTTPSearch
  52. // http client of Infoc Data
  53. HTTPInfoc *HTTPInfoc
  54. // tracer
  55. Tracer *trace.Config
  56. // databus client
  57. ActionPub *databus.Config
  58. Host Host
  59. ManagerAuth *permit.Config
  60. // elastic config
  61. Elastic *elastic.Config
  62. // manager log config
  63. ManagerLog *databus.Config
  64. //bfs config
  65. BFS *bfs.Config
  66. }
  67. // DB mysql config struct
  68. type DB struct {
  69. DM *sql.Config
  70. DMMetaWriter *sql.Config
  71. DMMetaReader *sql.Config
  72. }
  73. // Memcache dm memcache
  74. type Memcache struct {
  75. Filter *struct {
  76. *memcache.Config
  77. }
  78. Subtitle *struct {
  79. *memcache.Config
  80. }
  81. }
  82. // DMReport dm report
  83. type DMReport struct {
  84. Count int64
  85. MsgURL string
  86. MoralURL string
  87. BlockURL string
  88. }
  89. // Host hosts used in dm admin
  90. type Host struct {
  91. Videoup string
  92. API string
  93. Search string
  94. Season string
  95. Message string
  96. Account string
  97. Mask string
  98. Berserker string
  99. }
  100. // HTTPClient http client
  101. type HTTPClient struct {
  102. *bm.ClientConfig
  103. JudgeURL string
  104. ArcInfoURL string
  105. CidAidsURL string
  106. TypeInfoURL string
  107. }
  108. // HTTPSearch http client of search
  109. type HTTPSearch struct {
  110. *bm.ClientConfig
  111. SearchURL string
  112. UpdateURL string
  113. ReportURL string
  114. UpdateOldURL string
  115. CountURL string
  116. }
  117. // HTTPInfoc http client of Infoc query
  118. type HTTPInfoc struct {
  119. *bm.ClientConfig
  120. InfocQueryURL string
  121. }
  122. func init() {
  123. flag.StringVar(&ConfPath, "conf", "", "config path")
  124. }
  125. //Init int config
  126. func Init() error {
  127. if ConfPath != "" {
  128. return local()
  129. }
  130. return remote()
  131. }
  132. func local() (err error) {
  133. _, err = toml.DecodeFile(ConfPath, &Conf)
  134. return
  135. }
  136. func remote() (err error) {
  137. if client, err = conf.New(); err != nil {
  138. return
  139. }
  140. if err = load(); err != nil {
  141. return
  142. }
  143. go func() {
  144. for range client.Event() {
  145. log.Info("config reload")
  146. if load() != nil {
  147. log.Error("config reload error (%v)", err)
  148. }
  149. }
  150. }()
  151. return
  152. }
  153. func load() (err error) {
  154. var (
  155. s string
  156. ok bool
  157. tmpConf *Config
  158. )
  159. if s, ok = client.Toml2(); !ok {
  160. return errors.New("load config center error")
  161. }
  162. if _, err = toml.Decode(s, &tmpConf); err != nil {
  163. return errors.New("could not decode config")
  164. }
  165. *Conf = *tmpConf
  166. return
  167. }