MCPcopy
hub / github.com/go-chi/chi / SupressNotFound

Function SupressNotFound

middleware/supress_notfound.go:15–27  ·  middleware/supress_notfound.go::SupressNotFound

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 a

(router *chi.Mux)

Source from the content-addressed store, hash-verified

13// processing of requests that are not going to match any routes anyway. For
14// example its super annoying to see a bunch of 404's in your logs from bots.
15func SupressNotFound(router *chi.Mux) func(next http.Handler) http.Handler {
16 return func(next http.Handler) http.Handler {
17 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
18 rctx := chi.RouteContext(r.Context())
19 match := rctx.Routes.Match(rctx, r.Method, r.URL.Path)
20 if !match {
21 router.NotFoundHandler().ServeHTTP(w, r)
22 return
23 }
24 next.ServeHTTP(w, r)
25 })
26 }
27}

Callers

nothing calls this directly

Calls 4

HandlerFuncMethod · 0.80
NotFoundHandlerMethod · 0.80
MatchMethod · 0.65
ServeHTTPMethod · 0.45

Tested by

no test coverage detected