conf.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "time"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "go-common/library/log/infoc"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/trace"
  13. "go-common/library/queue/databus"
  14. xtime "go-common/library/time"
  15. hbase "go-common/library/database/hbase.v2"
  16. "github.com/BurntSushi/toml"
  17. )
  18. var (
  19. // ConfPath .
  20. ConfPath string
  21. client *conf.Client
  22. // Conf .
  23. Conf = &Config{}
  24. )
  25. // Config .
  26. type Config struct {
  27. // log
  28. XLog *log.Config
  29. // tracer
  30. Tracer *trace.Config
  31. // hbase
  32. HBase *HBaseConfig
  33. // business
  34. Business *Business
  35. // xhttp
  36. HTTPServer *bm.ServerConfig
  37. // http client
  38. HTTPClient *bm.ClientConfig
  39. // database
  40. DB map[string]*sql.Config
  41. // es cluster
  42. Es map[string]EsInfo
  43. // databus
  44. Databus map[string]*databus.Config
  45. // infoc
  46. InfoC map[string]*infoc.Config
  47. // sms
  48. SMS *SMS
  49. }
  50. // HBaseConfig combine with hbase.Config add ReadTimeout, WriteTimeout
  51. type HBaseConfig struct {
  52. *hbase.Config
  53. // extra config
  54. ReadTimeout xtime.Duration
  55. ReadsTimeout xtime.Duration
  56. WriteTimeout xtime.Duration
  57. WritesTimeout xtime.Duration
  58. }
  59. // Consumer .
  60. type Consumer struct {
  61. GroupID string
  62. Topic []string
  63. Offset string
  64. Addrs []string
  65. }
  66. // Business .
  67. type Business struct {
  68. Env string
  69. Index bool
  70. }
  71. // Redis search redis.
  72. type Redis struct {
  73. *redis.Config
  74. Expire time.Duration
  75. }
  76. // EsInfo (deprecated).
  77. type EsInfo struct {
  78. Addr []string
  79. }
  80. // SMS config
  81. type SMS struct {
  82. Phone string
  83. Token string
  84. Interval int64
  85. }
  86. // init .
  87. func init() {
  88. flag.StringVar(&ConfPath, "conf", "", "config path")
  89. }
  90. // Init .
  91. func Init() (err error) {
  92. if ConfPath != "" {
  93. return local()
  94. }
  95. return remote()
  96. }
  97. // local .
  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. }