conf.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/app/interface/main/upload/http/antispam"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/orm"
  9. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/auth"
  13. "go-common/library/net/http/blademaster/middleware/verify"
  14. xtime "go-common/library/time"
  15. "github.com/BurntSushi/toml"
  16. )
  17. var (
  18. confPath string
  19. client *conf.Client
  20. // Conf conf
  21. Conf = &Config{}
  22. )
  23. // Config config
  24. type Config struct {
  25. XLog *log.Config
  26. // BM
  27. BM *bm.ServerConfig
  28. // ecode
  29. Ecode *ecode.Config
  30. // orm
  31. ORM *orm.Config
  32. // bfs
  33. Bfs *Bfs
  34. BfsBucket *BfsBucket
  35. Auths []*Auth
  36. // Antispam redis
  37. Antispam *antispam.Config
  38. // AuthN
  39. AuthInter *auth.Config
  40. // VerifyN
  41. Verify *verify.Config
  42. // AuthN outside
  43. AuthOut *auth.Config
  44. }
  45. // Bfs .
  46. type Bfs struct {
  47. BfsURL string
  48. WaterMarkURL string
  49. ImageGenURL string
  50. TimeOut xtime.Duration
  51. WmTimeOut xtime.Duration
  52. ImageGenTimeOut xtime.Duration
  53. }
  54. // BfsBucket .
  55. type BfsBucket struct {
  56. Bucket string
  57. Key string
  58. Sercet string
  59. }
  60. // Auth .
  61. type Auth struct {
  62. AppKey string
  63. AppSercet string
  64. BfsBucket *BfsBucket
  65. }
  66. // Antispam .
  67. type Antispam struct {
  68. Redis *redis.Config
  69. Switch bool
  70. Second int
  71. N int
  72. Hour int
  73. M int
  74. }
  75. func init() {
  76. flag.StringVar(&confPath, "conf", "", "config path")
  77. }
  78. // Init init conf
  79. func Init() (err error) {
  80. if confPath != "" {
  81. _, err = toml.DecodeFile(confPath, &Conf)
  82. return
  83. }
  84. err = remote()
  85. return
  86. }
  87. func remote() (err error) {
  88. if client, err = conf.New(); err != nil {
  89. return
  90. }
  91. if err = load(); err != nil {
  92. return
  93. }
  94. go func() {
  95. for range client.Event() {
  96. log.Info("config reload")
  97. if load() != nil {
  98. log.Error("config reload error (%v)", err)
  99. }
  100. }
  101. }()
  102. return
  103. }
  104. func load() (err error) {
  105. var (
  106. s string
  107. ok bool
  108. tmpConf *Config
  109. )
  110. if s, ok = client.Toml2(); !ok {
  111. return errors.New("load config center error")
  112. }
  113. if _, err = toml.Decode(s, &tmpConf); err != nil {
  114. return errors.New("could not decode config")
  115. }
  116. *Conf = *tmpConf
  117. return
  118. }