conf.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. xlog "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/rpc"
  11. "go-common/library/net/trace"
  12. "go-common/library/queue/databus"
  13. xtime "go-common/library/time"
  14. "github.com/BurntSushi/toml"
  15. )
  16. // Config .
  17. type Config struct {
  18. // Env .
  19. Env string
  20. // App .
  21. App *bm.App
  22. // Log is go-common log.
  23. Log *xlog.Config
  24. // Tracer .
  25. Tracer *trace.Config
  26. // PlaylistStatSub databus.
  27. PlaylistViewSub *databus.Config
  28. PlaylistFavSub *databus.Config
  29. PlaylistReplySub *databus.Config
  30. PlaylistShareSub *databus.Config
  31. // HTTPServer .
  32. HTTPServer *bm.ServerConfig
  33. // HTTPClient .
  34. HTTPClient *bm.ClientConfig
  35. // RPC .
  36. PlaylistRPC *rpc.ClientConfig
  37. // Mysql .
  38. Mysql *sql.Config
  39. // Redis .
  40. Redis *redis.Config
  41. // Job params .
  42. Job *job
  43. }
  44. type job struct {
  45. InterceptOn bool
  46. ViewCacheTTL xtime.Duration
  47. UpdateDbInterval xtime.Duration
  48. }
  49. var (
  50. confPath string
  51. client *conf.Client
  52. // Conf config.
  53. Conf = &Config{}
  54. )
  55. func init() {
  56. flag.StringVar(&confPath, "conf", "", "config path")
  57. }
  58. // Init .
  59. func Init() (err error) {
  60. if confPath != "" {
  61. return local()
  62. }
  63. return remote()
  64. }
  65. func local() (err error) {
  66. _, err = toml.DecodeFile(confPath, &Conf)
  67. return
  68. }
  69. func remote() (err error) {
  70. if client, err = conf.New(); err != nil {
  71. return
  72. }
  73. if err = load(); err != nil {
  74. return
  75. }
  76. go func() {
  77. for range client.Event() {
  78. xlog.Info("config reload")
  79. if load() != nil {
  80. xlog.Error("config reload 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. }