38 lines
871 B
Go
38 lines
871 B
Go
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||
|
|
|
||
|
|
package middleware
|
||
|
|
|
||
|
|
import (
|
||
|
|
"log/slog"
|
||
|
|
"net/http"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
// responseWriter wraps http.ResponseWriter to capture the status code.
|
||
|
|
type responseWriter struct {
|
||
|
|
http.ResponseWriter
|
||
|
|
statusCode int
|
||
|
|
}
|
||
|
|
|
||
|
|
func (rw *responseWriter) WriteHeader(code int) {
|
||
|
|
rw.statusCode = code
|
||
|
|
rw.ResponseWriter.WriteHeader(code)
|
||
|
|
}
|
||
|
|
|
||
|
|
// RequestLogger logs each HTTP request with method, path, status, and duration.
|
||
|
|
func RequestLogger(next http.Handler) http.Handler {
|
||
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
start := time.Now()
|
||
|
|
rw := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK}
|
||
|
|
|
||
|
|
next.ServeHTTP(rw, r)
|
||
|
|
|
||
|
|
slog.Debug("request",
|
||
|
|
"method", r.Method,
|
||
|
|
"path", r.URL.Path,
|
||
|
|
"status", rw.statusCode,
|
||
|
|
"duration", time.Since(start),
|
||
|
|
"ip", RealIPFromContext(r.Context()),
|
||
|
|
)
|
||
|
|
})
|
||
|
|
}
|