conf.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package conf
  2. import (
  3. "flag"
  4. "go-common/library/database/elastic"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/rpc"
  12. "go-common/library/net/rpc/warden"
  13. "go-common/library/net/trace"
  14. "go-common/library/queue/databus"
  15. "go-common/library/time"
  16. "github.com/BurntSushi/toml"
  17. )
  18. var (
  19. // ConfPath config path.
  20. ConfPath string
  21. // Conf config.
  22. Conf *Config
  23. )
  24. // Config config.
  25. type Config struct {
  26. XLog *log.Config
  27. Tracer *trace.Config
  28. // HTTP
  29. HTTPClient *bm.ClientConfig
  30. DrawyooHTTPClient *bm.ClientConfig
  31. // databus
  32. Databus *Databus
  33. // rpc
  34. RPCClient2 *RPCClient2
  35. // bm
  36. BM *bm.ServerConfig
  37. // mysql
  38. MySQL *MySQL
  39. // redis
  40. Redis *Redis
  41. // mc
  42. Memcache *Memcache
  43. // Host
  44. Host *Host
  45. Weight *Weight
  46. Job *Job
  47. StatTypes map[string]int8
  48. Es *elastic.Config
  49. AccountClient *warden.ClientConfig
  50. }
  51. // Job job.
  52. type Job struct {
  53. Proc int
  54. SearchNum int
  55. SearchFlush time.Duration
  56. MessageMids []int64
  57. BatchNumber int
  58. }
  59. // Weight weight.
  60. type Weight struct {
  61. Like int
  62. Hate int
  63. }
  64. // Host represents host info.
  65. type Host struct {
  66. Activity string
  67. Message string
  68. DrawYoo string
  69. Search string
  70. BlackRoom string
  71. LiveVC string
  72. LiveAct string
  73. API string
  74. Bangumi string
  75. }
  76. // MySQL mysql.
  77. type MySQL struct {
  78. Reply *sql.Config
  79. }
  80. // Redis redis.
  81. type Redis struct {
  82. *redis.Config
  83. IndexExpire time.Duration
  84. ReportExpire time.Duration
  85. UserCntExpire time.Duration
  86. StatCacheExpire time.Duration
  87. UserActExpire time.Duration
  88. NotifyExpire time.Duration
  89. }
  90. // Memcache mc.
  91. type Memcache struct {
  92. *memcache.Config
  93. Expire time.Duration
  94. TopExpire time.Duration
  95. }
  96. // RPCClient2 rpc client.
  97. type RPCClient2 struct {
  98. Account *rpc.ClientConfig
  99. Archive *rpc.ClientConfig
  100. Article *rpc.ClientConfig
  101. Assist *rpc.ClientConfig
  102. }
  103. // Databus databus.
  104. type Databus struct {
  105. Event *databus.Config
  106. Stats *databus.Config
  107. Consumer *databus.Config
  108. Like *databus.Config
  109. }
  110. // Stats stats.
  111. type Stats struct {
  112. *databus.Config
  113. Type int8
  114. Field string
  115. }
  116. func init() {
  117. flag.StringVar(&ConfPath, "conf", "", "config path")
  118. }
  119. // Init init conf
  120. func Init() (err error) {
  121. if ConfPath == "" {
  122. return configCenter()
  123. }
  124. _, err = toml.DecodeFile(ConfPath, &Conf)
  125. return
  126. }
  127. func configCenter() (err error) {
  128. var (
  129. ok bool
  130. value string
  131. client *conf.Client
  132. )
  133. if client, err = conf.New(); err != nil {
  134. return
  135. }
  136. if value, ok = client.Toml2(); !ok {
  137. panic(err)
  138. }
  139. _, err = toml.Decode(value, &Conf)
  140. return
  141. }