85 lines
1.8 KiB
Go
85 lines
1.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"dip-ids/internal/storage"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AlertsHandler struct {
|
|
db *storage.DB
|
|
}
|
|
|
|
func NewAlertsHandler(db *storage.DB) *AlertsHandler {
|
|
return &AlertsHandler{db: db}
|
|
}
|
|
|
|
// GetAlerts GET /api/alerts
|
|
func (h *AlertsHandler) GetAlerts(c *gin.Context) {
|
|
filter := storage.AlertFilter{
|
|
Type: c.Query("type"),
|
|
Severity: c.Query("severity"),
|
|
SrcIP: c.Query("src_ip"),
|
|
}
|
|
|
|
if p := c.Query("page"); p != "" {
|
|
if v, err := strconv.Atoi(p); err == nil {
|
|
filter.Page = v
|
|
}
|
|
}
|
|
if ps := c.Query("page_size"); ps != "" {
|
|
if v, err := strconv.Atoi(ps); err == nil {
|
|
filter.PageSize = v
|
|
}
|
|
}
|
|
if from := c.Query("date_from"); from != "" {
|
|
if t, err := time.Parse(time.RFC3339, from); err == nil {
|
|
filter.DateFrom = &t
|
|
}
|
|
}
|
|
if to := c.Query("date_to"); to != "" {
|
|
if t, err := time.Parse(time.RFC3339, to); err == nil {
|
|
filter.DateTo = &t
|
|
}
|
|
}
|
|
|
|
alerts, total, err := h.db.GetAlerts(filter)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": alerts,
|
|
"total": total,
|
|
"page": filter.Page,
|
|
})
|
|
}
|
|
|
|
// DeleteAlert DELETE /api/alerts/:id
|
|
func (h *AlertsHandler) DeleteAlert(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
|
return
|
|
}
|
|
if err := h.db.DeleteAlert(uint(id)); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
}
|
|
|
|
// ClearAlerts DELETE /api/alerts
|
|
func (h *AlertsHandler) ClearAlerts(c *gin.Context) {
|
|
if err := h.db.DeleteAllAlerts(); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
}
|