conf.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "time"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "go-common/library/naming/discovery"
  10. bm "go-common/library/net/http/blademaster"
  11. "github.com/BurntSushi/toml"
  12. )
  13. var (
  14. confPath string
  15. // ConfClient get config client
  16. ConfClient *conf.Client
  17. // Conf canal config variable
  18. Conf = &Config{}
  19. )
  20. // Config canal config struct
  21. type Config struct {
  22. Monitor *Monitor
  23. // xlog
  24. Log *log.Config
  25. // http client
  26. HTTPClient *bm.ClientConfig
  27. // http server
  28. BM *bm.ServerConfig
  29. // master info
  30. MasterInfo *MasterInfoConfig
  31. // discovery
  32. Discovery *discovery.Config
  33. // db
  34. DB *sql.Config
  35. }
  36. // Monitor wechat monitor
  37. type Monitor struct {
  38. User string
  39. Token string
  40. Secret string
  41. }
  42. // MasterInfoConfig save pos of binlog in file or db
  43. type MasterInfoConfig struct {
  44. Addr string `toml:"addr"`
  45. DBName string `toml:"dbName"`
  46. User string `toml:"user"`
  47. Password string `toml:"password"`
  48. Timeout time.Duration `toml:"timeout"`
  49. }
  50. func init() {
  51. flag.StringVar(&confPath, "conf", "", "config path")
  52. flag.StringVar(&canalPath, "canal", "", "canal instance path")
  53. }
  54. //Init int config
  55. func Init() (err error) {
  56. if confPath != "" {
  57. _, err = toml.DecodeFile(confPath, &Conf)
  58. return
  59. }
  60. return remote()
  61. }
  62. func remote() (err error) {
  63. if ConfClient, err = conf.New(); err != nil {
  64. return
  65. }
  66. ConfClient.WatchAll()
  67. err = LoadCanal()
  68. return
  69. }
  70. // LoadCanal canal config
  71. func LoadCanal() (err error) {
  72. var (
  73. s string
  74. ok bool
  75. tmpConf *Config
  76. )
  77. if s, ok = ConfClient.Value("canal.toml"); !ok {
  78. return errors.New("load config center error")
  79. }
  80. if _, err = toml.Decode(s, &tmpConf); err != nil {
  81. return errors.New("could not decode config")
  82. }
  83. *Conf = *tmpConf
  84. return
  85. }