conf.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package conf
  2. import (
  3. "flag"
  4. "strings"
  5. "go-common/app/admin/ep/saga/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/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/permit"
  13. "go-common/library/net/trace"
  14. xtime "go-common/library/time"
  15. "github.com/BurntSushi/toml"
  16. "github.com/pkg/errors"
  17. )
  18. const (
  19. _configKey = "saga-admin.toml"
  20. )
  21. var (
  22. confPath string
  23. client *conf.Client
  24. // Conf store the global config
  25. Conf = &Config{}
  26. reload chan bool
  27. )
  28. // Config def.
  29. type Config struct {
  30. Tracer *trace.Config
  31. BM *bm.ServerConfig
  32. HTTPClient *bm.ClientConfig
  33. Memcache *Memcache
  34. Redis *redis.Config
  35. Log *log.Config
  36. ORM *orm.Config
  37. Permit *permit.Config2
  38. Property *Property
  39. }
  40. // Memcache config.
  41. type Memcache struct {
  42. MC *memcache.Config
  43. MCRecordExpire xtime.Duration
  44. }
  45. // Property config for biz logic.
  46. type Property struct {
  47. Gitlab *struct {
  48. API string // gitlab api host
  49. Token string // saga 账户 access token
  50. }
  51. Git *struct {
  52. API string // gitlab api host
  53. Token string // saga 账户 access token
  54. CheckCron string
  55. UserList []string
  56. AlertPipeline []*model.AlertPipeline
  57. }
  58. SyncProject *struct {
  59. CheckCron string
  60. }
  61. SyncData *struct {
  62. SyncAllTime bool
  63. DefaultSyncDays int
  64. CheckCron string
  65. CheckCronAll string
  66. CheckCronWeek string
  67. WechatUser []string
  68. }
  69. Department *model.PairKey
  70. Business *model.PairKey
  71. DeInfo []*model.PairKey
  72. BuInfo []*model.PairKey
  73. Mail *struct {
  74. Host string
  75. Port int
  76. Address string
  77. Pwd string
  78. Name string
  79. }
  80. Wechat *model.AppConfig
  81. Contact *model.AppConfig
  82. Group *struct {
  83. Name string
  84. Department string
  85. Business string
  86. }
  87. DefaultProject *struct {
  88. ProjectIDs []int
  89. Status []string
  90. Types []int
  91. }
  92. Sven *struct {
  93. ConfigValue string
  94. Configs string
  95. ConfigUpdate string
  96. TagUpdate string
  97. ConfigsParam *model.ConfigsParam
  98. SagaConfigsParam *model.SagaConfigsParam
  99. }
  100. }
  101. func init() {
  102. flag.StringVar(&confPath, "conf", "", "config path")
  103. reload = make(chan bool, 10)
  104. }
  105. // Init init conf.
  106. func Init() (err error) {
  107. if confPath == "" {
  108. return configCenter()
  109. }
  110. if _, err = toml.DecodeFile(confPath, &Conf); err != nil {
  111. log.Error("toml.DecodeFile(%s) err(%+v)", confPath, err)
  112. return
  113. }
  114. Conf = parseTeamInfo(Conf)
  115. return
  116. }
  117. func configCenter() (err error) {
  118. if client, err = conf.New(); err != nil {
  119. panic(err)
  120. }
  121. if err = load(); err != nil {
  122. return
  123. }
  124. client.WatchAll()
  125. go func() {
  126. for range client.Event() {
  127. log.Info("config reload")
  128. if load() != nil {
  129. log.Error("config reload error (%v)", err)
  130. } else {
  131. reload <- true
  132. }
  133. }
  134. }()
  135. return
  136. }
  137. func load() (err error) {
  138. var (
  139. s string
  140. ok bool
  141. tmpConf *Config
  142. )
  143. if s, ok = client.Value(_configKey); !ok {
  144. err = errors.Errorf("load config center error [%s]", _configKey)
  145. return
  146. }
  147. if _, err = toml.Decode(s, &tmpConf); err != nil {
  148. err = errors.Wrapf(err, "could not decode config err(%+v)", err)
  149. return
  150. }
  151. Conf = parseTeamInfo(tmpConf)
  152. return
  153. }
  154. func parseTeamInfo(c *Config) *Config {
  155. DeLabel := strings.Fields(c.Property.Department.Label)
  156. DeValue := strings.Fields(c.Property.Department.Value)
  157. for i := 0; i < len(DeLabel); i++ {
  158. info := &model.PairKey{
  159. Label: DeLabel[i],
  160. Value: DeValue[i],
  161. }
  162. c.Property.DeInfo = append(c.Property.DeInfo, info)
  163. }
  164. buLabel := strings.Fields(c.Property.Business.Label)
  165. buValue := strings.Fields(c.Property.Business.Value)
  166. for i := 0; i < len(buLabel); i++ {
  167. info := &model.PairKey{
  168. Label: buLabel[i],
  169. Value: buValue[i],
  170. }
  171. c.Property.BuInfo = append(c.Property.BuInfo, info)
  172. }
  173. /*for _, r := range c.Property.Developer {
  174. r.Total = r.Android + r.Ios + r.Service + r.Web
  175. }*/
  176. return c
  177. }