mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
feat(server): integrate ip ban cron and refine updates
- Initialize and schedule IP ban maintenance tasks in server entry point - Perform initial IP ban sync to Redis on startup - Implement optional JSON unmarshalling to handle null `expires_at` in API - Add CIDR overlap validation when updating rule status to active
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -29,9 +30,9 @@ type CreateIPBanRequest struct {
|
||||
|
||||
// UpdateIPBanRequest represents a request to update an IP ban.
|
||||
type UpdateIPBanRequest struct {
|
||||
Reason *string `json:"reason,omitempty"`
|
||||
ExpiresAt *int64 `json:"expires_at,omitempty"`
|
||||
Status *string `json:"status,omitempty"`
|
||||
Reason *string `json:"reason,omitempty"`
|
||||
ExpiresAt optionalInt64 `json:"expires_at,omitempty"`
|
||||
Status *string `json:"status,omitempty"`
|
||||
}
|
||||
|
||||
// IPBanView represents the API response for an IP ban.
|
||||
@@ -47,6 +48,25 @@ type IPBanView struct {
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
}
|
||||
|
||||
type optionalInt64 struct {
|
||||
Value *int64
|
||||
Set bool
|
||||
}
|
||||
|
||||
func (o *optionalInt64) UnmarshalJSON(data []byte) error {
|
||||
o.Set = true
|
||||
if string(data) == "null" {
|
||||
o.Value = nil
|
||||
return nil
|
||||
}
|
||||
var v int64
|
||||
if err := json.Unmarshal(data, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
o.Value = &v
|
||||
return nil
|
||||
}
|
||||
|
||||
func toIPBanView(ban *model.IPBan) IPBanView {
|
||||
return IPBanView{
|
||||
ID: ban.ID,
|
||||
@@ -183,6 +203,7 @@ func (h *IPBanHandler) Get(c *gin.Context) {
|
||||
// @Success 200 {object} IPBanView
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 404 {object} gin.H
|
||||
// @Failure 409 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/ip-bans/{id} [put]
|
||||
func (h *IPBanHandler) Update(c *gin.Context) {
|
||||
@@ -199,15 +220,19 @@ func (h *IPBanHandler) Update(c *gin.Context) {
|
||||
}
|
||||
|
||||
ban, err := h.ipBanService.Update(c.Request.Context(), uint(id), service.UpdateIPBanRequest{
|
||||
Reason: req.Reason,
|
||||
ExpiresAt: req.ExpiresAt,
|
||||
Status: req.Status,
|
||||
Reason: req.Reason,
|
||||
ExpiresAt: req.ExpiresAt.Value,
|
||||
ExpiresAtSet: req.ExpiresAt.Set,
|
||||
Status: req.Status,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrIPBanNotFound) {
|
||||
switch {
|
||||
case errors.Is(err, service.ErrIPBanNotFound):
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "IP ban not found"})
|
||||
} else {
|
||||
case errors.Is(err, service.ErrCIDROverlap):
|
||||
c.JSON(http.StatusConflict, gin.H{"error": err.Error()})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update IP ban", "details": err.Error()})
|
||||
}
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user