conf.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  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. xtime "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. var (
  16. confPath string
  17. client *conf.Client
  18. // Conf Config
  19. Conf = &Config{}
  20. )
  21. // Config .
  22. type Config struct {
  23. Log *log.Config
  24. Bugly *BuglyConf
  25. BM *bm.ServerConfig
  26. Ecode *ecode.Config
  27. ORM *orm.Config
  28. HTTPClient *bm.ClientConfig
  29. Mail *Mail
  30. Auth *permit.Config
  31. Memcache *Memcache
  32. Scheduler *Scheduler
  33. Tapd *TapdConf
  34. }
  35. func init() {
  36. flag.StringVar(&confPath, "conf", "", "default config path")
  37. }
  38. // Scheduler scheduler
  39. type Scheduler struct {
  40. BatchRunEnableVersion string
  41. BatchRunUpdateTapdBug string
  42. DisableBatchRunOverTime string
  43. BatchRunOverHourTime int
  44. SyncWechatContact string
  45. Active bool
  46. }
  47. // Memcache memcache
  48. type Memcache struct {
  49. *memcache.Config
  50. Expire xtime.Duration
  51. }
  52. // Mail mail
  53. type Mail struct {
  54. Host string
  55. Port int
  56. Username string
  57. Password string
  58. NoticeOwner []string
  59. }
  60. // BuglyConf Bugly Conf.
  61. type BuglyConf struct {
  62. Host string
  63. UrlRetryCount int
  64. CookieUsageUpper int
  65. IssuePageSize int
  66. IssueCountUpper int
  67. SuperOwner []string
  68. }
  69. // TapdConf Tapd Conf.
  70. type TapdConf struct {
  71. BugOperateAuth bool
  72. }
  73. // Tapd Tapd info
  74. type Tapd struct {
  75. IterationWorkspaceIDs []string
  76. StoryWorkspaceIDs []string
  77. BugWorkspaceIDs []string
  78. IPS int
  79. SPS int
  80. SCPS int
  81. CPS int
  82. StoryFilePath string
  83. ChangeFilePath string
  84. IterationFilePath string
  85. BugFilePath string
  86. RetryTime int
  87. WaitTime xtime.Duration
  88. }
  89. // Init init conf
  90. func Init() error {
  91. if confPath != "" {
  92. return local()
  93. }
  94. return remote()
  95. }
  96. func local() (err error) {
  97. _, err = toml.DecodeFile(confPath, &Conf)
  98. return
  99. }
  100. func remote() (err error) {
  101. if client, err = conf.New(); err != nil {
  102. return
  103. }
  104. if err = load(); err != nil {
  105. return
  106. }
  107. go func() {
  108. for range client.Event() {
  109. log.Info("config reload")
  110. if load() != nil {
  111. log.Error("config reload error (%v)", err)
  112. }
  113. }
  114. }()
  115. return
  116. }
  117. func load() (err error) {
  118. var (
  119. s string
  120. ok bool
  121. tmpConf *Config
  122. )
  123. if s, ok = client.Toml2(); !ok {
  124. return errors.New("load config center error")
  125. }
  126. if _, err = toml.Decode(s, &tmpConf); err != nil {
  127. return errors.New("could not decode config")
  128. }
  129. *Conf = *tmpConf
  130. return
  131. }