Files
mcr/vendor/github.com/go-chi/chi/v5/middleware/supress_notfound.go
Kyle Isom 75c8b110da Add Nix flake for mcrctl
Vendor dependencies and expose mcrctl binary via nix build.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-25 21:01:28 -07:00

28 lines
852 B
Go

package middleware
import (
"net/http"
"github.com/go-chi/chi/v5"
)
// SupressNotFound will quickly respond with a 404 if the route is not found
// and will not continue to the next middleware handler.
//
// This is handy to put at the top of your middleware stack to avoid unnecessary
// processing of requests that are not going to match any routes anyway. For
// example its super annoying to see a bunch of 404's in your logs from bots.
func SupressNotFound(router *chi.Mux) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
match := rctx.Routes.Match(rctx, r.Method, r.URL.Path)
if !match {
router.NotFoundHandler().ServeHTTP(w, r)
return
}
next.ServeHTTP(w, r)
})
}
}