mirror of
https://github.com/swissmakers/fail2ban-ui.git
synced 2026-04-17 05:53:15 +02:00
Improve ban events search search through all db-entries and also implement pagination to load more events
This commit is contained in:
@@ -365,13 +365,25 @@ func UnbanNotificationHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Unban notification processed successfully"})
|
||||
}
|
||||
|
||||
// ListBanEventsHandler returns stored ban events from the internal database.
|
||||
// ListBanEventsHandler returns stored ban events from the internal database with optional search and pagination.
|
||||
// Query params: serverId, limit (default 50, max 50), offset (default 0, max 1000), since, search, country.
|
||||
// When offset=0, response includes total (matching count). Response includes hasMore when more results exist.
|
||||
func ListBanEventsHandler(c *gin.Context) {
|
||||
serverID := c.Query("serverId")
|
||||
limit := 100
|
||||
if limitStr := c.DefaultQuery("limit", "100"); limitStr != "" {
|
||||
limit := storage.MaxBanEventsLimit
|
||||
if limitStr := c.DefaultQuery("limit", strconv.Itoa(storage.MaxBanEventsLimit)); limitStr != "" {
|
||||
if parsed, err := strconv.Atoi(limitStr); err == nil && parsed > 0 {
|
||||
limit = parsed
|
||||
if parsed <= storage.MaxBanEventsLimit {
|
||||
limit = parsed
|
||||
}
|
||||
}
|
||||
}
|
||||
offset := 0
|
||||
if offsetStr := c.DefaultQuery("offset", "0"); offsetStr != "" {
|
||||
if parsed, err := strconv.Atoi(offsetStr); err == nil && parsed >= 0 {
|
||||
if parsed <= storage.MaxBanEventsOffset {
|
||||
offset = parsed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -381,13 +393,24 @@ func ListBanEventsHandler(c *gin.Context) {
|
||||
since = parsed
|
||||
}
|
||||
}
|
||||
search := strings.TrimSpace(c.Query("search"))
|
||||
country := strings.TrimSpace(c.Query("country"))
|
||||
|
||||
events, err := storage.ListBanEvents(c.Request.Context(), serverID, limit, since)
|
||||
ctx := c.Request.Context()
|
||||
events, err := storage.ListBanEventsFiltered(ctx, serverID, limit, offset, since, search, country)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"events": events})
|
||||
|
||||
resp := gin.H{"events": events, "hasMore": len(events) == limit}
|
||||
if offset == 0 {
|
||||
total, errCount := storage.CountBanEventsFiltered(ctx, serverID, since, search, country)
|
||||
if errCount == nil {
|
||||
resp["total"] = total
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
// BanStatisticsHandler returns aggregated ban counts per server.
|
||||
|
||||
Reference in New Issue
Block a user