conf.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/app/service/main/coin/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/conf"
  9. "go-common/library/database/sql"
  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/verify"
  14. "go-common/library/net/rpc"
  15. "go-common/library/net/rpc/warden"
  16. "go-common/library/net/trace"
  17. "go-common/library/queue/databus"
  18. "go-common/library/time"
  19. "github.com/BurntSushi/toml"
  20. )
  21. // global var
  22. var (
  23. confPath string
  24. Conf = &Config{}
  25. client *conf.Client
  26. )
  27. // Config config.
  28. type Config struct {
  29. // rpc server2
  30. RPCServer *rpc.ServerConfig
  31. GRPC *warden.ServerConfig
  32. // BM
  33. BM *bm.ServerConfig
  34. // db
  35. DB *DB
  36. // redis
  37. Redis *Redis
  38. // databus
  39. DbBigData *databus.Config
  40. DbCoinJob *databus.Config
  41. // new stat.
  42. Stat *Stat
  43. // rpc client
  44. MemberRPC *warden.ClientConfig
  45. // tracer
  46. Tracer *trace.Config
  47. // verify
  48. Verify *verify.Config
  49. // Log
  50. Log *log.Config
  51. Report *log.AgentConfig
  52. // ding url
  53. TagURL string
  54. HTTPClient *bm.ClientConfig
  55. // Antispam
  56. Antispam *antispam.Config
  57. // Memcache .
  58. Memcache *Memcache
  59. Businesses []*model.Business
  60. Coin *Coin
  61. UserReport *databus.Config
  62. StatMerge *StatMerge
  63. }
  64. // Coin .
  65. type Coin struct {
  66. ESLogURL string
  67. }
  68. // StatMerge .
  69. type StatMerge struct {
  70. Business string
  71. Target int64
  72. Sources []int64
  73. }
  74. // Stat databus stat conf.
  75. type Stat struct {
  76. Databus *databus.Config
  77. }
  78. // DB db config.
  79. type DB struct {
  80. Coin *sql.Config
  81. }
  82. // Memcache mc config.
  83. type Memcache struct {
  84. *memcache.Config
  85. Expire time.Duration
  86. ExpExpire time.Duration
  87. }
  88. // Redis redis conf.
  89. type Redis struct {
  90. *redis.Config
  91. Expire time.Duration
  92. }
  93. func configCenter() (err error) {
  94. if client, err = conf.New(); err != nil {
  95. panic(err)
  96. }
  97. err = load()
  98. return
  99. }
  100. func load() (err error) {
  101. var (
  102. s string
  103. ok bool
  104. tmpConf *Config
  105. )
  106. if s, ok = client.Toml2(); !ok {
  107. return errors.New("load config center error")
  108. }
  109. if _, err = toml.Decode(s, &tmpConf); err != nil {
  110. return errors.New("could not decode config")
  111. }
  112. *Conf = *tmpConf
  113. return
  114. }
  115. func init() {
  116. flag.StringVar(&confPath, "conf", "", "config path")
  117. }
  118. // Init init conf.
  119. func Init() (err error) {
  120. if confPath == "" {
  121. return configCenter()
  122. }
  123. _, err = toml.DecodeFile(confPath, &Conf)
  124. return
  125. }