conf.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/verify"
  13. "go-common/library/net/trace"
  14. "go-common/library/queue/databus"
  15. xtime "go-common/library/time"
  16. "go-common/library/net/rpc/warden"
  17. "github.com/BurntSushi/toml"
  18. )
  19. var (
  20. confPath string
  21. client *conf.Client
  22. // Conf config
  23. Conf = &Config{}
  24. )
  25. // Config .
  26. type Config struct {
  27. Log *log.Config
  28. BM *HTTPGeneral
  29. Verify *verify.Config
  30. Tracer *trace.Config
  31. Redis *redis.Config
  32. BfRedis *redis.Config
  33. Memcache *memcache.Config
  34. MySQL *sql.Config
  35. MySQLCms *sql.Config
  36. MySQLOffline *sql.Config
  37. Ecode *ecode.Config
  38. Scheduler *Scheduler
  39. GRPCClient map[string]*GRPCConf
  40. //berserker
  41. Berserker *Berserker
  42. TagMap *TagMap
  43. Databus map[string]*databus.Config
  44. Mail *Mail
  45. URLs map[string]string
  46. Download *Download
  47. FTP *FTP
  48. VST *Vst
  49. Path map[string]string
  50. SubBvcControl *Sub
  51. }
  52. //Sub ...
  53. type Sub struct {
  54. Control int8
  55. }
  56. //Vst ...
  57. type Vst struct {
  58. TmpStatus int64
  59. }
  60. // FTP FTP.
  61. type FTP struct {
  62. Addr string
  63. User string
  64. Password string
  65. RemotePath map[string]string
  66. Timeout xtime.Duration
  67. LocalPath map[string]string
  68. }
  69. //GRPCConf .
  70. type GRPCConf struct {
  71. WardenConf *warden.ClientConfig
  72. Addr string
  73. }
  74. // Download .
  75. type Download struct {
  76. File string
  77. }
  78. //Mail ...
  79. type Mail struct {
  80. Host string
  81. Port int
  82. From string
  83. Password string
  84. To []string
  85. }
  86. //TagMap ...
  87. type TagMap struct {
  88. TagTidMap map[string]string
  89. TagSubTidMap map[string]string
  90. }
  91. // HTTPGeneral conf
  92. type HTTPGeneral struct {
  93. Server *bm.ServerConfig
  94. Client *bm.ClientConfig
  95. }
  96. // Berserker conf
  97. type Berserker struct {
  98. Key *BerSerkerKeyList
  99. API *BerserkerAPI
  100. }
  101. // BerserkerAPI conf
  102. type BerserkerAPI struct {
  103. Rankdaily string
  104. Userdmg string
  105. Upuserdmg string
  106. Operaonce string
  107. Userbasic string
  108. Upmid string
  109. VideoView string
  110. UserProfile string
  111. UserProfileBuvid string
  112. }
  113. // BerSerkerKeyList conf
  114. type BerSerkerKeyList struct {
  115. YYC *BerSerkerKey
  116. HSC *BerSerkerKey
  117. LZQ *BerSerkerKey
  118. LJ *BerSerkerKey
  119. DW *BerSerkerKey
  120. HM *BerSerkerKey
  121. }
  122. // BerSerkerKey conf
  123. type BerSerkerKey struct {
  124. Appkey string
  125. Secret string
  126. }
  127. //Scheduler .
  128. type Scheduler struct {
  129. CheckVideo2ES string
  130. SyncUserDmg string
  131. Test string
  132. SyncUpUserDmg string
  133. CheckVideo string
  134. CheckVideoSt string
  135. CheckVideoStHv string
  136. CheckVideoTag string
  137. CheckTag string
  138. SyncVideoOper string
  139. DeliveryNewVideoToCms string
  140. SyncUsrSta string
  141. SyncSearch string
  142. VideoViewHistory string
  143. SysMsgTask string
  144. UserProfileBbq string
  145. TransToReview string
  146. TransToCheckBack string
  147. }
  148. // Databus .
  149. type Databus struct {
  150. Video *databus.Config
  151. VideoRep *databus.Config
  152. BvcSub *databus.Config
  153. }
  154. func init() {
  155. flag.StringVar(&confPath, "conf", "", "default config path")
  156. }
  157. // Init init conf
  158. func Init() error {
  159. if confPath != "" {
  160. return local()
  161. }
  162. return remote()
  163. }
  164. func local() (err error) {
  165. _, err = toml.DecodeFile(confPath, &Conf)
  166. return
  167. }
  168. func remote() (err error) {
  169. if client, err = conf.New(); err != nil {
  170. return
  171. }
  172. if err = load(); err != nil {
  173. return
  174. }
  175. go func() {
  176. for range client.Event() {
  177. log.Info("config reload")
  178. if load() != nil {
  179. log.Error("config reload error (%v)", err)
  180. }
  181. }
  182. }()
  183. return
  184. }
  185. func load() (err error) {
  186. var (
  187. s string
  188. ok bool
  189. tmpConf *Config
  190. )
  191. if s, ok = client.Toml2(); !ok {
  192. return errors.New("load config center error")
  193. }
  194. if _, err = toml.Decode(s, &tmpConf); err != nil {
  195. return errors.New("could not decode config")
  196. }
  197. *Conf = *tmpConf
  198. return
  199. }