conf.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/hbase.v2"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/trace"
  11. "go-common/library/queue/databus"
  12. "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. var (
  16. // ConfPath local config path
  17. confPath string
  18. client *conf.Client
  19. // Conf is global config object.
  20. Conf = &Config{}
  21. )
  22. // Config is project all config
  23. type Config struct {
  24. // base
  25. // log
  26. Log *log.Config
  27. // Mysql
  28. Mysql *Mysql
  29. // tracer
  30. Tracer *trace.Config
  31. // key secret
  32. KeySecret *KeySecret
  33. // mail
  34. Mail *Mail
  35. // avratio
  36. Ratio *TagConf
  37. // upincome
  38. Income *TagConf
  39. // http client
  40. HTTPClient *bm.ClientConfig
  41. // berserker client
  42. DPClient *bm.ClientConfig
  43. // Host
  44. Host *Host
  45. // bm
  46. BM *bm.ServerConfig
  47. // hbase
  48. HBase *HBaseConfig
  49. // databus
  50. ArchiveSub *databus.Config
  51. //
  52. Bubble *BubbleConfig
  53. }
  54. // KeySecret.
  55. type KeySecret struct {
  56. Key string
  57. Secret string
  58. }
  59. // Mysql mysql config
  60. type Mysql struct {
  61. Growup *sql.Config
  62. Allowance *sql.Config
  63. }
  64. // Mail config
  65. type Mail struct {
  66. Host string
  67. Port int
  68. Username, Password string
  69. Send []*MailAddr
  70. }
  71. // MailAddr mail send addr.
  72. type MailAddr struct {
  73. Type int
  74. Addr []string
  75. }
  76. // Host is hosts
  77. type Host struct {
  78. Archive string
  79. VideoType string
  80. ColumnType string
  81. DataPlatform string
  82. ColumnAct string
  83. Profit string
  84. VC string
  85. Porder string
  86. Archives string
  87. API string
  88. }
  89. // TagConf tag config
  90. type TagConf struct {
  91. Hour int
  92. Sleep int64
  93. Num int64
  94. Limit int64
  95. }
  96. // HBaseConfig for new hbase client.
  97. type HBaseConfig struct {
  98. *hbase.Config
  99. WriteTimeout time.Duration
  100. ReadTimeout time.Duration
  101. }
  102. // BubbleConfig.
  103. type BubbleConfig struct {
  104. BRatio []*BRatio
  105. }
  106. // BRatio .
  107. type BRatio struct {
  108. BType int
  109. Ratio float64
  110. }
  111. func init() {
  112. flag.StringVar(&confPath, "conf", "", "default config path")
  113. }
  114. // Init init config.
  115. func Init() (err error) {
  116. if confPath != "" {
  117. _, err = toml.DecodeFile(confPath, &Conf)
  118. return
  119. }
  120. err = remote()
  121. return
  122. }
  123. func remote() (err error) {
  124. if client, err = conf.New(); err != nil {
  125. return
  126. }
  127. if err = load(); err != nil {
  128. return
  129. }
  130. go func() {
  131. for range client.Event() {
  132. log.Info("config reload")
  133. if load() != nil {
  134. log.Error("config reload error (%v)", err)
  135. }
  136. }
  137. }()
  138. return
  139. }
  140. func load() (err error) {
  141. var (
  142. s string
  143. ok bool
  144. tmpConf *Config
  145. )
  146. if s, ok = client.Toml2(); !ok {
  147. return errors.New("load config center error")
  148. }
  149. if _, err = toml.Decode(s, &tmpConf); err != nil {
  150. return errors.New("could not decode config")
  151. }
  152. *Conf = *tmpConf
  153. return
  154. }