conf.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/queue/databus"
  12. "github.com/BurntSushi/toml"
  13. )
  14. // is
  15. var (
  16. confPath string
  17. Conf = &Config{}
  18. client *conf.Client
  19. )
  20. // Config is
  21. type Config struct {
  22. // Env
  23. Env string
  24. // interface XLog
  25. XLog *log.Config
  26. // host
  27. Host *Host
  28. // httpClinet
  29. HTTPClient *bm.ClientConfig
  30. // databus
  31. VideoupSub *databus.Config
  32. DmSub *databus.Config
  33. ArchiveResultPub *databus.Config
  34. DmPub *databus.Config
  35. CacheSub *databus.Config
  36. AccountNotifySub *databus.Config
  37. // rpc
  38. ArchiveServices []*rpc.ClientConfig
  39. Dm2RPC *rpc.ClientConfig
  40. // mail
  41. Mail *Mail
  42. // DB
  43. DB *DB
  44. // BM
  45. BM *bm.ServerConfig
  46. // Redis
  47. Redis *redis.Config
  48. // ChanSize aid%ChanSize
  49. ChanSize int
  50. PGCAsync int
  51. UGCAsync int
  52. MonitorSize int
  53. // qiye wechat
  54. WeChatToken string
  55. WeChatSecret string
  56. WeChantUsers string
  57. }
  58. // Mail is
  59. type Mail struct {
  60. Host string
  61. Port int
  62. Username, Password string
  63. Bangumi, Movie []string
  64. }
  65. // Host is
  66. type Host struct {
  67. APICo string
  68. }
  69. // DB is db config.
  70. type DB struct {
  71. Archive *sql.Config
  72. Result *sql.Config
  73. }
  74. func init() {
  75. flag.StringVar(&confPath, "conf", "", "default config path")
  76. }
  77. // Init init config.
  78. func Init() (err error) {
  79. if confPath != "" {
  80. _, err = toml.DecodeFile(confPath, &Conf)
  81. return
  82. }
  83. err = remote()
  84. return
  85. }
  86. func remote() (err error) {
  87. if client, err = conf.New(); err != nil {
  88. return
  89. }
  90. if err = load(); err != nil {
  91. return
  92. }
  93. go func() {
  94. for range client.Event() {
  95. log.Info("config reload")
  96. if load() != nil {
  97. log.Error("config reload error (%v)", err)
  98. }
  99. }
  100. }()
  101. return
  102. }
  103. func load() (err error) {
  104. var (
  105. s string
  106. ok bool
  107. tmpConf = &Config{}
  108. )
  109. if s, ok = client.Toml2(); !ok {
  110. return errors.New("load config center error")
  111. }
  112. if _, err = toml.Decode(s, tmpConf); err != nil {
  113. return errors.New("could not decode config")
  114. }
  115. *Conf = *tmpConf
  116. return
  117. }