conf.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/app/interface/main/tv/model"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. ecode "go-common/library/ecode/tip"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/middleware/auth"
  12. "go-common/library/net/http/blademaster/middleware/verify"
  13. "go-common/library/net/rpc"
  14. "go-common/library/net/rpc/warden"
  15. "go-common/library/net/trace"
  16. xtime "go-common/library/time"
  17. "github.com/BurntSushi/toml"
  18. )
  19. // Conf global variable.
  20. var (
  21. Conf = &Config{}
  22. confPath string
  23. client *conf.Client
  24. )
  25. // Config struct of conf.
  26. type Config struct {
  27. // zone configure
  28. Newzone map[string]*PageCfg
  29. // log
  30. Log *log.Config
  31. // tracer
  32. Tracer *trace.Config
  33. // http server config
  34. HTTPServer *bm.ServerConfig
  35. // auth
  36. Auth *auth.Config
  37. // verify
  38. Verify *verify.Config
  39. // mysql
  40. Mysql *sql.Config
  41. // memcache
  42. Memcache *Memcache
  43. // app
  44. TVApp *TVApp
  45. // homepage settings
  46. Homepage *PageConf
  47. // HTTPClient .
  48. HTTPClient *bm.ClientConfig
  49. PlayurlClient *bm.ClientConfig
  50. SearchClient *bm.ClientConfig
  51. // Redis
  52. Redis *Redis
  53. // Cfg common configuration
  54. Cfg *Cfg
  55. // Search Config
  56. Search *Search
  57. // RPC config
  58. ArcClient *warden.ClientConfig
  59. AccClient *warden.ClientConfig
  60. HisRPC *rpc.ClientConfig
  61. FavoriteRPC *rpc.ClientConfig
  62. TvVipClient *warden.ClientConfig
  63. // Ip Whitelist
  64. IP *IP
  65. // ecode
  66. Ecode *ecode.Config
  67. // api url
  68. Host *Host
  69. Region *Region
  70. Style *Style
  71. Wild *Wild
  72. }
  73. // IPWhite .
  74. type IPWhite struct {
  75. TvVip []string
  76. }
  77. // IP .
  78. type IP struct {
  79. White *IPWhite
  80. }
  81. // Style label .
  82. type Style struct {
  83. LabelSpan xtime.Duration
  84. }
  85. // Region .
  86. type Region struct {
  87. StopSpan xtime.Duration // get region time span
  88. }
  89. // IndexLabel def.
  90. type IndexLabel struct {
  91. Fre xtime.Duration
  92. PGCOrder []string // pgc order
  93. UGCOrder []string // ugc order
  94. YearV map[string]*YearVDur // year value pair
  95. YearParam []string // year params = pub_date, year
  96. }
  97. // YearVDur def
  98. type YearVDur struct {
  99. Dur string `json:"dur"`
  100. }
  101. // IsYear distinguishes whether the param is year type param
  102. func (u *IndexLabel) IsYear(param string) bool {
  103. for _, v := range u.YearParam {
  104. if v == param {
  105. return true
  106. }
  107. }
  108. return false
  109. }
  110. // Host api urls
  111. type Host struct {
  112. Data string // data.bilibili.co
  113. APIIndex string // homepage pgc data source
  114. APIZone string // zonepage pgc data source
  115. APIFollow string // pgc follow
  116. APIMedia string // pgc media detail
  117. APIMediaV2 string // pgc media detail v2
  118. APIRecom string // pgc recom
  119. APINewindex string // pgc index_show
  120. UgcPlayURL string // ugc play url
  121. AIUgcType string // ai ugc type data
  122. APICo string
  123. FavAdd string // favorite add url
  124. FavDel string // favorite del url
  125. ReqURL string // version update request url
  126. ESHost string // manager url
  127. }
  128. // Wild .
  129. type Wild struct {
  130. WildSearch *WildSearch
  131. }
  132. // WildSearch wild search .
  133. type WildSearch struct {
  134. UserNum int
  135. UserVideoLimit int
  136. BiliUserNum int
  137. BiliUserVl int
  138. SeasonNum int
  139. MovieNum int
  140. SeasonMore int
  141. MovieMore int
  142. }
  143. // Cfg def.
  144. type Cfg struct {
  145. ZonePs int // Zone index page size
  146. AuthMsg *AuthMsg // auth error message config
  147. ZonesInfo *ZonesInfo // all the zones info
  148. Dangbei *Dangbei // dangbei configuration
  149. PageReload xtime.Duration // all page reload duration
  150. IndexShowReload xtime.Duration // index show reload duration
  151. EsIntervReload xtime.Duration // es intervention reload duration
  152. DefaultSplash string // default splash url
  153. FavPs int // favorite cfg
  154. PGCFilterBuild int // the build number, under which we export only pgc modules and data
  155. VipQns []string // the qualities dedicated for vips
  156. HisCfg *HisCfg // history related cfg
  157. EsIdx *EsIdx // elastic search index page cfg
  158. IndexLabel *IndexLabel // index label cfg
  159. EmptyArc *EmptyArc // chan size
  160. VipMark *VipMark // vip mark
  161. SnVipCorner *model.SnVipCorner // season vip corner mark cfg
  162. AuditSign *AuditSign
  163. }
  164. // AuditSign cfg is used to check license owner requests
  165. type AuditSign struct {
  166. Key string
  167. Secret string
  168. }
  169. // TvVip def.
  170. type TvVip struct {
  171. Build int64
  172. Msg string
  173. }
  174. // VipMark def.
  175. type VipMark struct {
  176. V1HideChargeable bool // whether we hide chargeable episode in pgc view V1
  177. EpFree int // ep's pay status which means free
  178. EP *model.CornerMark
  179. LoadepMsg *TvVip // tv vip cfg
  180. }
  181. // EmptyArc def.
  182. type EmptyArc struct {
  183. ChanSize int64
  184. UnshelvePS int
  185. }
  186. // EsIdx def.
  187. type EsIdx struct {
  188. UgcIdx, PgcIdx *EsCfg
  189. }
  190. // EsCfg def.
  191. type EsCfg struct {
  192. Business string
  193. Index string
  194. }
  195. // HisCfg def.
  196. type HisCfg struct {
  197. Businesses []string
  198. Pagesize int
  199. }
  200. // Dangbei cfg def.
  201. type Dangbei struct {
  202. Pagesize int64 // dangbei api page size
  203. MangoPS int // mango page size
  204. Expire xtime.Duration // dangbei page ID expiration
  205. }
  206. // AuthMsg configures the auth error messages
  207. type AuthMsg struct {
  208. PGCOffline string // offline pgc
  209. CMSInvalid string // cms not valid
  210. LicenseReject string // license owner rejected
  211. }
  212. // App config
  213. type App struct {
  214. *bm.App
  215. }
  216. func configCenter() (err error) {
  217. if client, err = conf.New(); err != nil {
  218. panic(err)
  219. }
  220. if err = load(); err != nil {
  221. return
  222. }
  223. go func() {
  224. for range client.Event() {
  225. log.Info("config reload")
  226. if load() != nil {
  227. log.Error("config reload error (%v)", err)
  228. }
  229. }
  230. }()
  231. return
  232. }
  233. func load() (err error) {
  234. var (
  235. s string
  236. ok bool
  237. tmpConf *Config
  238. )
  239. if s, ok = client.Toml2(); !ok {
  240. return errors.New("load config center error")
  241. }
  242. if _, err = toml.Decode(s, &tmpConf); err != nil {
  243. return errors.New("could not decode config")
  244. }
  245. *Conf = *tmpConf
  246. return
  247. }
  248. func init() {
  249. flag.StringVar(&confPath, "conf", "", "config path")
  250. }
  251. // Init init conf.
  252. func Init() (err error) {
  253. if confPath == "" {
  254. return configCenter()
  255. }
  256. _, err = toml.DecodeFile(confPath, &Conf)
  257. return
  258. }