conf.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/trace"
  10. "go-common/library/queue/databus"
  11. "github.com/BurntSushi/toml"
  12. )
  13. var (
  14. confPath string
  15. client *conf.Client
  16. // Conf config
  17. Conf = &Config{}
  18. )
  19. // Config .
  20. type Config struct {
  21. Log *log.Config
  22. BM *bm.ServerConfig
  23. Tracer *trace.Config
  24. MySQL *sql.Config
  25. // databus
  26. DataBus *DataBus
  27. // HTTPClientConfig
  28. HTTPClientConfig *bm.ClientConfig
  29. // AccRecover request URL info
  30. AccRecover *AccRecover
  31. }
  32. // AccRecover http request
  33. type AccRecover struct {
  34. CompareURL string
  35. SendMailURL string
  36. }
  37. // DataBus config
  38. type DataBus struct {
  39. CompareDatabus *databus.Config
  40. SendMailDatabus *databus.Config
  41. }
  42. func init() {
  43. flag.StringVar(&confPath, "conf", "", "default config path")
  44. }
  45. // Init init conf
  46. func Init() error {
  47. if confPath != "" {
  48. return local()
  49. }
  50. return remote()
  51. }
  52. func local() (err error) {
  53. _, err = toml.DecodeFile(confPath, &Conf)
  54. return
  55. }
  56. func remote() (err error) {
  57. if client, err = conf.New(); err != nil {
  58. return
  59. }
  60. if err = load(); err != nil {
  61. return
  62. }
  63. go func() {
  64. for range client.Event() {
  65. log.Info("config reload")
  66. if load() != nil {
  67. log.Error("config reload error (%v)", err)
  68. }
  69. }
  70. }()
  71. return
  72. }
  73. func load() (err error) {
  74. var (
  75. s string
  76. ok bool
  77. tmpConf *Config
  78. )
  79. if s, ok = client.Toml2(); !ok {
  80. return errors.New("load config center error")
  81. }
  82. if _, err = toml.Decode(s, &tmpConf); err != nil {
  83. return errors.New("could not decode config")
  84. }
  85. *Conf = *tmpConf
  86. return
  87. }