conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/trace"
  11. "go-common/library/queue/databus"
  12. "go-common/library/queue/databus/databusutil"
  13. xtime "go-common/library/time"
  14. "github.com/BurntSushi/toml"
  15. )
  16. var (
  17. confPath string
  18. // Conf conf.
  19. Conf = &Config{}
  20. client *conf.Client
  21. )
  22. // Config config.
  23. type Config struct {
  24. // log
  25. Xlog *log.Config
  26. // Tracer tracer
  27. Tracer *trace.Config
  28. // http
  29. BM *bm.ServerConfig
  30. // DB
  31. DB *DB
  32. Memcache *Memcache
  33. //Databus databus
  34. DataBus *DataBus
  35. // DataUtil config
  36. DatabusUtil *databusutil.Config
  37. // SyncConf
  38. SyncConf *SyncConf
  39. }
  40. // DB db config
  41. type DB struct {
  42. Aso *sql.Config
  43. Sns *sql.Config
  44. }
  45. // Memcache memcache
  46. type Memcache struct {
  47. *memcache.Config
  48. Expire xtime.Duration
  49. }
  50. // DataBus databus.
  51. type DataBus struct {
  52. SnsLogSub *databus.Config
  53. AsoBinLogSub *databus.Config
  54. }
  55. // SyncConf sync conf
  56. type SyncConf struct {
  57. IncSwitch bool
  58. FullSwitch bool
  59. CheckSwitch bool
  60. ChanNum int
  61. ChanSize int
  62. CheckTicker xtime.Duration
  63. }
  64. func init() {
  65. flag.StringVar(&confPath, "conf", "", "default config path")
  66. }
  67. // Init init config.
  68. func Init() (err error) {
  69. if confPath != "" {
  70. return local()
  71. }
  72. return remote()
  73. }
  74. func local() (err error) {
  75. _, err = toml.DecodeFile(confPath, &Conf)
  76. return
  77. }
  78. func remote() (err error) {
  79. if client, err = conf.New(); err != nil {
  80. return
  81. }
  82. if err = load(); err != nil {
  83. return
  84. }
  85. go func() {
  86. for range client.Event() {
  87. log.Info("config reload")
  88. if load() != nil {
  89. log.Error("config reload error (%v)", err)
  90. }
  91. }
  92. }()
  93. return
  94. }
  95. func load() (err error) {
  96. var (
  97. s string
  98. ok bool
  99. tmpConf = &Config{}
  100. )
  101. if s, ok = client.Toml2(); !ok {
  102. return errors.New("load config center error")
  103. }
  104. if _, err = toml.Decode(s, tmpConf); err != nil {
  105. return errors.New("could not decode config")
  106. }
  107. *Conf = *tmpConf
  108. return
  109. }