conf.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. ecode "go-common/library/ecode/tip"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/http/blademaster/middleware/verify"
  10. "go-common/library/net/trace"
  11. "github.com/BurntSushi/toml"
  12. "go-common/library/database/orm"
  13. "go-common/library/net/rpc"
  14. "go-common/library/net/rpc/warden"
  15. "go-common/library/queue/databus"
  16. "path"
  17. )
  18. var (
  19. confPath string
  20. client *conf.Client
  21. // Conf config
  22. Conf = &Config{}
  23. )
  24. // Config .
  25. type Config struct {
  26. Log *log.Config
  27. BM *bm.ServerConfig
  28. Verify *verify.Config
  29. Tracer *trace.Config
  30. Ecode *ecode.Config
  31. // rpc client
  32. AccountRPC *rpc.ClientConfig
  33. // gorm
  34. Upcrm *orm.Config
  35. ArchiveOrm *orm.Config
  36. MailConf *Mail
  37. MailTemplateConf *MailTemplateConfig
  38. DatabusConf *DataBusConfig
  39. GRPCClient *GRPCClient
  40. // cron job
  41. Job *JobCron
  42. IsTest bool
  43. }
  44. func init() {
  45. flag.StringVar(&confPath, "conf", "", "default config path")
  46. }
  47. // Init init conf
  48. func Init() error {
  49. if confPath != "" {
  50. return local()
  51. }
  52. return remote()
  53. }
  54. func local() (err error) {
  55. _, err = toml.DecodeFile(confPath, &Conf)
  56. if err != nil {
  57. return
  58. }
  59. var templateConfPath = path.Join(path.Dir(confPath), "mail-template.toml")
  60. _, err = toml.DecodeFile(templateConfPath, &Conf)
  61. return
  62. }
  63. func remote() (err error) {
  64. if client, err = conf.New(); err != nil {
  65. return
  66. }
  67. if err = load(); err != nil {
  68. return
  69. }
  70. go func() {
  71. for range client.Event() {
  72. log.Info("config reload")
  73. if load() != nil {
  74. log.Error("config reload error (%v)", err)
  75. }
  76. }
  77. }()
  78. return
  79. }
  80. func load() (err error) {
  81. var (
  82. s string
  83. ok bool
  84. tmpConf *Config
  85. )
  86. if s, ok = client.Toml2(); !ok {
  87. return errors.New("load config center error")
  88. }
  89. if _, err = toml.Decode(s, &tmpConf); err != nil {
  90. return errors.New("could not decode config")
  91. }
  92. *Conf = *tmpConf
  93. return
  94. }
  95. //Mail 邮件配置
  96. type Mail struct {
  97. Host string
  98. Port int
  99. Username, Password string
  100. DueMailReceivers []string // []adminname, send to adminname@bilibili.com
  101. }
  102. //JobCron 任务时间配置
  103. type JobCron struct {
  104. UpCheckDateDueTaskTime string
  105. TaskScheduleTime string
  106. CheckStateJobTime string
  107. UpdateUpTidJobTime string
  108. }
  109. //MailTemplateConfig mail template conf
  110. type MailTemplateConfig struct {
  111. SignTmplTitle string
  112. SignTmplContent string
  113. PayTmplTitle string
  114. PayTmplContent string
  115. TaskTmplTitle string
  116. TaskTmplContent string
  117. }
  118. //DataBusConfig databus config
  119. type DataBusConfig struct {
  120. ArchiveNotify *databus.Config
  121. Archive *databus.Config
  122. }
  123. // GRPCClient .
  124. type GRPCClient struct {
  125. Up *warden.ClientConfig
  126. Archive *warden.ClientConfig
  127. }