conf.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. ecode "go-common/library/ecode/tip"
  9. "go-common/library/log"
  10. "go-common/library/log/infoc"
  11. bm "go-common/library/net/http/blademaster"
  12. antispam "go-common/library/net/http/blademaster/middleware/antispam"
  13. "go-common/library/net/http/blademaster/middleware/auth"
  14. "go-common/library/net/http/blademaster/middleware/verify"
  15. "go-common/library/net/rpc/warden"
  16. "go-common/library/net/trace"
  17. "github.com/BurntSushi/toml"
  18. )
  19. var (
  20. confPath string
  21. client *conf.Client
  22. // Conf config
  23. Conf = &Config{}
  24. )
  25. // Config .
  26. type Config struct {
  27. Log *log.Config
  28. BM *bm.ServerConfig
  29. Verify *verify.Config
  30. Auth *auth.Config
  31. Tracer *trace.Config
  32. Redis *redis.Config
  33. MySQL *sql.Config
  34. OnlineMySQL *sql.Config
  35. Ecode *ecode.Config
  36. GRPCClient map[string]*GRPCConf
  37. AntiSpam map[string]*antispam.Config
  38. BulletConfig BulletConfig
  39. Infoc *infoc.Config
  40. }
  41. // BulletConfig 弹幕的一些配置项
  42. type BulletConfig struct {
  43. CloseWrite bool
  44. CloseRead bool
  45. }
  46. // GRPCConf .
  47. type GRPCConf struct {
  48. WardenConf *warden.ClientConfig
  49. Addr string
  50. }
  51. func init() {
  52. flag.StringVar(&confPath, "conf", "", "default config path")
  53. }
  54. // Init init conf
  55. func Init() (err error) {
  56. if confPath != "" {
  57. err = local()
  58. } else {
  59. err = remote()
  60. }
  61. if Conf.Redis != nil {
  62. for _, anti := range Conf.AntiSpam {
  63. anti.Redis = Conf.Redis
  64. }
  65. }
  66. return
  67. }
  68. func local() (err error) {
  69. _, err = toml.DecodeFile(confPath, &Conf)
  70. return
  71. }
  72. func remote() (err error) {
  73. if client, err = conf.New(); err != nil {
  74. return
  75. }
  76. if err = load(); err != nil {
  77. return
  78. }
  79. go func() {
  80. for range client.Event() {
  81. log.Info("config reload")
  82. if load() != nil {
  83. log.Error("config reload error (%v)", err)
  84. }
  85. }
  86. }()
  87. return
  88. }
  89. func load() (err error) {
  90. var (
  91. s string
  92. ok bool
  93. tmpConf *Config
  94. )
  95. if s, ok = client.Toml2(); !ok {
  96. return errors.New("load config center error")
  97. }
  98. if _, err = toml.Decode(s, &tmpConf); err != nil {
  99. return errors.New("could not decode config")
  100. }
  101. *Conf = *tmpConf
  102. return
  103. }