Files
2026-08-06 18:26:45 +12:00

25 lines
630 B
Go

package main
import (
"log"
"net/http"
"time"
)
// logMiddleware and recovery in one small wrapper — no need for a whole
// middleware package for two stdlib one-liners.
func logMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rec := recover(); rec != nil {
log.Printf("panic handling %s %s: %v", r.Method, r.URL.Path, rec)
http.Error(w, "internal server error", http.StatusInternalServerError)
}
}()
start := time.Now()
next.ServeHTTP(w, r)
log.Printf("%s %s %s", r.Method, r.URL.Path, time.Since(start))
})
}