791cd4d6af
Build / build (push) Successful in 12m20s
Rebrand the project as Tinyforge to reflect its evolution from a Docker container watcher into a self-hosted mini CI/deployment platform. Rename covers: Go module path, Docker labels, DB/config filenames, JWT issuer, Dockerfile binary, docker-compose, CI workflows, frontend i18n, README with static sites docs, and all code comments.
77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
package api
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/alexei/tinyforge/internal/store"
|
|
)
|
|
|
|
// listEventLog handles GET /api/events/log.
|
|
// Supports query parameters: severity, source, since, until, limit, offset.
|
|
func (s *Server) listEventLog(w http.ResponseWriter, r *http.Request) {
|
|
q := r.URL.Query()
|
|
|
|
limit, _ := strconv.Atoi(q.Get("limit"))
|
|
offset, _ := strconv.Atoi(q.Get("offset"))
|
|
|
|
filter := store.EventLogFilter{
|
|
Severity: q.Get("severity"),
|
|
Source: q.Get("source"),
|
|
Since: q.Get("since"),
|
|
Until: q.Get("until"),
|
|
Limit: limit,
|
|
Offset: offset,
|
|
}
|
|
|
|
events, err := s.store.ListEvents(filter)
|
|
if err != nil {
|
|
slog.Error("failed to list events", "error", err)
|
|
respondError(w, http.StatusInternalServerError, "failed to list events")
|
|
return
|
|
}
|
|
|
|
respondJSON(w, http.StatusOK, events)
|
|
}
|
|
|
|
// getEventLogStats handles GET /api/events/log/stats.
|
|
func (s *Server) getEventLogStats(w http.ResponseWriter, r *http.Request) {
|
|
stats, err := s.store.GetEventStats()
|
|
if err != nil {
|
|
slog.Error("failed to get event stats", "error", err)
|
|
respondError(w, http.StatusInternalServerError, "failed to get event stats")
|
|
return
|
|
}
|
|
|
|
respondJSON(w, http.StatusOK, stats)
|
|
}
|
|
|
|
// deleteEvent handles DELETE /api/events/log/{id}.
|
|
func (s *Server) deleteEvent(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
if err != nil {
|
|
respondError(w, http.StatusBadRequest, "invalid event ID")
|
|
return
|
|
}
|
|
if err := s.store.DeleteEvent(id); err != nil {
|
|
slog.Error("failed to delete event", "id", id, "error", err)
|
|
respondError(w, http.StatusInternalServerError, "internal server error")
|
|
return
|
|
}
|
|
respondJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
|
}
|
|
|
|
// clearEvents handles DELETE /api/events/log.
|
|
func (s *Server) clearEvents(w http.ResponseWriter, r *http.Request) {
|
|
cleared, err := s.store.ClearAllEvents()
|
|
if err != nil {
|
|
slog.Error("failed to clear events", "error", err)
|
|
respondError(w, http.StatusInternalServerError, "internal server error")
|
|
return
|
|
}
|
|
respondJSON(w, http.StatusOK, map[string]any{"status": "cleared", "count": cleared})
|
|
}
|