conf.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/sql"
  9. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/permit"
  13. "go-common/library/net/rpc/warden"
  14. "go-common/library/net/trace"
  15. "go-common/library/queue/databus"
  16. "go-common/library/time"
  17. "github.com/BurntSushi/toml"
  18. )
  19. // Conf global variable.
  20. var (
  21. Conf = &Config{}
  22. client *conf.Client
  23. confPath string
  24. )
  25. // Config struct of conf.
  26. type Config struct {
  27. // base
  28. // log
  29. Log *log.Config
  30. // tracer
  31. Tracer *trace.Config
  32. // http
  33. BM *bm.ServerConfig
  34. // Auth auth
  35. Auth *permit.Config
  36. // MySQL mysql
  37. DB *DB
  38. // BFS
  39. BFS *BFS
  40. // redis
  41. Redis *redis.Config
  42. // memcache
  43. Memcache *Memcache
  44. // AccountGRPC account grpc
  45. AccountGRPC *warden.ClientConfig
  46. // http client
  47. HTTPClient *bm.ClientConfig
  48. // databus
  49. AccountNotify *databus.Config
  50. // host
  51. Host *Host
  52. // ecodes
  53. Ecode *ecode.Config
  54. }
  55. // Host host config .
  56. type Host struct {
  57. Bfs string
  58. Msg string
  59. Manager string
  60. }
  61. // BFS bfs config
  62. type BFS struct {
  63. Bucket string
  64. Key string
  65. Secret string
  66. }
  67. // DB .
  68. type DB struct {
  69. Usersuit *sql.Config
  70. }
  71. // Memcache config.
  72. type Memcache struct {
  73. *memcache.Config
  74. Expire time.Duration
  75. PointExpire time.Duration
  76. }
  77. func local() (err error) {
  78. _, err = toml.DecodeFile(confPath, &Conf)
  79. return
  80. }
  81. func remote() (err error) {
  82. if client, err = conf.New(); err != nil {
  83. return
  84. }
  85. if err = load(); err != nil {
  86. return
  87. }
  88. go func() {
  89. for range client.Event() {
  90. log.Info("config reload")
  91. if load() != nil {
  92. log.Error("config reload error (%v)", err)
  93. }
  94. }
  95. }()
  96. return
  97. }
  98. func load() (err error) {
  99. var (
  100. s string
  101. ok bool
  102. tmpConf *Config
  103. )
  104. if s, ok = client.Toml2(); !ok {
  105. return errors.New("load config center error")
  106. }
  107. if _, err = toml.Decode(s, &tmpConf); err != nil {
  108. return errors.New("could not decode config")
  109. }
  110. *Conf = *tmpConf
  111. return
  112. }
  113. func init() {
  114. flag.StringVar(&confPath, "conf", "", "default config path")
  115. }
  116. // Init int config
  117. func Init() error {
  118. if confPath != "" {
  119. return local()
  120. }
  121. return remote()
  122. }