conf.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/hbase.v2"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/queue/databus"
  12. xtime "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. var (
  16. confPath string
  17. client *conf.Client
  18. Conf = &Config{}
  19. )
  20. // Config def.
  21. type Config struct {
  22. // base
  23. // log
  24. Log *log.Config
  25. // http
  26. BM *bm.ServerConfig
  27. Figure figure
  28. // databus
  29. DataSource *DataSource
  30. // hbase
  31. HBase *HBaseConfig
  32. // redis
  33. Redis *Redis
  34. // mysql
  35. Mysql *sql.Config
  36. }
  37. type figure struct {
  38. Sync bool
  39. SpyPath string
  40. VipPath string
  41. Lawful int32
  42. Wide int32
  43. Friendly int32
  44. Bounty int32
  45. Creativity int32
  46. }
  47. // DataSource config all figure job dataSource
  48. type DataSource struct {
  49. AccountExp *databus.Config
  50. AccountReg *databus.Config
  51. Vip *databus.Config
  52. Spy *databus.Config
  53. Coin *databus.Config
  54. ReplyInfo *databus.Config
  55. Pay *databus.Config
  56. Blocked *databus.Config
  57. Danmaku *databus.Config
  58. }
  59. // Redis conf.
  60. type Redis struct {
  61. *redis.Config
  62. Expire xtime.Duration
  63. WaiteMidExpire xtime.Duration
  64. }
  65. // HBaseConfig extra hbase config
  66. type HBaseConfig struct {
  67. *hbase.Config
  68. ReadTimeout xtime.Duration
  69. WriteTimeout xtime.Duration
  70. }
  71. func init() {
  72. flag.StringVar(&confPath, "conf", "", "config path")
  73. }
  74. // Init init conf.
  75. func Init() (err error) {
  76. if confPath == "" {
  77. return configCenter()
  78. }
  79. _, err = toml.DecodeFile(confPath, &Conf)
  80. return
  81. }
  82. func configCenter() (err error) {
  83. if client, err = conf.New(); err != nil {
  84. panic(err)
  85. }
  86. if err = load(); err != nil {
  87. return
  88. }
  89. go func() {
  90. for range client.Event() {
  91. log.Info("config reload")
  92. if load() != nil {
  93. log.Error("config reload error (%v)", err)
  94. }
  95. }
  96. }()
  97. return
  98. }
  99. func load() (err error) {
  100. var (
  101. s string
  102. ok bool
  103. tmpConf *Config
  104. )
  105. if s, ok = client.Toml2(); !ok {
  106. return errors.New("load config center error")
  107. }
  108. if _, err = toml.Decode(s, &tmpConf); err != nil {
  109. return errors.New("could not decode config")
  110. }
  111. *Conf = *tmpConf
  112. return
  113. }