conf.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package conf
  2. import (
  3. "flag"
  4. "go-common/library/cache/redis"
  5. "go-common/library/conf/paladin"
  6. "go-common/library/database/sql"
  7. ecode "go-common/library/ecode/tip"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/verify"
  11. "go-common/library/net/rpc/warden"
  12. "go-common/library/net/trace"
  13. "go-common/library/queue/databus"
  14. "github.com/BurntSushi/toml"
  15. )
  16. var (
  17. localConf string
  18. confName string
  19. // Conf config
  20. Conf = &Config{}
  21. // ArchiveRules 稿件导入规则
  22. ArchiveRules = &Rules{}
  23. )
  24. // Config .
  25. type Config struct {
  26. Log *log.Config
  27. BM *HTTPGeneral
  28. Verify *verify.Config
  29. Tracer *trace.Config
  30. Redis *redis.Config
  31. MySQL *sql.Config
  32. CMSMySQL *sql.Config
  33. Ecode *ecode.Config
  34. Berserker *Berserker
  35. GRPCClient map[string]*GRPCConf
  36. URLs map[string]string
  37. Databus map[string]*databus.Config
  38. BPSCode map[string]map[string]int64
  39. Upload *Upload
  40. }
  41. //Upload .
  42. type Upload struct {
  43. File *UploadFile
  44. Endpoint *UploadEndPoint
  45. Auth *UploadAuth
  46. }
  47. //UploadFile .
  48. type UploadFile struct {
  49. Prefix string
  50. Line string
  51. }
  52. //UploadEndPoint .
  53. type UploadEndPoint struct {
  54. Main string
  55. BackUp string
  56. }
  57. //UploadAuth .
  58. type UploadAuth struct {
  59. AK string
  60. SK string
  61. }
  62. //GRPCConf .
  63. type GRPCConf struct {
  64. WardenConf *warden.ClientConfig
  65. Addr string
  66. }
  67. //HTTPGeneral ...
  68. type HTTPGeneral struct {
  69. Server *bm.ServerConfig
  70. Client *bm.ClientConfig
  71. }
  72. // Berserker conf
  73. type Berserker struct {
  74. Key *BerSerkerKeyList
  75. API *BerserkerAPI
  76. }
  77. // BerserkerAPI conf
  78. type BerserkerAPI struct {
  79. Rankdaily string
  80. Userdmg string
  81. Operaonce string
  82. }
  83. // BerSerkerKeyList conf
  84. type BerSerkerKeyList struct {
  85. YYC *BerSerkerKey
  86. HSC *BerSerkerKey
  87. LZQ *BerSerkerKey
  88. }
  89. // BerSerkerKey conf
  90. type BerSerkerKey struct {
  91. Appkey string
  92. Secret string
  93. }
  94. // Set .
  95. func (c *Config) Set(text string) error {
  96. if _, err := toml.Decode(text, c); err != nil {
  97. panic(err)
  98. }
  99. return nil
  100. }
  101. // Set .
  102. func (r *Rules) Set(text string) error {
  103. if _, err := toml.Decode(text, r); err != nil {
  104. panic(err)
  105. }
  106. return nil
  107. }
  108. func init() {
  109. //线下使用
  110. flag.StringVar(&localConf, "localconf", "", "default config path")
  111. flag.StringVar(&confName, "conf_name", "video-service.toml", "default config filename")
  112. }
  113. // Init init conf
  114. func Init() error {
  115. if localConf != "" {
  116. return local()
  117. }
  118. return remote()
  119. }
  120. func local() (err error) {
  121. _, err = toml.DecodeFile(localConf, &Conf)
  122. return
  123. }
  124. func remote() (err error) {
  125. if err := paladin.Init(); err != nil {
  126. panic(err)
  127. }
  128. // var setter
  129. if err := paladin.Watch(confName, Conf); err != nil {
  130. panic(err)
  131. }
  132. if err := paladin.Watch("rule.toml", ArchiveRules); err != nil {
  133. panic(err)
  134. }
  135. return
  136. }