conf.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/queue/databus"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/conf"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/trace"
  12. "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. // Conf global variable.
  16. var (
  17. Conf = &Config{}
  18. client *conf.Client
  19. confPath string
  20. )
  21. // Config struct of conf.
  22. type Config struct {
  23. // base
  24. // log
  25. Log *log.Config
  26. // db
  27. DB *DB
  28. // mc
  29. Memcache *Memcache
  30. LevelExpire int32
  31. // http
  32. BM *HTTPServers
  33. // http client
  34. HTTPClient HTTPClient
  35. // app
  36. App *bm.App
  37. // tracer
  38. Tracer *trace.Config
  39. // switch
  40. Switch *ConfigSwitch
  41. // report
  42. Report *databus.Config
  43. }
  44. // ConfigSwitch switch config.
  45. type ConfigSwitch struct {
  46. QueryExp uint64
  47. }
  48. // DB db config.
  49. type DB struct {
  50. Exp *sql.Config
  51. }
  52. // Memcache config
  53. type Memcache struct {
  54. Exp *memcache.Config
  55. ExpExpire time.Duration
  56. }
  57. // HTTPServers Http Servers
  58. type HTTPServers struct {
  59. Inner *bm.ServerConfig
  60. Local *bm.ServerConfig
  61. }
  62. // HTTPClient config
  63. type HTTPClient struct {
  64. Read *bm.ClientConfig
  65. Write *bm.ClientConfig
  66. }
  67. func local() (err error) {
  68. _, err = toml.DecodeFile(confPath, &Conf)
  69. return
  70. }
  71. func remote() (err error) {
  72. if client, err = conf.New(); err != nil {
  73. return
  74. }
  75. if err = load(); err != nil {
  76. return
  77. }
  78. go func() {
  79. for range client.Event() {
  80. log.Info("config event")
  81. }
  82. }()
  83. return
  84. }
  85. func load() (err error) {
  86. var (
  87. s string
  88. ok bool
  89. tmpConf *Config
  90. )
  91. if s, ok = client.Toml2(); !ok {
  92. return errors.New("load config center error")
  93. }
  94. if _, err = toml.Decode(s, &tmpConf); err != nil {
  95. return errors.New("could not decode config")
  96. }
  97. *Conf = *tmpConf
  98. return
  99. }
  100. func init() {
  101. flag.StringVar(&confPath, "conf", "", "default config path")
  102. }
  103. // Init int config
  104. func Init() error {
  105. if confPath != "" {
  106. return local()
  107. }
  108. return remote()
  109. }