Private
Public Access
1
0
Files
it232Abschied/middleware.go
Sebastian Unterschütz 8950b70378
Some checks failed
Dynamic Branch Deploy / build-and-deploy (push) Has been cancelled
fix README, SYNC, DATENSCHUTZ
2025-11-30 19:33:20 +01:00

50 lines
766 B
Go

package main
import (
"log"
"net/http"
"time"
)
type StatusRecorder struct {
http.ResponseWriter
Status int
}
func (r *StatusRecorder) WriteHeader(status int) {
r.Status = status
r.ResponseWriter.WriteHeader(status)
}
func Logger(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
recorder := &StatusRecorder{
ResponseWriter: w,
Status: http.StatusOK,
}
next(recorder, r)
duration := time.Since(start)
icon := "✅"
if recorder.Status >= 400 {
icon = "⚠️"
}
if recorder.Status >= 500 {
icon = "🔥"
}
log.Printf("%s [%s] %s | %d | %v | %s",
icon,
r.Method,
r.URL.Path,
recorder.Status,
duration,
r.RemoteAddr,
)
}
}