conf.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. ecode "go-common/library/ecode/tip"
  7. "go-common/library/log"
  8. "go-common/library/log/infoc"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/verify"
  11. "go-common/library/net/rpc/warden"
  12. "go-common/library/net/trace"
  13. xtime "go-common/library/time"
  14. "github.com/BurntSushi/toml"
  15. )
  16. var (
  17. confPath string
  18. client *conf.Client
  19. // Conf config
  20. Conf = &Config{}
  21. )
  22. // Config .
  23. type Config struct {
  24. Log *log.Config
  25. BM *bm.ServerConfig
  26. Verify *verify.Config
  27. Tracer *trace.Config
  28. Ecode *ecode.Config
  29. RPCConf *warden.ServerConfig
  30. WorkPool *WorkPoolConfig
  31. JPush *JPushConfig
  32. Infoc *infoc.Config
  33. }
  34. // WorkPoolConfig .
  35. type WorkPoolConfig struct {
  36. Capacity uint64
  37. MaxWorkers uint64
  38. MaxIdleWorkers uint64
  39. MinIdleWorkers uint64
  40. KeepAlive xtime.Duration
  41. }
  42. func init() {
  43. flag.StringVar(&confPath, "conf", "", "default config path")
  44. }
  45. // Init init conf
  46. func Init() error {
  47. if confPath != "" {
  48. return local()
  49. }
  50. return remote()
  51. }
  52. func local() (err error) {
  53. _, err = toml.DecodeFile(confPath, &Conf)
  54. return
  55. }
  56. func remote() (err error) {
  57. if client, err = conf.New(); err != nil {
  58. return
  59. }
  60. if err = load(); err != nil {
  61. return
  62. }
  63. go func() {
  64. for range client.Event() {
  65. log.Info("config reload")
  66. if load() != nil {
  67. log.Error("config reload error (%v)", err)
  68. }
  69. }
  70. }()
  71. return
  72. }
  73. func load() (err error) {
  74. var (
  75. s string
  76. ok bool
  77. tmpConf *Config
  78. )
  79. if s, ok = client.Toml2(); !ok {
  80. return errors.New("load config center error")
  81. }
  82. if _, err = toml.Decode(s, &tmpConf); err != nil {
  83. return errors.New("could not decode config")
  84. }
  85. *Conf = *tmpConf
  86. return
  87. }