conf.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/rpc"
  11. "go-common/library/net/rpc/warden"
  12. "go-common/library/net/trace"
  13. "go-common/library/queue/databus"
  14. "go-common/library/time"
  15. "github.com/BurntSushi/toml"
  16. "go-common/library/database/hbase.v2"
  17. )
  18. var (
  19. confPath string
  20. // Conf .
  21. Conf = &Config{}
  22. client *conf.Client
  23. )
  24. // Config config struct .
  25. type Config struct {
  26. // API host
  27. Host *Host
  28. // channal len
  29. ChanSize int
  30. BeginOffset int64
  31. // log
  32. Xlog *log.Config
  33. // http
  34. BM *bm.ServerConfig
  35. // tracer
  36. Tracer *trace.Config
  37. // tick load pgc
  38. Tick time.Duration
  39. // db
  40. DB *DB
  41. // redis
  42. Redis *Redis
  43. // hbase
  44. Hbase *hbaseConf
  45. // http client test
  46. HTTPClient HTTPClient
  47. // databus
  48. ArchiveSub *databus.Config
  49. ArchiveResultSub *databus.Config
  50. VideoupSub *databus.Config
  51. ManagerDBSub *databus.Config
  52. // ChanSize aid%ChanSize
  53. ArchiveRPCGroup2 *rpc.ClientConfig
  54. TagDisConf *rpc.ClientConfig
  55. //grpc
  56. GRPC *GRPC
  57. // mail
  58. Mail *mail
  59. }
  60. //GRPC .
  61. type GRPC struct {
  62. AccRPC *warden.ClientConfig
  63. UpsRPC *warden.ClientConfig
  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 for httpclient
  73. type Host struct {
  74. Data string
  75. API string
  76. Archive string
  77. Profit string
  78. WWW string
  79. }
  80. // DB db struct
  81. type DB struct {
  82. Archive *sql.Config
  83. Manager *sql.Config
  84. }
  85. // Redis redis struct
  86. type Redis struct {
  87. Track *struct {
  88. *redis.Config
  89. Expire time.Duration
  90. }
  91. Mail *redis.Config
  92. Secondary *struct {
  93. *redis.Config
  94. Expire time.Duration
  95. }
  96. }
  97. // HTTPClient http client struct
  98. type HTTPClient struct {
  99. Read *bm.ClientConfig
  100. Write *bm.ClientConfig
  101. }
  102. //mail 邮件配置
  103. type mail struct {
  104. Host string
  105. Port, SpeedThreshold, OverspeedThreshold int
  106. Username, Password string
  107. Addr, PrivateAddr []*MailElemenet
  108. }
  109. //MailElemenet 邮件接收人配置
  110. type MailElemenet struct {
  111. Type string
  112. Desc string
  113. Addr []string
  114. }
  115. func init() {
  116. flag.StringVar(&confPath, "conf", "", "default config path")
  117. }
  118. // Init init conf
  119. func Init() (err error) {
  120. if confPath != "" {
  121. return local()
  122. }
  123. return remote()
  124. }
  125. func local() (err error) {
  126. _, err = toml.DecodeFile(confPath, &Conf)
  127. return
  128. }
  129. func remote() (err error) {
  130. if client, err = conf.New(); err != nil {
  131. return
  132. }
  133. if err = load(); err != nil {
  134. return
  135. }
  136. go func() {
  137. for range client.Event() {
  138. log.Info("config reload")
  139. if load() != nil {
  140. log.Error("config reload error (%v)", err)
  141. }
  142. }
  143. }()
  144. return
  145. }
  146. func load() (err error) {
  147. var (
  148. s string
  149. ok bool
  150. tmpConf *Config
  151. )
  152. if s, ok = client.Toml2(); !ok {
  153. return errors.New("load config center error")
  154. }
  155. if _, err = toml.Decode(s, &tmpConf); err != nil {
  156. return errors.New("could not decode config")
  157. }
  158. *Conf = *tmpConf
  159. return
  160. }