rule.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package http
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "go-common/app/service/main/antispam/conf"
  7. "go-common/app/service/main/antispam/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. )
  12. // GetRule .
  13. func GetRule(c *bm.Context) {
  14. params := c.Request.Form
  15. _, area, err := getAdminIDAndArea(params)
  16. if err != nil {
  17. log.Error("%v", err)
  18. errResp(c, ecode.RequestErr, err)
  19. return
  20. }
  21. if params.Get(ProtocolRuleLimitType) == "" ||
  22. params.Get(ProtocolRuleLimitScope) == "" {
  23. err = errors.New("either limit_type or limit_scope is nil")
  24. log.Error("%v", err)
  25. errResp(c, ecode.RequestErr, err)
  26. return
  27. }
  28. rule, err := Svr.GetRuleByAreaAndLimitTypeAndScope(c,
  29. area, params.Get(ProtocolRuleLimitType), params.Get(ProtocolRuleLimitScope))
  30. if err != nil {
  31. errResp(c, ecode.ServerErr, err)
  32. return
  33. }
  34. c.JSON(rule, nil)
  35. }
  36. // GetRules .
  37. func GetRules(c *bm.Context) {
  38. params := c.Request.Form
  39. _, area, err := getAdminIDAndArea(params)
  40. if err != nil {
  41. log.Error("%v", err)
  42. errResp(c, ecode.RequestErr, err)
  43. return
  44. }
  45. rules, err := Svr.GetRuleByArea(c, area)
  46. if err != nil {
  47. errResp(c, ecode.ServerErr, err)
  48. return
  49. }
  50. c.JSON(rules, nil)
  51. }
  52. // AddRule .
  53. func AddRule(c *bm.Context) {
  54. params := c.Request.Form
  55. _, area, err := getAdminIDAndArea(params)
  56. if err != nil {
  57. log.Error("%v", err)
  58. errResp(c, ecode.RequestErr, err)
  59. return
  60. }
  61. typ := params.Get(ProtocolRuleLimitType)
  62. if typ != model.LimitTypeDefault && typ != model.LimitTypeRestrict {
  63. err = fmt.Errorf("illegal limit type %q", typ)
  64. log.Error("%v", err)
  65. errResp(c, ecode.RequestErr, err)
  66. return
  67. }
  68. scope := params.Get(ProtocolRuleLimitScope)
  69. if scope != model.LimitScopeLocal && scope != model.LimitScopeGlobal {
  70. err = fmt.Errorf("illegal limit scope %q", scope)
  71. log.Error("%v", err)
  72. errResp(c, ecode.RequestErr, err)
  73. return
  74. }
  75. r := &model.Rule{
  76. Area: area,
  77. LimitType: typ,
  78. LimitScope: scope,
  79. }
  80. allowedCounts := params.Get(ProtocolRuleAllowedCounts)
  81. if r.AllowedCounts, err = strconv.ParseInt(allowedCounts, 10, 64); err != nil {
  82. log.Error("%v", err)
  83. errResp(c, ecode.RequestErr, err)
  84. return
  85. }
  86. if r.DurationSec, err = strconv.ParseInt(params.Get(ProtocolRuleDuration), 10, 64); err != nil {
  87. log.Error("%v", err)
  88. errResp(c, ecode.RequestErr, err)
  89. return
  90. }
  91. if r.DurationSec <= 0 || r.AllowedCounts <= 0 {
  92. err = fmt.Errorf("both durationSec(%d) and allowedCounts(%d) must be greater than 0", r.AllowedCounts, r.DurationSec)
  93. log.Error("%v", err)
  94. errResp(c, ecode.RequestErr, err)
  95. return
  96. }
  97. if r.DurationSec > conf.Conf.MaxDurationSec || r.AllowedCounts > conf.Conf.MaxAllowedCounts {
  98. err = fmt.Errorf("either durationSec(%d) or allowedCounts(%d) exceed maxDurationSec(%d), maxAllowedCounts(%d)",
  99. r.AllowedCounts, r.DurationSec, conf.Conf.MaxDurationSec, conf.Conf.MaxAllowedCounts)
  100. log.Error("%v", err)
  101. errResp(c, ecode.RequestErr, err)
  102. return
  103. }
  104. r, err = Svr.UpsertRule(c, r)
  105. if err != nil {
  106. errResp(c, ecode.ServerErr, err)
  107. return
  108. }
  109. c.JSON(r, nil)
  110. }