conf.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/orm"
  8. ecode "go-common/library/ecode/tip"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/middleware/permit"
  12. "go-common/library/queue/databus"
  13. xtime "go-common/library/time"
  14. "github.com/BurntSushi/toml"
  15. )
  16. var (
  17. // config
  18. confPath string
  19. client *conf.Client
  20. // Conf global config.
  21. Conf = &Config{}
  22. )
  23. // Config def.
  24. type Config struct {
  25. // base
  26. Superman []string
  27. AppToken string
  28. //utBaseLine
  29. UTBaseLine *UTBaseLine
  30. // Host
  31. Host *Host
  32. // Canal
  33. Canal *Canal
  34. // log
  35. Log *log.Config
  36. // ecode
  37. Ecode *ecode.Config
  38. // client
  39. HTTPClient *bm.ClientConfig
  40. // db
  41. ORM *orm.Config
  42. // db databus
  43. ORMDatabus *orm.Config
  44. // db canal
  45. ORMCanal *orm.Config
  46. // kafka
  47. Kafka map[string]*Kafka
  48. // identify
  49. Auth *permit.Config
  50. // discovery
  51. Discovery *Discovery
  52. // tree
  53. Tree *Tree
  54. // databus kafka create topic config
  55. DatabusConfig *Databus
  56. //BM
  57. BM *bm.ServerConfig
  58. // ManagerReport
  59. ManagerReport *databus.Config
  60. // pprof
  61. Pprof *Pprof
  62. //Bfs
  63. Bfs *Bfs
  64. Prometheus *Prometheus
  65. Apps *Apps
  66. Cron *Cron
  67. BroadCast *BroadCast
  68. Gitlab *Gitlab
  69. WeChat *WeChat
  70. //Alarm
  71. Alarm *Alarm
  72. // Redis
  73. // Redis *redis.Config
  74. Redis *Redis
  75. }
  76. // Alarm .
  77. type Alarm struct {
  78. DatabusURL string
  79. DatabusKey string
  80. }
  81. // WeChat .
  82. type WeChat struct {
  83. Users []string
  84. ChatID string
  85. }
  86. // BroadCast .
  87. type BroadCast struct {
  88. TenCent []string
  89. KingSoft []string
  90. }
  91. // Cron .
  92. type Cron struct {
  93. Crontab string
  94. CrontabRepo string
  95. }
  96. // Prometheus .
  97. type Prometheus struct {
  98. URL string
  99. Key string
  100. Secret string
  101. }
  102. // Apps .
  103. type Apps struct {
  104. Name []string
  105. Max int64
  106. }
  107. //UTBaseLine .
  108. type UTBaseLine struct {
  109. Coverage int
  110. Passrate int
  111. }
  112. // Bfs bfs config
  113. type Bfs struct {
  114. Addr string
  115. Bucket string
  116. Key string
  117. Secret string
  118. MaxFileSize int
  119. }
  120. // Pprof dir path
  121. type Pprof struct {
  122. Dir string
  123. GoPath string
  124. }
  125. // Databus config
  126. type Databus struct {
  127. Partitions int32
  128. Factor int16
  129. }
  130. // Tree PlatformID
  131. type Tree struct {
  132. PlatformID string
  133. MsmPlatformID string
  134. }
  135. // Host hosts
  136. type Host struct {
  137. APICo string
  138. SVENCo string
  139. MANAGERCo string
  140. DapperCo string
  141. }
  142. //Canal canal
  143. type Canal struct {
  144. CANALSVENCo string
  145. BUILD string
  146. Reviewer []string
  147. }
  148. // Discovery discovery
  149. type Discovery struct {
  150. API []string
  151. }
  152. // Kafka kafka config
  153. type Kafka struct {
  154. Brokers []string
  155. }
  156. // Gitlab gitlab config
  157. type Gitlab struct {
  158. API string // gitlab api host
  159. Token string // saga 账户 access token
  160. }
  161. // Redis .
  162. type Redis struct {
  163. *redis.Config
  164. ExpireTime xtime.Duration
  165. }
  166. func local() (err error) {
  167. _, err = toml.DecodeFile(confPath, &Conf)
  168. return
  169. }
  170. func remote() (err error) {
  171. if client, err = conf.New(); err != nil {
  172. return
  173. }
  174. if err = load(); err != nil {
  175. return
  176. }
  177. go func() {
  178. for range client.Event() {
  179. log.Info("config reload")
  180. if load() != nil {
  181. log.Error("config reload error (%v)", err)
  182. }
  183. }
  184. }()
  185. return
  186. }
  187. func load() (err error) {
  188. var (
  189. s string
  190. ok bool
  191. tmpConf *Config
  192. )
  193. if s, ok = client.Toml2(); !ok {
  194. return errors.New("load config center error")
  195. }
  196. if _, err = toml.Decode(s, &tmpConf); err != nil {
  197. return errors.New("could not decode config")
  198. }
  199. *Conf = *tmpConf
  200. return
  201. }
  202. func init() {
  203. flag.StringVar(&confPath, "conf", "", "default config path")
  204. }
  205. // Init int config
  206. func Init() error {
  207. if confPath != "" {
  208. return local()
  209. }
  210. return remote()
  211. }