conf.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/net/http/blademaster"
  9. httpx "go-common/library/net/http/blademaster"
  10. "go-common/library/net/rpc"
  11. "go-common/library/net/trace"
  12. "go-common/library/queue/databus"
  13. "go-common/library/time"
  14. "go-common/library/database/hbase.v2"
  15. "github.com/BurntSushi/toml"
  16. )
  17. // Conf global variable.
  18. var (
  19. Conf = &Config{}
  20. client *conf.Client
  21. confPath string
  22. )
  23. // Config struct of conf.
  24. type Config struct {
  25. // base
  26. // Tracer tracer
  27. Tracer *trace.Config
  28. // Xlog log
  29. Xlog *log.Config
  30. // BM
  31. BM *blademaster.ServerConfig
  32. // URI uri
  33. URI *URI
  34. // Game game
  35. Game *Game
  36. // RPC rpc
  37. RPC *RPC
  38. // DB db
  39. DB *DB
  40. // HTTPClient httpx client
  41. HTTPClient *httpx.ClientConfig
  42. // Group group.
  43. Group *Group
  44. // DataBus databus
  45. DataBus *DataBus
  46. // HBase hbase
  47. HBase *HBase
  48. Encode *Encode
  49. Sync *Sync
  50. }
  51. // Encode encode
  52. type Encode struct {
  53. AesKey string
  54. Salt string
  55. }
  56. // HBase multi hbase.
  57. type HBase struct {
  58. LoginLog *HBaseConfig
  59. PwdLog *HBaseConfig
  60. }
  61. // HBaseConfig .
  62. type HBaseConfig struct {
  63. *hbase.Config
  64. ReadTimeout time.Duration
  65. WriteTimeout time.Duration
  66. }
  67. // URI multi uri.
  68. type URI struct {
  69. SetToken string
  70. DelCache string
  71. }
  72. // RPC multi rpc conf collection.
  73. type RPC struct {
  74. IdentifyGame *rpc.ClientConfig
  75. }
  76. // Game game notify conf.
  77. type Game struct {
  78. AppIDs []int32
  79. DelCacheURI string
  80. Client *httpx.ClientConfig
  81. }
  82. // Group multi group config collection.
  83. type Group struct {
  84. AsoBinLog *GroupConfig
  85. User *GroupConfig
  86. Log *GroupConfig
  87. ContactBindLog *GroupConfig
  88. PwdLog *GroupConfig
  89. AuthBinLog *GroupConfig
  90. }
  91. // GroupConfig group config.
  92. type GroupConfig struct {
  93. // Size merge size
  94. Size int
  95. // Num merge goroutine num
  96. Num int
  97. // Ticker duration of submit merges when no new message
  98. Ticker time.Duration
  99. // Chan size of merge chan and done chan
  100. Chan int
  101. }
  102. // DataBus multi databus collection.
  103. type DataBus struct {
  104. AsoBinLog *databus.Config
  105. User *databus.Config
  106. Log *databus.Config
  107. ContactBindLog *databus.Config
  108. UserLog *databus.Config
  109. PwdLog *databus.Config
  110. AuthBinLog *databus.Config
  111. }
  112. // DB db config.
  113. type DB struct {
  114. Log *sql.Config
  115. ASO *sql.Config
  116. }
  117. // Sync config.
  118. type Sync struct {
  119. SyncPwdID int64
  120. }
  121. func local() (err error) {
  122. _, err = toml.DecodeFile(confPath, &Conf)
  123. return
  124. }
  125. func remote() (err error) {
  126. if client, err = conf.New(); err != nil {
  127. return
  128. }
  129. if err = load(); err != nil {
  130. return
  131. }
  132. go func() {
  133. for range client.Event() {
  134. log.Info("config reload")
  135. if load() != nil {
  136. log.Error("config reload error (%v)", err)
  137. }
  138. }
  139. }()
  140. return
  141. }
  142. func load() (err error) {
  143. var (
  144. s string
  145. ok bool
  146. tmpConf *Config
  147. )
  148. if s, ok = client.Toml2(); !ok {
  149. return errors.New("load config center error")
  150. }
  151. if _, err = toml.Decode(s, &tmpConf); err != nil {
  152. return errors.New("could not decode config")
  153. }
  154. *Conf = *tmpConf
  155. return
  156. }
  157. func init() {
  158. flag.StringVar(&confPath, "conf", "", "default config path")
  159. }
  160. // Init int config
  161. func Init() error {
  162. if confPath != "" {
  163. return local()
  164. }
  165. return remote()
  166. }