conf.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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"
  14. "go-common/library/net/trace"
  15. "github.com/BurntSushi/toml"
  16. )
  17. // global var
  18. var (
  19. confPath string
  20. client *conf.Client
  21. // Conf config
  22. Conf = &Config{}
  23. )
  24. // Config config set
  25. type Config struct {
  26. // base
  27. // elk
  28. Log *log.Config
  29. // http
  30. BM *bm.ServerConfig
  31. // tracer
  32. Tracer *trace.Config
  33. // redis
  34. Redis *redis.Config
  35. // memcache
  36. Memcache *memcache.Config
  37. // MySQL
  38. MySQL *sql.Config
  39. // ecode
  40. Ecode *ecode.Config
  41. //auth
  42. Auth *permit.Config
  43. //httpclient
  44. HTTPClient *bm.ClientConfig
  45. // rpc server
  46. RPC *RPCClient
  47. }
  48. // RPCClient def.
  49. type RPCClient struct {
  50. Point *rpc.ClientConfig
  51. }
  52. func init() {
  53. flag.StringVar(&confPath, "conf", "", "default config path")
  54. }
  55. // Init init conf
  56. func Init() error {
  57. if confPath != "" {
  58. return local()
  59. }
  60. return remote()
  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 reload")
  76. if load() != nil {
  77. log.Error("config reload error (%v)", err)
  78. }
  79. }
  80. }()
  81. return
  82. }
  83. func load() (err error) {
  84. var (
  85. s string
  86. ok bool
  87. tmpConf *Config
  88. )
  89. if s, ok = client.Toml2(); !ok {
  90. return errors.New("load config center error")
  91. }
  92. if _, err = toml.Decode(s, &tmpConf); err != nil {
  93. return errors.New("could not decode config")
  94. }
  95. *Conf = *tmpConf
  96. return
  97. }