conf.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. ecode "go-common/library/ecode/tip"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/middleware/auth"
  12. "go-common/library/net/http/blademaster/middleware/verify"
  13. "go-common/library/net/rpc"
  14. "go-common/library/net/rpc/warden"
  15. "go-common/library/net/trace"
  16. "go-common/library/time"
  17. "github.com/BurntSushi/toml"
  18. )
  19. // global var
  20. var (
  21. confPath string
  22. client *conf.Client
  23. // Conf config
  24. Conf = &Config{}
  25. )
  26. // Config config set
  27. type Config struct {
  28. // base
  29. // elk
  30. Log *log.Config
  31. // App
  32. App *bm.App
  33. // rpc server2
  34. RPCServer2 *rpc.ServerConfig
  35. // tracer
  36. Tracer *trace.Config
  37. // bm
  38. BM *bm.ServerConfig
  39. // Ecode
  40. Ecode *ecode.Config
  41. // rpc
  42. FavoriteRPC *rpc.ClientConfig
  43. // grpc
  44. ArcClient *warden.ClientConfig
  45. // Mysql
  46. Mysql *sql.Config
  47. // Redis
  48. Redis *Redis
  49. // HTTP client
  50. HTTPClient *bm.ClientConfig
  51. // Host
  52. Host *Host
  53. // Auth
  54. Auth *auth.Config
  55. // verify
  56. Verify *verify.Config
  57. // reload
  58. Rule *Rule
  59. // leidata
  60. Leidata *Leidata
  61. }
  62. // Host hosts.
  63. type Host struct {
  64. Search string
  65. }
  66. // Redis redis struct
  67. type Redis struct {
  68. *redis.Config
  69. FilterExpire time.Duration
  70. ListExpire time.Duration
  71. }
  72. // Rule rule .
  73. type Rule struct {
  74. KnockTree time.Duration
  75. }
  76. // Leidata lei da data .
  77. type Leidata struct {
  78. Timeout time.Duration
  79. AfterSleep time.Duration
  80. EndSleep time.Duration
  81. Retry int
  82. URL string
  83. Key string
  84. LolPlayersCron string
  85. DotaPlayersCron string
  86. InfoCron string
  87. }
  88. func init() {
  89. flag.StringVar(&confPath, "conf", "", "default config path")
  90. }
  91. // Init init conf
  92. func Init() error {
  93. if confPath != "" {
  94. return local()
  95. }
  96. return remote()
  97. }
  98. func local() (err error) {
  99. _, err = toml.DecodeFile(confPath, &Conf)
  100. return
  101. }
  102. func remote() (err error) {
  103. if client, err = conf.New(); err != nil {
  104. return
  105. }
  106. if err = load(); err != nil {
  107. return
  108. }
  109. go func() {
  110. for range client.Event() {
  111. log.Info("config reload")
  112. if load() != nil {
  113. log.Error("config reload error (%v)", err)
  114. }
  115. }
  116. }()
  117. return
  118. }
  119. func load() (err error) {
  120. var (
  121. s string
  122. ok bool
  123. tmpConf *Config
  124. )
  125. if s, ok = client.Toml2(); !ok {
  126. return errors.New("load config center error")
  127. }
  128. if _, err = toml.Decode(s, &tmpConf); err != nil {
  129. return errors.New("could not decode config")
  130. }
  131. *Conf = *tmpConf
  132. return
  133. }