allowanddenyip.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package waf
  2. import (
  3. "github.com/gin-gonic/gin"
  4. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  5. "github.com/go-nunu/nunu-layout-advanced/internal/handler"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/service/api/waf"
  7. "net/http"
  8. )
  9. type AllowAndDenyIpHandler struct {
  10. *handler.Handler
  11. allowAndDenyIpService waf.AllowAndDenyIpService
  12. }
  13. func NewAllowAndDenyIpHandler(
  14. handler *handler.Handler,
  15. allowAndDenyIpService waf.AllowAndDenyIpService,
  16. ) *AllowAndDenyIpHandler {
  17. return &AllowAndDenyIpHandler{
  18. Handler: handler,
  19. allowAndDenyIpService: allowAndDenyIpService,
  20. }
  21. }
  22. func (h *AllowAndDenyIpHandler) GetAllowAndDenyIp(ctx *gin.Context) {
  23. var req v1.AllowAndDenyIpRequest
  24. if err := ctx.ShouldBind(&req); err != nil {
  25. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  26. return
  27. }
  28. res, err := h.allowAndDenyIpService.GetAllowAndDenyIp(ctx, int64(req.Id))
  29. if err != nil {
  30. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  31. return
  32. }
  33. v1.HandleSuccess(ctx, res)
  34. }
  35. func (h *AllowAndDenyIpHandler) GetAllowAndDenyIpList(ctx *gin.Context) {
  36. var req v1.AllowAndDenyIpRequest
  37. if err := ctx.ShouldBind(&req); err != nil {
  38. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  39. return
  40. }
  41. res, err := h.allowAndDenyIpService.GetAllowAndDenyIpsAllByHostId(ctx, int64(req.HostId))
  42. if err != nil {
  43. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  44. return
  45. }
  46. v1.HandleSuccess(ctx, res)
  47. }
  48. func (h *AllowAndDenyIpHandler) AddAllowAndDenyIp(ctx *gin.Context) {
  49. var req v1.AllowAndDenyIpRequest
  50. if err := ctx.ShouldBind(&req); err != nil {
  51. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  52. return
  53. }
  54. err := h.allowAndDenyIpService.AddAllowAndDenyIps(ctx,req)
  55. if err != nil {
  56. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  57. return
  58. }
  59. v1.HandleSuccess(ctx, nil)
  60. }
  61. func (h *AllowAndDenyIpHandler) EditAllowAndDenyIp(ctx *gin.Context) {
  62. var req v1.AllowAndDenyIpRequest
  63. if err := ctx.ShouldBind(&req); err != nil {
  64. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  65. return
  66. }
  67. err := h.allowAndDenyIpService.EditAllowAndDenyIps(ctx,req)
  68. if err != nil {
  69. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  70. return
  71. }
  72. v1.HandleSuccess(ctx, nil)
  73. }
  74. func (h *AllowAndDenyIpHandler) DeleteAllowAndDenyIp(ctx *gin.Context) {
  75. var req v1.DelAllowAndDenyIpRequest
  76. if err := ctx.ShouldBind(&req); err != nil {
  77. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  78. return
  79. }
  80. err := h.allowAndDenyIpService.DeleteAllowAndDenyIps(ctx,req)
  81. if err != nil {
  82. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  83. return
  84. }
  85. v1.HandleSuccess(ctx, nil)
  86. }