conf.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/sql"
  9. "go-common/library/log"
  10. "go-common/library/net/http/blademaster"
  11. "go-common/library/queue/databus"
  12. "github.com/BurntSushi/toml"
  13. )
  14. var (
  15. confPath string
  16. client *conf.Client
  17. // Conf is global config object.
  18. Conf = &Config{}
  19. )
  20. // Config is project all config.
  21. type Config struct {
  22. Host *Host
  23. // interface XLog
  24. XLog *log.Config
  25. Redis *redis.Config
  26. // db
  27. DB *DB
  28. // http client
  29. HTTPClient *blademaster.ClientConfig
  30. // databus sub
  31. Bvc2VuSub *databus.Config
  32. VideoupSub *databus.Config
  33. ArcResultSub *databus.Config
  34. StatSub *databus.Config
  35. VideoshotSub2 *databus.Config
  36. // databus pub
  37. VideoupPub *databus.Config
  38. BlogPub *databus.Config
  39. CheckFrPub *databus.Config
  40. // rpc
  41. AccRPC *warden.ClientConfig
  42. // mail
  43. Mail *Mail
  44. Bm *blademaster.ServerConfig
  45. ManagerReport *databus.Config
  46. // others
  47. SpecialUp []int64
  48. Tels string
  49. ChangeDebug bool
  50. ChangeMid int64
  51. //灰度控制
  52. Debug bool
  53. DebugMid int64
  54. BvcConsumeTimeout int64
  55. }
  56. // Host is hosts
  57. type Host struct {
  58. Message string
  59. API string
  60. Monitor string
  61. Act string
  62. Bvc *BVC
  63. Push *PushC
  64. RecCover string
  65. }
  66. //BVC key
  67. type BVC struct {
  68. Bvc string
  69. GapKey string
  70. AppendKey string
  71. }
  72. //PushC 创作姬app推送消息配置
  73. type PushC struct {
  74. AppID string
  75. BusinessID string
  76. Token string
  77. }
  78. // DB is db config.
  79. type DB struct {
  80. Archive *sql.Config
  81. ArchiveRead *sql.Config
  82. Manager *sql.Config
  83. }
  84. //Mail 邮件配置
  85. type Mail struct {
  86. Host, Checkout string
  87. Port int
  88. Username, Password string
  89. Addr []*MailElemenet
  90. PrivateAddr []*MailElemenet
  91. }
  92. //MailElemenet 邮件接收人配置
  93. type MailElemenet struct {
  94. Type string
  95. Desc string
  96. Addr []string
  97. }
  98. func init() {
  99. flag.StringVar(&confPath, "conf", "", "config path")
  100. }
  101. // Init init config.
  102. func Init() (err error) {
  103. if confPath != "" {
  104. _, err = toml.DecodeFile(confPath, &Conf)
  105. return
  106. }
  107. err = remote()
  108. return
  109. }
  110. func remote() (err error) {
  111. if client, err = conf.New(); err != nil {
  112. return
  113. }
  114. if err = load(); err != nil {
  115. return
  116. }
  117. go func() {
  118. for range client.Event() {
  119. log.Info("config reload")
  120. if load() != nil {
  121. log.Error("config reload error (%v)", err)
  122. }
  123. }
  124. }()
  125. return
  126. }
  127. func load() (err error) {
  128. var (
  129. s string
  130. ok bool
  131. tmpConf *Config
  132. )
  133. if s, ok = client.Toml2(); !ok {
  134. return errors.New("load config center error")
  135. }
  136. if _, err = toml.Decode(s, &tmpConf); err != nil {
  137. return errors.New("could not decode config")
  138. }
  139. *Conf = *tmpConf
  140. return
  141. }