conf.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/orm"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/http/blademaster/middleware/permit"
  10. "go-common/library/net/rpc/warden"
  11. "go-common/library/net/trace"
  12. "github.com/BurntSushi/toml"
  13. )
  14. var (
  15. confPath string
  16. client *conf.Client
  17. // Conf of config
  18. Conf = &Config{}
  19. )
  20. // Config def.
  21. type Config struct {
  22. // base
  23. // http
  24. BM *bm.ServerConfig
  25. // auth
  26. Permit *permit.Config
  27. // db
  28. ORM *orm.Config
  29. // log
  30. Log *log.Config
  31. // tracer
  32. Tracer *trace.Config
  33. // rule
  34. Rule *Rule
  35. // client
  36. HTTPReply *bm.ClientConfig
  37. // Warden Client
  38. ArcClient *warden.ClientConfig
  39. AccClient *warden.ClientConfig
  40. // GameTypes game types.
  41. GameTypes []*types
  42. }
  43. // Rule .
  44. type Rule struct {
  45. MaxCSVRows int
  46. MaxBatchArcLimit int
  47. MaxTreeContests int
  48. }
  49. type types struct {
  50. ID int64
  51. Name string
  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. go func() {
  65. for range client.Event() {
  66. log.Info("config reload")
  67. if load() != nil {
  68. log.Error("config reload error (%v)", err)
  69. }
  70. }
  71. }()
  72. return
  73. }
  74. func load() (err error) {
  75. var (
  76. s string
  77. ok bool
  78. tmpConf *Config
  79. )
  80. if s, ok = client.Value("esports-admin.toml"); !ok {
  81. return errors.New("load config center error")
  82. }
  83. if _, err = toml.Decode(s, &tmpConf); err != nil {
  84. return errors.New("could not decode config")
  85. }
  86. *Conf = *tmpConf
  87. return
  88. }
  89. func init() {
  90. flag.StringVar(&confPath, "conf", "", "default config path")
  91. }
  92. // Init int config
  93. func Init() error {
  94. if confPath != "" {
  95. return local()
  96. }
  97. return remote()
  98. }