package middleware import ( "context" "net/http" "time" ) // Middleware to timeout requests after a given duration func Timeout(next http.Handler, duration time.Duration) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithTimeout(r.Context(), duration) defer cancel() next.ServeHTTP(w, r.WithContext(ctx)) }) }