conf.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/sql"
  9. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/auth"
  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/time"
  19. "go-common/library/database/hbase.v2"
  20. "github.com/BurntSushi/toml"
  21. )
  22. // global var
  23. var (
  24. confPath string
  25. client *conf.Client
  26. // Conf config
  27. Conf = &Config{}
  28. )
  29. // Config config set
  30. type Config struct {
  31. // elk
  32. Log *log.Config
  33. // App
  34. App *blademaster.App
  35. // tracer
  36. Tracer *trace.Config
  37. // Auth
  38. Auth *auth.Config
  39. // Verify
  40. Verify *verify.Config
  41. // Supervisor
  42. Supervisor *supervisor.Config
  43. // BM
  44. BM *httpServers
  45. // HTTPServer
  46. HTTPServer *blademaster.ServerConfig
  47. // Ecode
  48. Ecode *ecode.Config
  49. // ArchiveRPC
  50. AccountRPC *rpc.ClientConfig
  51. ArticleRPC *rpc.ClientConfig
  52. AssistRPC *rpc.ClientConfig
  53. TagRPC *rpc.ClientConfig
  54. FavoriteRPC *rpc.ClientConfig
  55. FilterRPC *rpc.ClientConfig
  56. ThumbupRPC *rpc.ClientConfig
  57. RelationRPC *rpc.ClientConfig
  58. MemberRPC *rpc.ClientConfig
  59. // grpc
  60. AccClient *warden.ClientConfig
  61. ArcClient *warden.ClientConfig
  62. CoinClient *warden.ClientConfig
  63. UpClient *warden.ClientConfig
  64. // Mysql
  65. Mysql *sql.Config
  66. // Redis
  67. Redis *redisConf
  68. // Mc
  69. Memcache *memConf
  70. // Rule
  71. Rule *rule
  72. // HTTP client
  73. HTTPClient *httpClient
  74. // Host
  75. Host *host
  76. // HBase hbase config
  77. HBase *Hbase
  78. }
  79. type redisConf struct {
  80. *redis.Config
  81. ClExpire time.Duration
  82. UpArtExpire time.Duration
  83. UpArcExpire time.Duration
  84. }
  85. type memConf struct {
  86. *memcache.Config
  87. SettingExpire time.Duration
  88. NoticeExpire time.Duration
  89. TopArcExpire time.Duration
  90. MpExpire time.Duration
  91. ThemeExpire time.Duration
  92. TopDyExpire time.Duration
  93. }
  94. type rule struct {
  95. MaxChNameLen int
  96. MaxChIntroLen int
  97. MaxChLimit int
  98. MaxChArcLimit int
  99. MaxChArcAddLimit int
  100. MaxChArcsPs int
  101. MaxRiderPs int
  102. MaxArticlePs int
  103. ChIndexCnt int
  104. MaxNoticeLen int
  105. MaxTopReasonLen int
  106. MaxMpReasonLen int
  107. MaxMpLimit int
  108. // RealNameOn
  109. RealNameOn bool
  110. // No limit notice mids
  111. NoNoticeMids []int64
  112. // default top photo
  113. TopPhoto string
  114. // dynamic list switch
  115. Merge bool
  116. ActFold bool
  117. // block mids
  118. BlockMids []int64
  119. //BlackFre space blacklist frequency
  120. BlackFre time.Duration
  121. }
  122. type host struct {
  123. Bangumi string
  124. API string
  125. Mall string
  126. APIVc string
  127. APILive string
  128. Acc string
  129. Game string
  130. AppGame string
  131. Search string
  132. Elec string
  133. Space string
  134. }
  135. type httpClient struct {
  136. Read *blademaster.ClientConfig
  137. Write *blademaster.ClientConfig
  138. Game *blademaster.ClientConfig
  139. }
  140. type httpServers struct {
  141. Outer *blademaster.ServerConfig
  142. }
  143. // Hbase .
  144. type Hbase struct {
  145. *hbase.Config
  146. ReadTimeout time.Duration
  147. }
  148. func init() {
  149. flag.StringVar(&confPath, "conf", "", "default config path")
  150. }
  151. // Init init conf
  152. func Init() error {
  153. if confPath != "" {
  154. return local()
  155. }
  156. return remote()
  157. }
  158. func local() (err error) {
  159. _, err = toml.DecodeFile(confPath, &Conf)
  160. return
  161. }
  162. func remote() (err error) {
  163. if client, err = conf.New(); err != nil {
  164. return
  165. }
  166. if err = load(); err != nil {
  167. return
  168. }
  169. go func() {
  170. for range client.Event() {
  171. log.Info("config reload")
  172. if load() != nil {
  173. log.Error("config reload error (%v)", err)
  174. }
  175. }
  176. }()
  177. return
  178. }
  179. func load() (err error) {
  180. var (
  181. s string
  182. ok bool
  183. tmpConf *Config
  184. )
  185. if s, ok = client.Toml2(); !ok {
  186. return errors.New("load config center error")
  187. }
  188. if _, err = toml.Decode(s, &tmpConf); err != nil {
  189. return errors.New("could not decode config")
  190. }
  191. *Conf = *tmpConf
  192. return
  193. }