conf.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/orm"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/permit"
  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. // Config .
  17. type Config struct {
  18. Cfg *cfg
  19. App *bm.App
  20. ORM *orm.Config
  21. Log *log.Config
  22. Tracer *trace.Config
  23. Memcache *memcache.Config
  24. HTTPServer *bm.ServerConfig
  25. HTTPClient *bm.ClientConfig
  26. DsbClient *bm.ClientConfig
  27. UnameTicker xtime.Duration
  28. WardenServer *warden.ServerConfig
  29. Permit *permit.Config2
  30. }
  31. type cfg struct {
  32. RankGroupMaxPs int
  33. }
  34. var (
  35. confPath string
  36. client *conf.Client
  37. // Conf config
  38. Conf = &Config{}
  39. )
  40. func init() {
  41. flag.StringVar(&confPath, "conf", "", "config path")
  42. }
  43. // Init .
  44. func Init() (err error) {
  45. if confPath != "" {
  46. return local()
  47. }
  48. return remote()
  49. }
  50. func local() (err error) {
  51. _, err = toml.DecodeFile(confPath, &Conf)
  52. return
  53. }
  54. func remote() (err error) {
  55. if client, err = conf.New(); err != nil {
  56. return
  57. }
  58. if err = load(); err != nil {
  59. return
  60. }
  61. go func() {
  62. for range client.Event() {
  63. log.Info("config reload")
  64. if load() != nil {
  65. log.Error("config reload err")
  66. }
  67. }
  68. }()
  69. return
  70. }
  71. func load() (err error) {
  72. var (
  73. s string
  74. ok bool
  75. tmpConf *Config
  76. )
  77. if s, ok = client.Toml2(); !ok {
  78. return errors.New("load config center error")
  79. }
  80. if _, err = toml.Decode(s, &tmpConf); err != nil {
  81. return errors.New("could not decode config")
  82. }
  83. *Conf = *tmpConf
  84. return
  85. }