conf.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. xtime "time"
  6. "go-common/app/interface/main/player/model"
  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/log/infoc"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/http/blademaster/middleware/auth"
  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. "github.com/BurntSushi/toml"
  20. )
  21. // global var
  22. var (
  23. confPath string
  24. client *conf.Client
  25. Conf = &Config{}
  26. )
  27. // Config is service conf.
  28. type Config struct {
  29. // 广播
  30. Broadcast Broadcast
  31. // policy
  32. Policy *model.Policy
  33. // policy items
  34. Pitem []*model.Pitem
  35. // 拜年祭
  36. Matsuri Matsuri
  37. // XLog
  38. XLog *log.Config
  39. // ecode
  40. Ecode *ecode.Config
  41. // host
  42. Host *Host
  43. // tracer
  44. Tracer *trace.Config
  45. // auth
  46. Auth *auth.Config
  47. // verify
  48. Verify *verify.Config
  49. // bm
  50. BM *HTTPServers
  51. // mysql
  52. MySQL *MySQL
  53. // rpc
  54. ArchiveRPC *rpc.ClientConfig
  55. AccountRPC *rpc.ClientConfig
  56. HistoryRPC *rpc.ClientConfig
  57. AssistRPC *rpc.ClientConfig
  58. ResourceRPC *rpc.ClientConfig
  59. Dm2RPC *rpc.ClientConfig
  60. LocRPC *rpc.ClientConfig
  61. TagRPC *rpc.ClientConfig
  62. // HTTPClient
  63. HTTPClient *bm.ClientConfig
  64. // Rule
  65. Rule *Rule
  66. Tick *Tick
  67. // Infoc2
  68. Infoc2 *infoc.Config
  69. // PlayURLToken
  70. PlayURLToken *PlayURLToken
  71. // grpc client
  72. AccClient *warden.ClientConfig
  73. ArcClient *warden.ClientConfig
  74. UGCPayClient *warden.ClientConfig
  75. // icon
  76. Icon *icon
  77. // bnj2019
  78. Bnj2019 *bnj2019
  79. }
  80. // Tick tick time.
  81. type Tick struct {
  82. // tick time
  83. CarouselTick time.Duration
  84. ParamTick time.Duration
  85. IconTick time.Duration
  86. }
  87. // Rule rules
  88. type Rule struct {
  89. // timeout
  90. VsTimeout time.Duration
  91. NoAssistMid int64
  92. VipQn []int
  93. LoginQn int
  94. MaxFreeQn int
  95. AutoQn int
  96. PlayurlGray int64
  97. }
  98. // Host is host info
  99. type Host struct {
  100. APICo string
  101. AccCo string
  102. PlayurlCo string
  103. H5Playurl string
  104. HighPlayurl string
  105. }
  106. // MySQL mysql.
  107. type MySQL struct {
  108. Show *sql.Config
  109. }
  110. // Broadcast breadcast.
  111. type Broadcast struct {
  112. TCPAddr string
  113. WsAddr string
  114. WssAddr string
  115. Begin string
  116. End string
  117. }
  118. // Matsuri matsuri.
  119. type Matsuri struct {
  120. PastID int64
  121. MatID int64
  122. MatTime string
  123. Tick time.Duration
  124. }
  125. // PlayURLToken playurl auth token.
  126. type PlayURLToken struct {
  127. Secret string
  128. PlayerToken string
  129. }
  130. // HTTPServers bm servers config.
  131. type HTTPServers struct {
  132. Outer *bm.ServerConfig
  133. }
  134. type bnj2019 struct {
  135. BnjMainAid int64
  136. BnjListAids []int64
  137. BnjTick time.Duration
  138. }
  139. type icon struct {
  140. Start xtime.Time
  141. End xtime.Time
  142. URL1 string
  143. Hash1 string
  144. URL2 string
  145. Hash2 string
  146. }
  147. func init() {
  148. flag.StringVar(&confPath, "conf", "", "config path")
  149. }
  150. // Init init conf
  151. func Init() error {
  152. if confPath != "" {
  153. return local()
  154. }
  155. return remote()
  156. }
  157. func local() (err error) {
  158. _, err = toml.DecodeFile(confPath, &Conf)
  159. return
  160. }
  161. func remote() (err error) {
  162. if client, err = conf.New(); err != nil {
  163. return
  164. }
  165. if err = load(); err != nil {
  166. return
  167. }
  168. go func() {
  169. for range client.Event() {
  170. log.Info("config reload")
  171. if load() != nil {
  172. log.Error("config reload error (%v)", err)
  173. }
  174. }
  175. }()
  176. return
  177. }
  178. func load() (err error) {
  179. var (
  180. s string
  181. ok bool
  182. tmpConf *Config
  183. )
  184. if s, ok = client.Toml2(); !ok {
  185. return errors.New("load config center error")
  186. }
  187. if _, err = toml.Decode(s, &tmpConf); err != nil {
  188. return errors.New("could not decode config")
  189. }
  190. *Conf = *tmpConf
  191. return
  192. }