conf.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "path"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/conf"
  8. "go-common/library/database/sql"
  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/rpc/warden"
  13. "go-common/library/net/trace"
  14. "github.com/BurntSushi/toml"
  15. )
  16. var (
  17. confPath string
  18. client *conf.Client
  19. // Conf config
  20. Conf = &Config{}
  21. )
  22. // Config .
  23. type Config struct {
  24. Log *log.Config
  25. BM *bm.ServerConfig
  26. Tracer *trace.Config
  27. Memcache *memcache.Config
  28. MySQL *sql.Config
  29. Ecode *ecode.Config
  30. // Property
  31. Property *Property
  32. // mail
  33. MailConf *Mail
  34. MailTemplateConf *MailTemplateConfig
  35. // rpc client
  36. GRPCClient *RPC
  37. }
  38. // RPC rpc client config.
  39. type RPC struct {
  40. Account *warden.ClientConfig
  41. }
  42. // Property config for biz logic.
  43. type Property struct {
  44. UpMcnSignStateCron string
  45. UpMcnUpStateCron string
  46. UpExpirePayCron string
  47. UpMcnDataSummaryCron string
  48. McnRecommendCron string
  49. DealFailRecommendCron string
  50. CheckMcnSignUpDueCron string
  51. }
  52. // Mail 邮件配置
  53. type Mail struct {
  54. Host string
  55. Port int
  56. Username, Password string
  57. DueMailReceivers []string // []adminname, send to adminname@bilibili.com
  58. DueAuthorityGroups []string
  59. }
  60. //MailTemplateConfig mail template conf
  61. type MailTemplateConfig struct {
  62. SignTmplTitle string
  63. SignTmplContent string
  64. PayTmplTitle string
  65. PayTmplContent string
  66. }
  67. func init() {
  68. flag.StringVar(&confPath, "conf", "", "default config path")
  69. }
  70. // Init init conf
  71. func Init() error {
  72. if confPath != "" {
  73. return local()
  74. }
  75. return remote()
  76. }
  77. func local() (err error) {
  78. _, err = toml.DecodeFile(confPath, &Conf)
  79. if err != nil {
  80. return
  81. }
  82. var templateConfPath = path.Join(path.Dir(confPath), "mail-template.toml")
  83. _, err = toml.DecodeFile(templateConfPath, &Conf)
  84. return
  85. }
  86. func remote() (err error) {
  87. if client, err = conf.New(); err != nil {
  88. return
  89. }
  90. if err = load(); err != nil {
  91. return
  92. }
  93. go func() {
  94. for range client.Event() {
  95. log.Info("config reload")
  96. if load() != nil {
  97. log.Error("config reload error (%v)", err)
  98. }
  99. }
  100. }()
  101. return
  102. }
  103. func load() (err error) {
  104. var (
  105. s string
  106. ok bool
  107. tmpConf *Config
  108. )
  109. if s, ok = client.Toml2(); !ok {
  110. return errors.New("load config center error")
  111. }
  112. if _, err = toml.Decode(s, &tmpConf); err != nil {
  113. return errors.New("could not decode config")
  114. }
  115. *Conf = *tmpConf
  116. return
  117. }