conf.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. xlog "go-common/library/log"
  11. "go-common/library/log/infoc"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/http/blademaster/middleware/antispam"
  14. "go-common/library/net/rpc"
  15. "go-common/library/net/rpc/warden"
  16. "go-common/library/net/trace"
  17. "go-common/library/time"
  18. "github.com/BurntSushi/toml"
  19. )
  20. var (
  21. // ConfPath str
  22. ConfPath string
  23. // Conf cfg
  24. Conf = &Config{}
  25. client *conf.Client
  26. )
  27. // Config struct
  28. type Config struct {
  29. AppEditorInfoc *infoc.Config
  30. // base
  31. Bfs *Bfs
  32. Limit *Limit
  33. // db
  34. DB *DB
  35. // tick
  36. Tick time.Duration
  37. // app
  38. App *bm.App
  39. // host
  40. Host *Host
  41. // http
  42. HTTPClient *HTTPClient
  43. // xlog
  44. Xlog *xlog.Config
  45. // tracer
  46. Tracer *trace.Config
  47. // ecode
  48. Ecode *ecode.Config
  49. // rpc client
  50. RelationRPC *rpc.ClientConfig
  51. SubRPC *rpc.ClientConfig
  52. // mc
  53. Memcache *Memcache
  54. // redis
  55. Redis *Redis
  56. UpCoverAnti *antispam.Config
  57. Game *Game
  58. BM *bm.ServerConfig
  59. // geetest
  60. Geetest *Geetest
  61. MaxAllVsCnt int
  62. MaxAddVsCnt int
  63. UgcPayAllowEditDays int
  64. AccClient *warden.ClientConfig
  65. UpClient *warden.ClientConfig
  66. }
  67. // Geetest geetest id & key
  68. type Geetest struct {
  69. CaptchaID string
  70. MCaptchaID string
  71. PrivateKEY string
  72. MPrivateKEY string
  73. }
  74. // Game str Conf
  75. type Game struct {
  76. OpenHost string
  77. App *bm.App
  78. }
  79. // DB conf.
  80. type DB struct {
  81. // archive db
  82. Manager *sql.Config
  83. }
  84. // Limit config
  85. type Limit struct {
  86. AddBasicExp time.Duration
  87. }
  88. // Bfs bfs config
  89. type Bfs struct {
  90. Timeout time.Duration
  91. MaxFileSize int
  92. }
  93. // Host conf
  94. type Host struct {
  95. Account string
  96. Archive string
  97. APICo string
  98. WWW string
  99. Member string
  100. UpMng string
  101. Tag string
  102. Elec string
  103. Geetest string
  104. Dynamic string
  105. MainSearch string
  106. Chaodian string //超电
  107. }
  108. // HTTPClient str
  109. type HTTPClient struct {
  110. Read *bm.ClientConfig
  111. Write *bm.ClientConfig
  112. UpMng *bm.ClientConfig
  113. FastRead *bm.ClientConfig
  114. Chaodian *bm.ClientConfig
  115. }
  116. // Memcache str
  117. type Memcache struct {
  118. Account *struct {
  119. *memcache.Config
  120. SubmitExpire time.Duration
  121. }
  122. }
  123. // Redis str
  124. type Redis struct {
  125. Videoup *struct {
  126. *redis.Config
  127. Expire time.Duration
  128. }
  129. }
  130. func init() {
  131. flag.StringVar(&ConfPath, "conf", "", "default config path")
  132. }
  133. // Init init
  134. func Init() (err error) {
  135. if ConfPath != "" {
  136. return local()
  137. }
  138. return remote()
  139. }
  140. func local() (err error) {
  141. _, err = toml.DecodeFile(ConfPath, &Conf)
  142. return
  143. }
  144. func remote() (err error) {
  145. if client, err = conf.New(); err != nil {
  146. return
  147. }
  148. if err = load(); err != nil {
  149. return
  150. }
  151. go func() {
  152. for range client.Event() {
  153. xlog.Info("config reload")
  154. if load() != nil {
  155. xlog.Error("config reload error (%v)", err)
  156. }
  157. }
  158. }()
  159. return
  160. }
  161. func load() (err error) {
  162. var (
  163. s string
  164. ok bool
  165. tmpConf *Config
  166. )
  167. if s, ok = client.Toml2(); !ok {
  168. return errors.New("load config center error")
  169. }
  170. if _, err = toml.Decode(s, &tmpConf); err != nil {
  171. return errors.New("could not decode config")
  172. }
  173. *Conf = *tmpConf
  174. return
  175. }