conf.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/elastic"
  8. "go-common/library/database/sql"
  9. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/antispam"
  13. "go-common/library/net/http/blademaster/middleware/auth"
  14. "go-common/library/net/http/blademaster/middleware/verify"
  15. "go-common/library/net/rpc"
  16. "go-common/library/net/rpc/warden"
  17. "go-common/library/net/trace"
  18. "go-common/library/queue/databus"
  19. "go-common/library/time"
  20. "github.com/BurntSushi/toml"
  21. )
  22. var (
  23. confPath string
  24. client *conf.Client
  25. // Conf export config variable
  26. Conf = &Config{}
  27. )
  28. // Config dm config file
  29. type Config struct {
  30. // base
  31. // ecode
  32. Ecode *ecode.Config
  33. // xlog
  34. Xlog *log.Config
  35. Auth *auth.Config
  36. Verify *verify.Config
  37. // http client
  38. HTTPClient *bm.ClientConfig
  39. ArchiveRPC *rpc.ClientConfig
  40. AccountRPC *warden.ClientConfig
  41. AssistRPC *rpc.ClientConfig
  42. DMRPC *rpc.ClientConfig
  43. // http server
  44. HTTPServer *bm.ServerConfig
  45. // db
  46. DB *DB
  47. // redis
  48. Redis *Redis
  49. // tracer
  50. Tracer *trace.Config
  51. // databus
  52. Databus *databus.Config
  53. // Antispam
  54. Antispam *antispam.Config
  55. Host Host
  56. ES *elastic.Config
  57. }
  58. // Host hosts used in dm admin
  59. type Host struct {
  60. API string
  61. Archive string
  62. Message string
  63. }
  64. // DB mysql database instance
  65. type DB struct {
  66. DM *sql.Config
  67. DMMetaReader *sql.Config
  68. DMWriter *sql.Config
  69. }
  70. // Redis redis instance
  71. type Redis struct {
  72. DM *DMRedis
  73. }
  74. // DMRedis redis instance of dm
  75. type DMRedis struct {
  76. *redis.Config
  77. DMIDExpire time.Duration
  78. LockExpire time.Duration
  79. IndexExpire time.Duration
  80. VideoExpire time.Duration
  81. }
  82. func init() {
  83. flag.StringVar(&confPath, "conf", "", "config path")
  84. }
  85. //Init int config
  86. func Init() error {
  87. if confPath != "" {
  88. return local()
  89. }
  90. return remote()
  91. }
  92. func local() (err error) {
  93. _, err = toml.DecodeFile(confPath, &Conf)
  94. return
  95. }
  96. func remote() (err error) {
  97. if client, err = conf.New(); err != nil {
  98. return
  99. }
  100. if err = load(); err != nil {
  101. return
  102. }
  103. go func() {
  104. for range client.Event() {
  105. log.Info("config reload")
  106. if load() != nil {
  107. log.Error("config reload error (%v)", err)
  108. }
  109. }
  110. }()
  111. return
  112. }
  113. func load() (err error) {
  114. var (
  115. s string
  116. ok bool
  117. tmpConf *Config
  118. )
  119. if s, ok = client.Toml2(); !ok {
  120. return errors.New("load config center error")
  121. }
  122. if _, err = toml.Decode(s, &tmpConf); err != nil {
  123. return errors.New("could not decode config")
  124. }
  125. *Conf = *tmpConf
  126. return
  127. }