28 lines
690 B
Go
28 lines
690 B
Go
package server
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type responseRecorder struct {
|
|
http.ResponseWriter
|
|
statusCode int
|
|
}
|
|
|
|
func (r *responseRecorder) WriteHeader(code int) {
|
|
r.statusCode = code
|
|
r.ResponseWriter.WriteHeader(code)
|
|
}
|
|
|
|
// LoggingMiddleware logs each incoming request with method, path, status, and duration.
|
|
func LoggingMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
rec := &responseRecorder{ResponseWriter: w, statusCode: http.StatusOK}
|
|
next.ServeHTTP(rec, r)
|
|
log.Printf("%s %s %d %s", r.Method, r.URL.Path, rec.statusCode, time.Since(start).Round(time.Millisecond))
|
|
})
|
|
}
|