conf.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. // global var
  16. var (
  17. confPath string
  18. client *conf.Client
  19. // Conf config
  20. Conf = &Config{}
  21. )
  22. // Memcache memcache.
  23. type Memcache struct {
  24. *memcache.Config
  25. Expire time.Duration
  26. }
  27. // Config config set
  28. type Config struct {
  29. // elk
  30. Log *log.Config
  31. // http
  32. BM *bm.ServerConfig
  33. // ecode
  34. Ecode *ecode.Config
  35. HTTPClient *bm.ClientConfig
  36. Memcache *Memcache
  37. // orm
  38. ORM *orm.Config
  39. Mail *Mail
  40. Scheduler *Scheduler
  41. Auth *permit.Config
  42. Tapd *Tapd
  43. }
  44. // Mail mail
  45. type Mail struct {
  46. Host string
  47. Port int
  48. Username string
  49. Password string
  50. NoticeOwner []string
  51. }
  52. // Scheduler Scheduler.
  53. type Scheduler struct {
  54. UpdateHookURLCacheTask string
  55. Active bool
  56. }
  57. // Tapd Tapd.
  58. type Tapd struct {
  59. CallbackToken string
  60. UseCache bool
  61. }
  62. func init() {
  63. flag.StringVar(&confPath, "conf", "", "default config path")
  64. }
  65. // Init init conf
  66. func Init() error {
  67. if confPath != "" {
  68. return local()
  69. }
  70. return remote()
  71. }
  72. func local() (err error) {
  73. _, err = toml.DecodeFile(confPath, &Conf)
  74. return
  75. }
  76. func remote() (err error) {
  77. if client, err = conf.New(); err != nil {
  78. return
  79. }
  80. return load()
  81. }
  82. func load() (err error) {
  83. var (
  84. s string
  85. ok bool
  86. tmpConf *Config
  87. )
  88. if s, ok = client.Toml2(); !ok {
  89. return errors.New("load config center error")
  90. }
  91. if _, err = toml.Decode(s, &tmpConf); err != nil {
  92. return errors.New("could not decode config")
  93. }
  94. *Conf = *tmpConf
  95. return
  96. }