conf.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/orm"
  9. "go-common/library/database/sql"
  10. "go-common/library/log"
  11. "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/permit"
  13. "go-common/library/net/rpc"
  14. "go-common/library/net/rpc/warden"
  15. "go-common/library/net/trace"
  16. "go-common/library/queue/databus"
  17. "go-common/library/time"
  18. "go-common/library/database/hbase.v2"
  19. "github.com/BurntSushi/toml"
  20. )
  21. // Conf info.
  22. var (
  23. ConfPath string
  24. Conf = &Config{}
  25. client *conf.Client
  26. )
  27. // Config struct.
  28. type Config struct {
  29. // bm
  30. HTTPServer *blademaster.ServerConfig
  31. // db
  32. DB *DB
  33. // base
  34. // elk
  35. XLog *log.Config
  36. // report log
  37. LogCli *log.AgentConfig
  38. // httpClinet
  39. HTTPClient *HTTPClient
  40. // tracer
  41. Tracer *trace.Config
  42. // host
  43. Host *Host
  44. // ArchStatus
  45. ArchStatus map[string]string
  46. // Redis
  47. Redis *Redis
  48. // Databus
  49. Env string
  50. Consume bool
  51. IsTest bool
  52. UpSub *upSub
  53. ChanSize int64
  54. Monitor *Monitor
  55. // rpc client2
  56. ArticleRPC *rpc.ClientConfig
  57. // identify
  58. App *blademaster.App
  59. Memcache *MC
  60. //API HOST
  61. API string
  62. Live string
  63. // auth
  64. Auth *permit.Config
  65. // hbase
  66. HBase *HBaseConfig
  67. HBase2 *HBaseConfig
  68. BfsConf *Bfs
  69. Debug bool
  70. TimeConf *TimeConfig
  71. MailConf *Mail
  72. // manager log config
  73. ManagerLog *databus.Config
  74. // GRPCClient
  75. GRPCClient *GRPC
  76. }
  77. // GRPC .
  78. type GRPC struct {
  79. Archive *warden.ClientConfig
  80. Account *warden.ClientConfig
  81. }
  82. // HBaseConfig for new hbase client.
  83. type HBaseConfig struct {
  84. *hbase.Config
  85. WriteTimeout time.Duration
  86. ReadTimeout time.Duration
  87. }
  88. //UpSub upsub config
  89. type upSub struct {
  90. *databus.Config
  91. UpChanSize int
  92. ConsumeLimit int
  93. RoutineLimit int
  94. }
  95. // DB conf.
  96. type DB struct {
  97. // Creative db
  98. Creative *sql.Config
  99. Manager *sql.Config
  100. Upcrm *orm.Config
  101. }
  102. // Redis conf.
  103. type Redis struct {
  104. Databus *struct {
  105. *redis.Config
  106. Expire time.Duration
  107. }
  108. }
  109. // HTTPClient conf.
  110. type HTTPClient struct {
  111. Normal *blademaster.ClientConfig
  112. Slow *blademaster.ClientConfig
  113. }
  114. // Host conf.
  115. type Host struct {
  116. API string
  117. Live string
  118. Search string
  119. Manager string
  120. Data string
  121. Tag string
  122. Coverrec string
  123. Videoup string
  124. }
  125. // Monitor conf.
  126. type Monitor struct {
  127. Host string
  128. Moni string
  129. UserName string
  130. AppSecret string
  131. AppToken string
  132. IntervalAlarm time.Duration
  133. }
  134. //App for key secret.
  135. type App struct {
  136. Key string
  137. Secret string
  138. }
  139. //MC memcache
  140. type MC struct {
  141. UpExpire time.Duration
  142. Up *memcache.Config
  143. }
  144. // Bfs struct.
  145. type Bfs struct {
  146. Addr string
  147. Bucket string
  148. Key string
  149. Secret string
  150. MaxFileSize int
  151. }
  152. //TimeConfig 定期任务时间
  153. type TimeConfig struct {
  154. TaskScheduleTime string // 每天定时检查完成的task情况,format "10:59:59"
  155. CheckDueScheduleTime string // 每天定时检查快要过期的任务,format "10:59:59"
  156. RefreshUpRankTime string // 每天定时检查upRank表的任务,format "10:59:59"
  157. }
  158. //Mail 邮件配置
  159. type Mail struct {
  160. Host string
  161. Port int
  162. Username, Password string
  163. }
  164. func init() {
  165. flag.StringVar(&ConfPath, "conf", "", "default config path")
  166. }
  167. // Init conf.
  168. func Init() (err error) {
  169. if ConfPath != "" {
  170. return local()
  171. }
  172. return remote()
  173. }
  174. func local() (err error) {
  175. _, err = toml.DecodeFile(ConfPath, &Conf)
  176. return
  177. }
  178. func remote() (err error) {
  179. if client, err = conf.New(); err != nil {
  180. return
  181. }
  182. if err = load(); err != nil {
  183. return
  184. }
  185. go func() {
  186. for range client.Event() {
  187. log.Info("config reload")
  188. if load() != nil {
  189. log.Error("config reload error (%v)", err)
  190. }
  191. }
  192. }()
  193. return
  194. }
  195. func load() (err error) {
  196. var (
  197. tomlStr string
  198. ok bool
  199. tmpConf *Config
  200. )
  201. if tomlStr, ok = client.Value("up-admin.toml"); !ok {
  202. return errors.New("load config center error")
  203. }
  204. if _, err = toml.Decode(tomlStr, &tmpConf); err != nil {
  205. return errors.New("could not decode toml config")
  206. }
  207. *Conf = *tmpConf
  208. return
  209. }