conf.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // Conf global variable.
  17. var (
  18. Conf = &Config{}
  19. client *conf.Client
  20. confPath string
  21. )
  22. // Config struct of conf.
  23. type Config struct {
  24. // base
  25. // log
  26. Log *log.Config
  27. //Tracer *conf.Tracer
  28. Tracer *trace.Config
  29. //Databus databus
  30. DataBus *DataBus
  31. // Databusutil
  32. Databusutil *databusutil.Config
  33. // memcache
  34. Memcaches map[string]*Memcache
  35. // BM
  36. BM *bm.ServerConfig
  37. // AuthDB
  38. AuthDB *sql.Config
  39. // AuthMC
  40. AuthMC *memcache.Config
  41. // CheckConf
  42. CheckConf *CheckConf
  43. }
  44. // DataBus databus.
  45. type DataBus struct {
  46. IdentifySub *databus.Config
  47. AuthDataBus *databus.Config
  48. }
  49. // Memcache contains prefix
  50. type Memcache struct {
  51. Prefix string
  52. *memcache.Config
  53. }
  54. // CheckConf .
  55. type CheckConf struct {
  56. Switch bool
  57. ChanNum int
  58. ChanSize int
  59. Ticker xtime.Duration
  60. Count int64
  61. }
  62. func local() (err error) {
  63. _, err = toml.DecodeFile(confPath, &Conf)
  64. return
  65. }
  66. func remote() (err error) {
  67. if client, err = conf.New(); err != nil {
  68. return
  69. }
  70. if err = load(); err != nil {
  71. return
  72. }
  73. go func() {
  74. for range client.Event() {
  75. log.Info("config event")
  76. }
  77. }()
  78. return
  79. }
  80. func load() (err error) {
  81. var (
  82. s string
  83. ok bool
  84. tmpConf *Config
  85. )
  86. if s, ok = client.Toml2(); !ok {
  87. return errors.New("load config center error")
  88. }
  89. if _, err = toml.Decode(s, &tmpConf); err != nil {
  90. return errors.New("could not decode config")
  91. }
  92. *Conf = *tmpConf
  93. return
  94. }
  95. func init() {
  96. flag.StringVar(&confPath, "conf", "", "default config path")
  97. }
  98. // Init int config
  99. func Init() error {
  100. if confPath != "" {
  101. return local()
  102. }
  103. return remote()
  104. }