conf.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/conf"
  7. ecode "go-common/library/ecode/tip"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/rpc"
  11. "go-common/library/net/rpc/warden"
  12. xtime "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. var (
  16. confPath string
  17. client *conf.Client
  18. // Conf all conf
  19. Conf = &Config{}
  20. )
  21. // Config is
  22. type Config struct {
  23. HTTPClient *bm.ClientConfig
  24. ArchiveRPC *rpc.ClientConfig
  25. ResourceRPC *rpc.ClientConfig
  26. Ecode *ecode.Config
  27. Log *log.Config
  28. Host *Host
  29. AidGray int64
  30. PadAid int64
  31. PadCid int64
  32. PhoneAid int64
  33. PhoneCid int64
  34. PadHDAid int64
  35. PadHDCid int64
  36. Bnj *Bnj
  37. // mc
  38. Memcache *memcache.Config
  39. // Warden Client
  40. ArchiveClient *warden.ClientConfig
  41. UGCpayClient *warden.ClientConfig
  42. AccountClient *warden.ClientConfig
  43. }
  44. // Bnj is
  45. type Bnj struct {
  46. Tick xtime.Duration
  47. Aids []int64
  48. }
  49. // Host struct
  50. type Host struct {
  51. Playurl string
  52. PlayurlBk string
  53. }
  54. func init() {
  55. flag.StringVar(&confPath, "conf", "", "config path")
  56. }
  57. // Init init conf
  58. func Init() error {
  59. if confPath != "" {
  60. return local()
  61. }
  62. return remote()
  63. }
  64. func local() (err error) {
  65. _, err = toml.DecodeFile(confPath, &Conf)
  66. return
  67. }
  68. func remote() (err error) {
  69. if client, err = conf.New(); err != nil {
  70. return
  71. }
  72. if err = load(); err != nil {
  73. return
  74. }
  75. client.Watch("app-player.toml")
  76. go func() {
  77. for range client.Event() {
  78. log.Info("config reload")
  79. if load() != nil {
  80. log.Error("config reload error (%v)", err)
  81. }
  82. }
  83. }()
  84. return
  85. }
  86. func load() (err error) {
  87. var (
  88. s string
  89. ok bool
  90. tmpConf *Config
  91. )
  92. if s, ok = client.Toml2(); !ok {
  93. return errors.New("load config center error")
  94. }
  95. if _, err = toml.Decode(s, &tmpConf); err != nil {
  96. return errors.New("could not decode config")
  97. }
  98. *Conf = *tmpConf
  99. return
  100. }