conf.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/app/job/main/aegis/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/conf"
  9. "go-common/library/database/orm"
  10. "go-common/library/database/sql"
  11. ecode "go-common/library/ecode/tip"
  12. "go-common/library/log"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/net/http/blademaster/middleware/verify"
  15. "go-common/library/net/rpc"
  16. "go-common/library/net/rpc/warden"
  17. "go-common/library/net/trace"
  18. "go-common/library/queue/databus"
  19. "go-common/library/queue/databus/databusutil"
  20. "github.com/BurntSushi/toml"
  21. )
  22. var (
  23. confPath string
  24. client *conf.Client
  25. // Conf config
  26. Conf = &Config{}
  27. )
  28. // Config .
  29. type Config struct {
  30. Debug bool
  31. Log *log.Config
  32. BM *bm.ServerConfig
  33. Verify *verify.Config
  34. Tracer *trace.Config
  35. Redis *redis.Config
  36. Memcache *memcache.Config
  37. MySQL *MySQL
  38. Ecode *ecode.Config
  39. // ORM
  40. ORM *orm.Config
  41. // DataBus databus
  42. DataBus *DataBus
  43. // Databusutil
  44. Databusutil *Databusutil
  45. // RPC
  46. RPC *RPC
  47. //GRPC
  48. GRPC *GRPC
  49. // BizConfiger
  50. BizCfg BizConfiger
  51. HTTP *HTTP
  52. Host *Host
  53. // mail
  54. Mail *Mail
  55. }
  56. //MySQL .
  57. type MySQL struct {
  58. Slow *sql.Config
  59. Fast *sql.Config
  60. }
  61. //Host .
  62. type Host struct {
  63. API string
  64. Videoup string
  65. }
  66. //HTTP .
  67. type HTTP struct {
  68. Fast *bm.ClientConfig
  69. Slow *bm.ClientConfig
  70. }
  71. //BizConfiger .
  72. type BizConfiger struct {
  73. WeightOpt []*model.WeightOPT
  74. }
  75. //RPC .
  76. type RPC struct {
  77. Rel *rpc.ClientConfig
  78. Up *rpc.ClientConfig
  79. }
  80. //GRPC .
  81. type GRPC struct {
  82. Up *warden.ClientConfig
  83. Acc *warden.ClientConfig
  84. }
  85. // DataBus databus infomation
  86. type DataBus struct {
  87. BinLogSub *databus.Config
  88. ResourceSub *databus.Config
  89. TaskSub *databus.Config
  90. ArchiveSub *databus.Config
  91. }
  92. //Mail 邮件配置
  93. type Mail struct {
  94. Host string
  95. Port int
  96. Username, Password string
  97. }
  98. // Databusutil databus group
  99. type Databusutil struct {
  100. Task *databusutil.Config
  101. Resource *databusutil.Config
  102. }
  103. func init() {
  104. flag.StringVar(&confPath, "conf", "", "default config path")
  105. }
  106. // Init init conf
  107. func Init() error {
  108. if confPath != "" {
  109. return local()
  110. }
  111. return remote()
  112. }
  113. func local() (err error) {
  114. _, err = toml.DecodeFile(confPath, &Conf)
  115. return
  116. }
  117. func remote() (err error) {
  118. if client, err = conf.New(); err != nil {
  119. return
  120. }
  121. if err = load(); err != nil {
  122. return
  123. }
  124. go func() {
  125. for range client.Event() {
  126. log.Info("config reload")
  127. if load() != nil {
  128. log.Error("config reload error (%v)", err)
  129. }
  130. }
  131. }()
  132. return
  133. }
  134. func load() (err error) {
  135. var (
  136. s string
  137. ok bool
  138. tmpConf *Config
  139. )
  140. if s, ok = client.Toml2(); !ok {
  141. return errors.New("load config center error")
  142. }
  143. if _, err = toml.Decode(s, &tmpConf); err != nil {
  144. return errors.New("could not decode config")
  145. }
  146. *Conf = *tmpConf
  147. return
  148. }