conf.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/rpc"
  12. "go-common/library/net/rpc/warden"
  13. "go-common/library/net/trace"
  14. "go-common/library/queue/databus"
  15. xtime "go-common/library/time"
  16. "github.com/BurntSushi/toml"
  17. )
  18. // Conf global variable.
  19. var (
  20. Conf = &Config{}
  21. client *conf.Client
  22. confPath string
  23. )
  24. // Config struct of conf.
  25. type Config struct {
  26. // base
  27. // log
  28. Log *log.Config
  29. // Databus cfg
  30. ContentSub *databus.Config
  31. ArchiveNotifySub *databus.Config
  32. UgcSub *databus.Config
  33. // tracer
  34. Tracer *trace.Config
  35. // http
  36. HTTPServer *bm.ServerConfig
  37. // db
  38. Mysql *sql.Config
  39. // sync params
  40. Sync *Sync
  41. // memcache
  42. Memcache *Memcache
  43. // redis
  44. Redis *Redis
  45. // playControl related config
  46. PlayControl *PlayControl
  47. // HTTPClient .
  48. HTTPClient *bm.ClientConfig
  49. // Search Cfg
  50. Search *Search
  51. // UgcSync cfg
  52. UgcSync *UgcSync
  53. // grpc
  54. ArcClient *warden.ClientConfig
  55. ArchiveRPC *rpc.ClientConfig
  56. AccClient *warden.ClientConfig
  57. Cfg *Cfg
  58. Report *Report
  59. DpClient *bm.ClientConfig
  60. Style *Style
  61. }
  62. // Style .
  63. type Style struct {
  64. LabelSpan xtime.Duration
  65. StyleSpan xtime.Duration
  66. }
  67. // Report data .
  68. type Report struct {
  69. ReportURI string
  70. UpDataURI string
  71. TimeDelay string
  72. SendDataDelay xtime.Duration
  73. Env string
  74. RoutineCount int
  75. ReadSize int
  76. Expire xtime.Duration
  77. SeTimeSpan xtime.Duration
  78. CronAc string
  79. CronAd string
  80. CronPd string
  81. CronVe string
  82. }
  83. // Cfg contains various of configuration
  84. type Cfg struct {
  85. TitleFilter []string
  86. LessStrategy int
  87. PgcTypes []string // pgc types name, need to filter these ugc archives
  88. PGCZonesID []int // all the zones' ID that need to be loaded
  89. UgcZones map[string]*UgcType
  90. SyncRetry *SyncRetry // sync retry
  91. Merak *Merak
  92. }
  93. // Merak cfg
  94. type Merak struct {
  95. Host string
  96. Key string
  97. Secret string
  98. Names []string
  99. Template string
  100. Title string
  101. Cron string
  102. Onlyfree bool // if true, we consider free+audited episodes, otherwise we consider only audited episodes
  103. }
  104. // SyncRetry def.
  105. type SyncRetry struct {
  106. MaxRetry int // max retry times for pgc already-passed sn & ep
  107. RetryFre xtime.Duration
  108. }
  109. // UgcType def.
  110. type UgcType struct {
  111. TID int32
  112. Name string
  113. }
  114. // Search represents the config for the search suggestion module
  115. type Search struct {
  116. UgcSwitch string // the Ugc search suggest Switch
  117. SugPath string // the tvsug file local path
  118. Md5Path string // the tvsug md5 file local path
  119. FTP *FTP // the ftp info
  120. PgcContPath string // the pgc content file local path
  121. PgcContMd5Path string // the pgc content md5 file local path
  122. UgcContPath string // the ugc content file local path
  123. UgcContMd5Path string // the ugc content md5 file local path
  124. Cfg *SearchCfg
  125. }
  126. //SearchCfg synchronize files time
  127. type SearchCfg struct {
  128. UploadFre xtime.Duration
  129. }
  130. // FTP represents the ftp login info
  131. type FTP struct {
  132. Pass string
  133. User string
  134. Host string
  135. URL string
  136. Timeout xtime.Duration // timeout in seconds
  137. UseEPSV bool
  138. RemoteFName string // file name in remote ftp server
  139. RemoteMd5 string // md5 file name in remote ftp server
  140. RemotePgcCont string // pgc file name in remote ftp server
  141. RemotePgcURL string // RemotePgcURL remote search pgc url dir
  142. RemotePgcContMd5 string // pgc md5 file name in remote ftp server
  143. RemoteUgcCont string // ugc file name in remote ftp server
  144. RemoteUgcURL string // RemotePgcCont remote search ugc url dir
  145. RemoteUgcContMd5 string // ugc md5 file name in remote ftp server
  146. }
  147. // Redis redis
  148. type Redis struct {
  149. *redis.Config
  150. Expire xtime.Duration
  151. CronPGC string
  152. CronUGC string
  153. }
  154. // PlayControl is the configuration for the play control interface, related to MC
  155. type PlayControl struct {
  156. ProducerCron string
  157. PieceSize int
  158. }
  159. // Memcache config
  160. type Memcache struct {
  161. *memcache.Config
  162. Expire xtime.Duration
  163. ExpireMedia xtime.Duration
  164. }
  165. func local() (err error) {
  166. _, err = toml.DecodeFile(confPath, &Conf)
  167. return
  168. }
  169. func remote() (err error) {
  170. if client, err = conf.New(); err != nil {
  171. return
  172. }
  173. if err = load(); err != nil {
  174. return
  175. }
  176. go func() {
  177. for range client.Event() {
  178. log.Info("config reload")
  179. if load() != nil {
  180. log.Error("config reload error (%v)", err)
  181. }
  182. }
  183. }()
  184. return
  185. }
  186. func load() (err error) {
  187. var (
  188. s string
  189. ok bool
  190. tmpConf *Config
  191. )
  192. if s, ok = client.Toml2(); !ok {
  193. return errors.New("load config center error")
  194. }
  195. if _, err = toml.Decode(s, &tmpConf); err != nil {
  196. return errors.New("could not decode config")
  197. }
  198. *Conf = *tmpConf
  199. return
  200. }
  201. func init() {
  202. flag.StringVar(&confPath, "conf", "", "default config path")
  203. }
  204. // Init int config
  205. func Init() error {
  206. if confPath != "" {
  207. return local()
  208. }
  209. return remote()
  210. }