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

Method routeHTTP

mux.go:441–485  ·  view source on GitHub ↗

routeHTTP routes a http.Request through the Mux routing tree to serve the matching handler for a particular http method.

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

439// routeHTTP routes a http.Request through the Mux routing tree to serve
440// the matching handler for a particular http method.
441func (mx *Mux) routeHTTP(w http.ResponseWriter, r *http.Request) {
442 // Grab the route context object
443 rctx := r.Context().Value(RouteCtxKey).(*Context)
444
445 // The request routing path
446 routePath := rctx.RoutePath
447 if routePath == "" {
448 if r.URL.RawPath != "" {
449 routePath = r.URL.RawPath
450 } else {
451 routePath = r.URL.Path
452 }
453 if routePath == "" {
454 routePath = "/"
455 }
456 }
457
458 // Check if method is supported by chi
459 if rctx.RouteMethod == "" {
460 rctx.RouteMethod = r.Method
461 }
462 method, ok := methodMap[rctx.RouteMethod]
463 if !ok {
464 mx.MethodNotAllowedHandler().ServeHTTP(w, r)
465 return
466 }
467
468 // Find the route
469 if _, _, h := mx.tree.FindRoute(rctx, method, routePath); h != nil {
470 // Set http.Request path values from our request context
471 for i, key := range rctx.URLParams.Keys {
472 value := rctx.URLParams.Values[i]
473 r.SetPathValue(key, value)
474 }
475 r.Pattern = rctx.RoutePattern()
476
477 h.ServeHTTP(w, r)
478 return
479 }
480 if rctx.methodNotAllowed {
481 mx.MethodNotAllowedHandler(rctx.methodsAllowed...).ServeHTTP(w, r)
482 } else {
483 mx.NotFoundHandler().ServeHTTP(w, r)
484 }
485}
486
487func (mx *Mux) nextRoutePath(rctx *Context) string {
488 routePath := "/"

Callers

nothing calls this directly

Calls 6

NotFoundHandlerMethod · 0.95
ValueMethod · 0.80
FindRouteMethod · 0.80
RoutePatternMethod · 0.80
ServeHTTPMethod · 0.45

Tested by

no test coverage detected