conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/rpc"
  10. "go-common/library/net/trace"
  11. "go-common/library/queue/databus"
  12. "go-common/library/queue/databus/databusutil"
  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. // Config config set
  23. type Config struct {
  24. // base
  25. // elk
  26. Log *log.Config
  27. // http
  28. BM *bm.ServerConfig
  29. // identify
  30. //Identify *identify.Config
  31. // tracer
  32. Tracer *trace.Config
  33. // MySQL
  34. MySQL *sql.Config
  35. // MySQL
  36. OldMySQL *sql.Config
  37. // Databus
  38. Databus *databus.Config
  39. // AuthDataBus authService binlog
  40. AuthDataBus *databus.Config
  41. // DataUtil config
  42. DatabusUtil *databusutil.Config
  43. // auth rpc
  44. AuthRPC *rpc.ClientConfig
  45. // HTTPClientConfig
  46. HTTPClientConfig *bm.ClientConfig
  47. // AuthJobConfig job config
  48. AuthJobConfig *AuthJobConfig
  49. // user defined
  50. SyncLines int64
  51. IDXFrom int64
  52. IDXTo int64
  53. }
  54. // AuthJobConfig auth job config
  55. type AuthJobConfig struct {
  56. AsoCleanURL string
  57. }
  58. func init() {
  59. flag.StringVar(&confPath, "conf", "", "default config path")
  60. }
  61. // Init init conf
  62. func Init() error {
  63. if confPath != "" {
  64. return local()
  65. }
  66. return remote()
  67. }
  68. func local() (err error) {
  69. _, err = toml.DecodeFile(confPath, &Conf)
  70. return
  71. }
  72. func remote() (err error) {
  73. if client, err = conf.New(); err != nil {
  74. return
  75. }
  76. if err = load(); err != nil {
  77. return
  78. }
  79. go func() {
  80. for range client.Event() {
  81. log.Info("config reload")
  82. if load() != nil {
  83. log.Error("config reload error (%v)", err)
  84. }
  85. }
  86. }()
  87. return
  88. }
  89. func load() (err error) {
  90. var (
  91. s string
  92. ok bool
  93. tmpConf *Config
  94. )
  95. if s, ok = client.Value("passport-auth-job.toml"); !ok {
  96. return errors.New("load config center error")
  97. }
  98. if _, err = toml.Decode(s, &tmpConf); err != nil {
  99. return errors.New("could not decode config")
  100. }
  101. *Conf = *tmpConf
  102. return
  103. }