conf.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. ecode "go-common/library/ecode/tip"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/middleware/verify"
  12. "go-common/library/net/trace"
  13. "go-common/library/queue/databus"
  14. "github.com/BurntSushi/toml"
  15. )
  16. var (
  17. confPath string
  18. client *conf.Client
  19. // Conf config
  20. Conf = &Config{}
  21. )
  22. // Config .
  23. type Config struct {
  24. Log *log.Config
  25. BM *bm.ServerConfig
  26. Verify *verify.Config
  27. Tracer *trace.Config
  28. Redis *redis.Config
  29. MySQL *sql.Config
  30. LiveAppMySQL *sql.Config
  31. Ecode *ecode.Config
  32. LiveDanmuSub *databus.Config //发送弹幕回调配置
  33. LiveGiftSendSub *databus.Config
  34. }
  35. func init() {
  36. flag.StringVar(&confPath, "conf", "", "default config path")
  37. }
  38. // Init init conf
  39. func Init() error {
  40. if confPath != "" {
  41. return local()
  42. }
  43. return remote()
  44. }
  45. func local() (err error) {
  46. _, err = toml.DecodeFile(confPath, &Conf)
  47. return
  48. }
  49. func remote() (err error) {
  50. if client, err = conf.New(); err != nil {
  51. return
  52. }
  53. if err = load(); err != nil {
  54. return
  55. }
  56. go func() {
  57. for range client.Event() {
  58. log.Info("config reload")
  59. if load() != nil {
  60. log.Error("config reload error (%v)", err)
  61. }
  62. }
  63. }()
  64. return
  65. }
  66. func load() (err error) {
  67. var (
  68. s string
  69. ok bool
  70. tmpConf *Config
  71. )
  72. if s, ok = client.Toml2(); !ok {
  73. return errors.New("load config center error")
  74. }
  75. if _, err = toml.Decode(s, &tmpConf); err != nil {
  76. return errors.New("could not decode config")
  77. }
  78. *Conf = *tmpConf
  79. return
  80. }