conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/sql"
  9. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/verify"
  13. "go-common/library/net/trace"
  14. "github.com/BurntSushi/toml"
  15. "go-common/library/queue/databus"
  16. "go-common/library/database/hbase.v2"
  17. "go-common/library/net/rpc/liverpc"
  18. xtime "go-common/library/time"
  19. )
  20. var (
  21. confPath string
  22. client *conf.Client
  23. // Conf config
  24. Conf = &Config{}
  25. )
  26. // Config .
  27. type Config struct {
  28. Log *log.Config
  29. BM *bm.ServerConfig
  30. Verify *verify.Config
  31. Tracer *trace.Config
  32. Redis *redis.Config
  33. Memcache *memcache.Config
  34. MySQL *sql.Config
  35. Ecode *ecode.Config
  36. LiveRpc map[string]*liverpc.ClientConfig
  37. //DataBus
  38. DataBus *DataBus
  39. Group *Group
  40. SearchHBase *hbaseConf
  41. MigrateNum int
  42. }
  43. type DataBus struct {
  44. RoomInfo *databus.Config
  45. Attention *databus.Config
  46. UserName *databus.Config
  47. PushSearch *databus.Config
  48. }
  49. // Group group.
  50. type Group struct {
  51. RoomInfo *GroupConf
  52. Attention *GroupConf
  53. UserInfo *GroupConf
  54. }
  55. // GroupConf group conf.
  56. type GroupConf struct {
  57. Num int
  58. Chan int
  59. }
  60. type hbaseConf struct {
  61. hbase.Config
  62. ReadTimeout xtime.Duration
  63. ReadsTimeout xtime.Duration
  64. WriteTimeout xtime.Duration
  65. WritesTimeout xtime.Duration
  66. }
  67. func init() {
  68. flag.StringVar(&confPath, "conf", "", "default config path")
  69. }
  70. // Init init conf
  71. func Init() error {
  72. if confPath != "" {
  73. return local()
  74. }
  75. return remote()
  76. }
  77. func local() (err error) {
  78. _, err = toml.DecodeFile(confPath, &Conf)
  79. return
  80. }
  81. func remote() (err error) {
  82. if client, err = conf.New(); err != nil {
  83. return
  84. }
  85. if err = load(); err != nil {
  86. return
  87. }
  88. go func() {
  89. for range client.Event() {
  90. log.Info("config reload")
  91. if load() != nil {
  92. log.Error("config reload error (%v)", err)
  93. }
  94. }
  95. }()
  96. return
  97. }
  98. func load() (err error) {
  99. var (
  100. s string
  101. ok bool
  102. tmpConf *Config
  103. )
  104. if s, ok = client.Toml2(); !ok {
  105. return errors.New("load config center error")
  106. }
  107. if _, err = toml.Decode(s, &tmpConf); err != nil {
  108. return errors.New("could not decode config")
  109. }
  110. *Conf = *tmpConf
  111. return
  112. }