conf.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/rpc"
  10. "go-common/library/queue/databus"
  11. "github.com/BurntSushi/toml"
  12. )
  13. var (
  14. confPath string
  15. Conf = &Config{}
  16. client *conf.Client
  17. )
  18. type Config struct {
  19. // Env
  20. Env string
  21. // interface XLog
  22. Log *log.Config
  23. // databus
  24. Databus *databus.Config
  25. ViewSub *databus.Config
  26. DmSub *databus.Config
  27. ReplySub *databus.Config
  28. FavSub *databus.Config
  29. CoinSub *databus.Config
  30. ShareSub *databus.Config
  31. RankSub *databus.Config
  32. LikeSub *databus.Config
  33. NotifyPub *databus.Config
  34. AccountNotify *databus.Config
  35. // rpc
  36. ArchiveRPCs []*rpc.ClientConfig
  37. // http
  38. BM *bm.ServerConfig
  39. // redis
  40. Redis *redis.Config
  41. }
  42. func init() {
  43. flag.StringVar(&confPath, "conf", "", "default config path")
  44. }
  45. // Init init config.
  46. func Init() (err error) {
  47. if confPath != "" {
  48. _, err = toml.DecodeFile(confPath, &Conf)
  49. return
  50. }
  51. err = remote()
  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 error (%v)", 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. }