conf.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "time"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/conf"
  9. "go-common/library/database/sql"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/rpc"
  13. "go-common/library/queue/databus"
  14. xtime "go-common/library/time"
  15. "go-common/library/database/hbase.v2"
  16. "github.com/BurntSushi/toml"
  17. )
  18. // Conf global config.
  19. var (
  20. confPath string
  21. client *conf.Client
  22. // config
  23. Conf = &Config{}
  24. )
  25. // Config info.
  26. type Config struct {
  27. // base
  28. // customized property
  29. Property *Property
  30. // log
  31. Xlog *log.Config
  32. // db
  33. DB *sql.Config
  34. // databus
  35. Databus *DataSource
  36. // rpc to spy-service
  37. SpyRPC *rpc.ClientConfig
  38. HBase *HBaseConfig
  39. Memcache *memcache.Config
  40. // redis
  41. Redis *Redis
  42. // http client
  43. HTTPClient *bm.ClientConfig
  44. // HTTPServer
  45. HTTPServer *bm.ServerConfig
  46. }
  47. // HBaseConfig extra hbase config
  48. type HBaseConfig struct {
  49. *hbase.Config
  50. ReadTimeout time.Duration
  51. WriteTimeout time.Duration
  52. }
  53. // DataSource config all spy job dataSource
  54. type DataSource struct {
  55. EventData *databus.Config
  56. SpyStatData *databus.Config
  57. SecLogin *databus.Config
  58. }
  59. // Property config for biz logic.
  60. type Property struct {
  61. TaskTimer time.Duration
  62. UserInfoShard int64
  63. Debug bool
  64. ConfigLoadTick xtime.Duration
  65. BlockTick xtime.Duration
  66. BlockWaitTick xtime.Duration
  67. LoadEventTick xtime.Duration
  68. BlockAccountURL string
  69. HistoryShard int64
  70. BlockEvent int64
  71. Block *struct {
  72. CycleTimes int64 // unit per seconds
  73. CycleCron string
  74. }
  75. ReportCron string
  76. ActivityEvents []string
  77. }
  78. // Redis conf.
  79. type Redis struct {
  80. *redis.Config
  81. Expire xtime.Duration
  82. MsgUUIDExpire xtime.Duration
  83. }
  84. func init() {
  85. flag.StringVar(&confPath, "conf", "", "config path")
  86. }
  87. // Init init conf.
  88. func Init() (err error) {
  89. if confPath == "" {
  90. return configCenter()
  91. }
  92. _, err = toml.DecodeFile(confPath, &Conf)
  93. return
  94. }
  95. func configCenter() (err error) {
  96. if client, err = conf.New(); err != nil {
  97. panic(err)
  98. }
  99. if err = load(); err != nil {
  100. return
  101. }
  102. go func() {
  103. for range client.Event() {
  104. log.Info("config reload")
  105. if load() != nil {
  106. log.Error("config reload error (%v)", err)
  107. }
  108. }
  109. }()
  110. return
  111. }
  112. func load() (err error) {
  113. var (
  114. s string
  115. ok bool
  116. tmpConf *Config
  117. )
  118. if s, ok = client.Toml2(); !ok {
  119. return errors.New("load config center error")
  120. }
  121. if _, err = toml.Decode(s, &tmpConf); err != nil {
  122. return errors.New("could not decode config")
  123. }
  124. *Conf = *tmpConf
  125. return
  126. }