Implementiere umfassendes Logging-System für WOL-Ereignisse

- Neue Log-Tabelle in der Datenbank
- Automatisches Logging bei WOL-Button-Klicks
- Dedizierte Logs-Seite mit Bootstrap-Design
- Tooltips mit letzten 5 WOL-Ereignissen pro PC
- API-Endpunkte für Log-Verwaltung
- Einheitliches Design zwischen Haupt- und Logs-Seite
- Vollständige Dokumentation des Logging-Systems
This commit is contained in:
2025-08-22 07:16:14 +02:00
parent 2f4920cc27
commit b6888ca5da
10 changed files with 723 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
package handlers
import (
"log"
"medi-wol/internal/database"
"medi-wol/internal/models"
"medi-wol/internal/ping"
@@ -34,6 +35,13 @@ func (h *PCHandler) Index(c *gin.Context) {
})
}
// Logs zeigt die Logs-Seite an
func (h *PCHandler) Logs(c *gin.Context) {
c.HTML(http.StatusOK, "logs.html", gin.H{
"title": "Medi-WOL - Logs",
})
}
// GetAllPCs gibt alle gespeicherten PCs zurück
func (h *PCHandler) GetAllPCs(c *gin.Context) {
pcs, err := h.db.GetAllPCs()
@@ -194,6 +202,13 @@ func (h *PCHandler) WakePC(c *gin.Context) {
return
}
// Log-Eintrag erstellen
_, logErr := h.db.CreateLog(pc.ID, pc.Name, pc.MAC, "button")
if logErr != nil {
// Log-Fehler nicht an den Benutzer weitergeben, nur intern protokollieren
log.Printf("Fehler beim Erstellen des Log-Eintrags: %v", logErr)
}
c.JSON(http.StatusOK, models.PCResponse{
Success: true,
Message: "Wake-on-LAN Paket erfolgreich gesendet an " + pc.Name,
@@ -229,3 +244,74 @@ func (h *PCHandler) GetPCStatus(c *gin.Context) {
"status": statusList,
})
}
// GetAllLogs gibt alle Log-Einträge zurück
func (h *PCHandler) GetAllLogs(c *gin.Context) {
logs, err := h.db.GetAllLogs()
if err != nil {
c.JSON(http.StatusInternalServerError, models.LogResponse{
Success: false,
Message: "Fehler beim Laden der Logs: " + err.Error(),
})
return
}
c.JSON(http.StatusOK, models.LogResponse{
Success: true,
Logs: logs,
})
}
// GetLogsByPCID gibt alle Log-Einträge für einen bestimmten PC zurück
func (h *PCHandler) GetLogsByPCID(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
c.JSON(http.StatusBadRequest, models.LogResponse{
Success: false,
Message: "Ungültige PC-ID",
})
return
}
logs, err := h.db.GetLogsByPCID(id)
if err != nil {
c.JSON(http.StatusInternalServerError, models.LogResponse{
Success: false,
Message: "Fehler beim Laden der Logs: " + err.Error(),
})
return
}
c.JSON(http.StatusOK, models.LogResponse{
Success: true,
Logs: logs,
})
}
// GetRecentLogsByPCID gibt die letzten 5 Log-Einträge für einen bestimmten PC zurück
func (h *PCHandler) GetRecentLogsByPCID(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
c.JSON(http.StatusBadRequest, models.LogResponse{
Success: false,
Message: "Ungültige PC-ID",
})
return
}
logs, err := h.db.GetRecentLogsByPCID(id)
if err != nil {
c.JSON(http.StatusInternalServerError, models.LogResponse{
Success: false,
Message: "Fehler beim Laden der Logs: " + err.Error(),
})
return
}
c.JSON(http.StatusOK, models.LogResponse{
Success: true,
Logs: logs,
})
}