conf.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/rpc"
  11. "go-common/library/queue/databus"
  12. "github.com/BurntSushi/toml"
  13. )
  14. // is
  15. var (
  16. confPath string
  17. client *conf.Client
  18. Conf = &Config{}
  19. )
  20. // Config is
  21. type Config struct {
  22. // Env
  23. Env string
  24. // interface XLog
  25. XLog *log.Config
  26. // databus
  27. ArchiveNotifySub *databus.Config
  28. // http
  29. BM *bm.ServerConfig
  30. // mc
  31. Memcache *memcache.Config
  32. // rpc client
  33. ArchiveRPC *rpc.ClientConfig
  34. // redis
  35. Redis *redis.Config
  36. // Custom 自定义启动参数
  37. Custom *Custom
  38. }
  39. // Custom is
  40. type Custom struct {
  41. Flush bool
  42. }
  43. func init() {
  44. flag.StringVar(&confPath, "conf", "", "config path")
  45. }
  46. // Init init conf
  47. func Init() error {
  48. if confPath != "" {
  49. return local()
  50. }
  51. return remote()
  52. }
  53. func local() (err error) {
  54. _, err = toml.DecodeFile(confPath, &Conf)
  55. return
  56. }
  57. func remote() (err error) {
  58. if client, err = conf.New(); err != nil {
  59. return
  60. }
  61. if err = load(); err != nil {
  62. return
  63. }
  64. client.Watch("app-player-job.toml")
  65. go func() {
  66. for range client.Event() {
  67. log.Info("config reload")
  68. if load() != nil {
  69. log.Error("config reload error (%v)", err)
  70. }
  71. }
  72. }()
  73. return
  74. }
  75. func load() (err error) {
  76. var (
  77. s string
  78. ok bool
  79. tmpConf *Config
  80. )
  81. if s, ok = client.Toml2(); !ok {
  82. return errors.New("load config center error")
  83. }
  84. if _, err = toml.Decode(s, &tmpConf); err != nil {
  85. return errors.New("could not decode config")
  86. }
  87. *Conf = *tmpConf
  88. return
  89. }