conf.go 2.4 KB

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