conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/trace"
  11. "go-common/library/queue/databus"
  12. "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. var (
  16. confPath string
  17. // Conf conf.
  18. Conf = &Config{}
  19. client *conf.Client
  20. )
  21. // Config config.
  22. type Config struct {
  23. // Xlog log
  24. Xlog *log.Config
  25. // Tracer tracer
  26. Tracer *trace.Config
  27. // DB db
  28. DB *DB
  29. // Memcache memcache
  30. Memcache *Memcache
  31. // Game
  32. Game *Game
  33. // HTTP
  34. BM *bm.ServerConfig
  35. // Group
  36. Group *Group
  37. // DataBus databus
  38. DataBus *DataBus
  39. }
  40. // Game game notify conf.
  41. type Game struct {
  42. AppIDs []int32
  43. DelCacheURI string
  44. Client *bm.ClientConfig
  45. }
  46. // Group multi group config collection.
  47. type Group struct {
  48. BinLog *GroupConfig
  49. EncryptTrans *GroupConfig
  50. }
  51. // GroupConfig group config.
  52. type GroupConfig struct {
  53. // Size merge size
  54. Size int
  55. // Num merge goroutine num
  56. Num int
  57. // Ticker duration of submit merges when no new message
  58. Ticker time.Duration
  59. // Chan size of merge chan and done chan
  60. Chan int
  61. }
  62. // DataBus databus infomation
  63. type DataBus struct {
  64. BinLogSub *databus.Config
  65. EncryptTransSub *databus.Config
  66. }
  67. // DB db config.
  68. type DB struct {
  69. Cloud *sql.Config
  70. }
  71. // Memcache general memcache config.
  72. type Memcache struct {
  73. *memcache.Config
  74. Expire time.Duration
  75. }
  76. func init() {
  77. flag.StringVar(&confPath, "conf", "", "default config path")
  78. }
  79. // Init init config.
  80. func Init() (err error) {
  81. if confPath != "" {
  82. return local()
  83. }
  84. return remote()
  85. }
  86. func local() (err error) {
  87. _, err = toml.DecodeFile(confPath, &Conf)
  88. return
  89. }
  90. func remote() (err error) {
  91. if client, err = conf.New(); err != nil {
  92. return
  93. }
  94. if err = load(); err != nil {
  95. return
  96. }
  97. go func() {
  98. for range client.Event() {
  99. log.Info("config reload")
  100. if load() != nil {
  101. log.Error("config reload error (%v)", err)
  102. }
  103. }
  104. }()
  105. return
  106. }
  107. func load() (err error) {
  108. var (
  109. s string
  110. ok bool
  111. tmpConf *Config
  112. )
  113. if s, ok = client.Toml2(); !ok {
  114. return errors.New("load config center error")
  115. }
  116. if _, err = toml.Decode(s, &tmpConf); err != nil {
  117. return errors.New("could not decode config")
  118. }
  119. *Conf = *tmpConf
  120. return
  121. }