conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. xlog "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/verify"
  11. "go-common/library/net/rpc"
  12. "go-common/library/net/trace"
  13. "go-common/library/queue/databus"
  14. "go-common/library/time"
  15. "github.com/BurntSushi/toml"
  16. )
  17. var (
  18. confPath string
  19. // Conf is global config
  20. Conf *Config
  21. )
  22. // Config service config
  23. type Config struct {
  24. Version string `toml:"version"`
  25. // reload
  26. Reload *ReloadInterval
  27. // rpc server2
  28. RPCServer *rpc.ServerConfig
  29. // verify
  30. Verify *verify.Config
  31. // http
  32. BM *BM
  33. // tracer
  34. Tracer *trace.Config
  35. // db
  36. DB *DB
  37. // httpClient
  38. HTTPClient *bm.ClientConfig
  39. // Host
  40. Host *Host
  41. // XLog
  42. XLog *xlog.Config
  43. // rpc
  44. LocationRPC *rpc.ClientConfig
  45. ArchiveRPC *rpc.ClientConfig
  46. // redis
  47. Redis *Redis
  48. // hash number
  49. HashNum int64
  50. // databus
  51. ArchiveSub *databus.Config
  52. // qiye wechat
  53. WeChatToken string
  54. WeChatSecret string
  55. WeChantUsers []string
  56. // kai guan off line
  57. MonitorArchive bool
  58. MonitorURL bool
  59. // sp limit
  60. SpLimit time.Duration
  61. }
  62. // BM http
  63. type BM struct {
  64. Inner *bm.ServerConfig
  65. Local *bm.ServerConfig
  66. }
  67. // ReloadInterval define reolad config
  68. type ReloadInterval struct {
  69. Ad time.Duration
  70. }
  71. // Host defeine host info
  72. type Host struct {
  73. DataPlat string
  74. Ad string
  75. }
  76. // DB define MySQL config
  77. type DB struct {
  78. Res *sql.Config
  79. Ads *sql.Config
  80. Show *sql.Config
  81. Manager *sql.Config
  82. }
  83. // Redis define Redis config
  84. type Redis struct {
  85. Ads *struct {
  86. *redis.Config
  87. Expire time.Duration
  88. }
  89. }
  90. func init() {
  91. flag.StringVar(&confPath, "conf", "", "default config path")
  92. }
  93. // Init init config
  94. func Init() (err error) {
  95. if confPath != "" {
  96. _, err = toml.DecodeFile(confPath, &Conf)
  97. return
  98. }
  99. err = configCenter()
  100. return
  101. }
  102. // configCenter ugc
  103. func configCenter() (err error) {
  104. var (
  105. client *conf.Client
  106. c string
  107. ok bool
  108. )
  109. if client, err = conf.New(); err != nil {
  110. panic(err)
  111. }
  112. if c, ok = client.Toml2(); !ok {
  113. err = errors.New("load config center error")
  114. return
  115. }
  116. _, err = toml.Decode(c, &Conf)
  117. go func() {
  118. for e := range client.Event() {
  119. xlog.Error("get config from config center error(%v)", e)
  120. }
  121. }()
  122. return
  123. }