conf.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/middleware/verify"
  12. "go-common/library/net/rpc"
  13. "go-common/library/net/trace"
  14. "go-common/library/queue/databus"
  15. "go-common/library/time"
  16. "go-common/library/database/hbase.v2"
  17. "github.com/BurntSushi/toml"
  18. )
  19. // Conf global variable.
  20. var (
  21. Conf = &Config{}
  22. client *conf.Client
  23. confPath string
  24. )
  25. // Config struct of conf.
  26. type Config struct {
  27. // app
  28. App *bm.App
  29. // tracer
  30. Tracer *trace.Config
  31. // goroutine sleep
  32. Tick time.Duration
  33. // http server
  34. BM *bm.ServerConfig
  35. // log
  36. Log *log.Config
  37. // verify
  38. Verify *verify.Config
  39. LocationRPC *rpc.ClientConfig
  40. DataBus *databus.Config
  41. Mysql *Mysql
  42. Expect *Expect
  43. // redis
  44. Redis *Redis
  45. // httpClient
  46. HTTPClient *bm.ClientConfig
  47. // rpc
  48. RPCServer *rpc.ServerConfig
  49. // HBase
  50. HBase *HBaseConfig
  51. // mc
  52. Memcache *Memcache
  53. }
  54. func init() {
  55. flag.StringVar(&confPath, "conf", "", "default config path")
  56. }
  57. // HBaseConfig extra hbase config for compatible
  58. type HBaseConfig struct {
  59. *hbase.Config
  60. WriteTimeout time.Duration
  61. ReadTimeout time.Duration
  62. }
  63. // Memcache mc config.
  64. type Memcache struct {
  65. *memcache.Config
  66. Expire time.Duration
  67. }
  68. // Mysql conf.
  69. type Mysql struct {
  70. Secure *sql.Config
  71. DDL *sql.Config
  72. }
  73. // Redis redis conf.
  74. type Redis struct {
  75. *redis.Config
  76. Expire time.Duration
  77. DoubleCheck time.Duration
  78. }
  79. // Expect Login expection config.
  80. type Expect struct {
  81. Top int64 // login loc count.
  82. Count int64 // login time count.
  83. CloseCount int64 // user close count.
  84. Rand int64 // rand ratio
  85. DoubleCheck int64 // double login check location count.
  86. }
  87. // Init create config instance.
  88. func Init() (err error) {
  89. if confPath != "" {
  90. return local()
  91. }
  92. return remote()
  93. }
  94. func local() (err error) {
  95. _, err = toml.DecodeFile(confPath, &Conf)
  96. return
  97. }
  98. func remote() (err error) {
  99. if client, err = conf.New(); err != nil {
  100. return
  101. }
  102. if err = load(); err != nil {
  103. return
  104. }
  105. go func() {
  106. for range client.Event() {
  107. log.Info("config reload")
  108. if load() != nil {
  109. log.Error("config reload error (%v)", err)
  110. }
  111. }
  112. }()
  113. return
  114. }
  115. func load() (err error) {
  116. var (
  117. s string
  118. ok bool
  119. tmpConf *Config
  120. )
  121. if s, ok = client.Toml2(); !ok {
  122. return errors.New("load config center error")
  123. }
  124. if _, err = toml.Decode(s, &tmpConf); err != nil {
  125. return errors.New("could not decode config")
  126. }
  127. *Conf = *tmpConf
  128. return
  129. }