conf.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package conf
  2. import (
  3. "flag"
  4. "go-common/library/cache/memcache"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. ecode "go-common/library/ecode/tip"
  9. "go-common/library/log"
  10. "go-common/library/log/infoc"
  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/supervisor"
  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. // Conf Config
  25. Conf *Config
  26. )
  27. // Config is favorte conf
  28. type Config struct {
  29. // base
  30. // log
  31. Log *log.Config
  32. App *bm.App
  33. // favorite config
  34. Fav *Fav
  35. Platform *Platform
  36. // BM blademaster
  37. BM *bm.ServerConfig
  38. // rpc server2
  39. RPCServer *rpc.ServerConfig
  40. // db
  41. MySQL *MySQL
  42. // redis
  43. Redis *Redis
  44. // memcache
  45. Memcache *Memcache
  46. // databus
  47. JobDatabus *databus.Config
  48. // verify
  49. Verify *verify.Config
  50. // rpc client
  51. RPCClient2 *RPC
  52. // tracer
  53. Tracer *trace.Config
  54. // http client
  55. HTTPClient *bm.ClientConfig
  56. // ecode
  57. Ecode *ecode.Config
  58. // TopicClient
  59. Topic *Topic
  60. // Antispam
  61. Antispam *antispam.Config
  62. // Supervisior
  63. Supervisor *supervisor.Config
  64. // collector
  65. Infoc2 *infoc.Config
  66. //grpc warden
  67. WardenServer *warden.ServerConfig
  68. }
  69. // RPC contain all rpc conf
  70. type RPC struct {
  71. Account *warden.ClientConfig
  72. Archive *rpc.ClientConfig
  73. Filter *rpc.ClientConfig
  74. Rank *rpc.ClientConfig
  75. }
  76. // Topic Topic
  77. type Topic struct {
  78. TopicURL string
  79. HTTPClient *bm.ClientConfig
  80. }
  81. // Fav config
  82. type Fav struct {
  83. // the max of the num of favorite folders
  84. MaxFolders int
  85. MaxPagesize int
  86. MaxBatchSize int
  87. MaxDataSize int
  88. MaxParallelSize int
  89. MaxRecentSize int
  90. MaxNameLen int
  91. MaxDescLen int
  92. // the num of operation
  93. MaxOperationNum int
  94. // the num of default favorite
  95. DefaultFolderLimit int
  96. NormalFolderLimit int
  97. // ApiHost api.bilibili.co .
  98. APIHost string
  99. // cache expire
  100. Expire time.Duration
  101. // cdtime cool down time
  102. CleanCDTime time.Duration
  103. // real-name switch
  104. RealNameOn bool
  105. }
  106. // Platform config
  107. type Platform struct {
  108. MaxFolders int
  109. MaxNameLen int
  110. MaxDescLen int
  111. }
  112. // MySQL is mysql conf
  113. type MySQL struct {
  114. // favorite db
  115. Fav *sql.Config
  116. Read *sql.Config
  117. Push *sql.Config
  118. }
  119. // Redis redis conf
  120. type Redis struct {
  121. *redis.Config
  122. Expire time.Duration
  123. CoverExpire time.Duration
  124. }
  125. // Memcache memcache conf
  126. type Memcache struct {
  127. *memcache.Config
  128. Expire time.Duration
  129. }
  130. func init() {
  131. flag.StringVar(&confPath, "conf", "", "config path")
  132. }
  133. // Init init conf
  134. func Init() (err error) {
  135. if confPath == "" {
  136. return configCenter()
  137. }
  138. _, err = toml.DecodeFile(confPath, &Conf)
  139. return
  140. }
  141. func configCenter() (err error) {
  142. var (
  143. ok bool
  144. value string
  145. client *conf.Client
  146. )
  147. if client, err = conf.New(); err != nil {
  148. return
  149. }
  150. if value, ok = client.Toml2(); !ok {
  151. panic(err)
  152. }
  153. _, err = toml.Decode(value, &Conf)
  154. return
  155. }