conf.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/app/admin/ep/merlin/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/conf"
  8. "go-common/library/database/orm"
  9. ecode "go-common/library/ecode/tip"
  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/time"
  14. "github.com/BurntSushi/toml"
  15. )
  16. // global var
  17. var (
  18. confPath string
  19. client *conf.Client
  20. // Conf config
  21. Conf = &Config{}
  22. )
  23. // Memcache memcache.
  24. type Memcache struct {
  25. *memcache.Config
  26. Expire time.Duration
  27. }
  28. // Config config set
  29. type Config struct {
  30. Version string `toml:"version"`
  31. // base
  32. // elk
  33. Log *log.Config
  34. // http
  35. BM *bm.ServerConfig
  36. // ecode
  37. Ecode *ecode.Config
  38. HTTPClient *bm.ClientConfig
  39. Memcache *Memcache
  40. // orm
  41. ORM *orm.Config
  42. Paas *model.PaasConf
  43. ServiceTree *model.TreeConf
  44. Mail *Mail
  45. Scheduler *Scheduler
  46. Auth *permit.Config
  47. BiliHub *BiliHub
  48. DeviceFarm *DeviceFarm
  49. WeChat *WeChat
  50. }
  51. //WeChat WeChat config
  52. type WeChat struct {
  53. WeChatHost string
  54. WeChatDeviceFarm *WeChatDeviceFarm
  55. }
  56. // WeChatDeviceFarm WeChatDeviceFarm.
  57. type WeChatDeviceFarm struct {
  58. ChatID string
  59. MsgType string
  60. Safe int
  61. SendMessage bool
  62. }
  63. //DeviceFarm DeviceFarm
  64. type DeviceFarm struct {
  65. HostList []string
  66. SuperOwner []string
  67. ApplyMonthTime int
  68. }
  69. // Scheduler scheduler
  70. type Scheduler struct {
  71. GetExpiredMachinesTime string
  72. SendTaskMailMachinesWillExpiredTime string
  73. DeleteExpiredMachinesInTask string
  74. CheckMachinesStatusInTask string
  75. UpdateMobileDeviceInTask string
  76. UpdateSnapshotStatusInDoing string
  77. Active bool
  78. ExpiredDate int
  79. }
  80. // Mail mail
  81. type Mail struct {
  82. Host string
  83. Port int
  84. Username string
  85. Password string
  86. NoticeOwner []string
  87. }
  88. // BiliHub BiliHub.
  89. type BiliHub struct {
  90. Host string
  91. HostName string
  92. Username string
  93. Password string
  94. MerlinPub string
  95. SharePub string
  96. MachineTagPri string
  97. SupportNetWork []string
  98. SuperOwner []string
  99. }
  100. func init() {
  101. flag.StringVar(&confPath, "conf", "", "default config path")
  102. }
  103. // Init init conf
  104. func Init() error {
  105. if confPath != "" {
  106. return local()
  107. }
  108. return remote()
  109. }
  110. func local() (err error) {
  111. _, err = toml.DecodeFile(confPath, &Conf)
  112. return
  113. }
  114. func remote() (err error) {
  115. if client, err = conf.New(); err != nil {
  116. return
  117. }
  118. return load()
  119. }
  120. func load() (err error) {
  121. var (
  122. s string
  123. ok bool
  124. tmpConf *Config
  125. )
  126. if s, ok = client.Toml2(); !ok {
  127. return errors.New("load config center error")
  128. }
  129. if _, err = toml.Decode(s, &tmpConf); err != nil {
  130. return errors.New("could not decode config")
  131. }
  132. *Conf = *tmpConf
  133. return
  134. }