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

Function walk

tree.go:838–877  ·  view source on GitHub ↗
(r Routes, walkFn WalkFunc, parentRoute string, parentMw ...func(http.Handler) http.Handler)

Source from the content-addressed store, hash-verified

836}
837
838func walk(r Routes, walkFn WalkFunc, parentRoute string, parentMw ...func(http.Handler) http.Handler) error {
839 for _, route := range r.Routes() {
840 mws := slices.Concat(parentMw, r.Middlewares())
841
842 if route.SubRoutes != nil {
843 if handler, ok := route.Handlers["*"]; ok {
844 if chain, ok := handler.(*ChainHandler); ok {
845 mws = append(mws, chain.Middlewares...)
846 }
847 }
848
849 if err := walk(route.SubRoutes, walkFn, parentRoute+route.Pattern, mws...); err != nil {
850 return err
851 }
852 continue
853 }
854
855 for method, handler := range route.Handlers {
856 if method == "*" {
857 // Ignore a "catchAll" method, since we pass down all the specific methods for each route.
858 continue
859 }
860
861 fullRoute := parentRoute + route.Pattern
862 fullRoute = strings.ReplaceAll(fullRoute, "/*/", "/")
863
864 if chain, ok := handler.(*ChainHandler); ok {
865 if err := walkFn(method, fullRoute, chain.Endpoint, append(mws, chain.Middlewares...)...); err != nil {
866 return err
867 }
868 } else {
869 if err := walkFn(method, fullRoute, handler, mws...); err != nil {
870 return err
871 }
872 }
873 }
874 }
875
876 return nil
877}

Callers 1

WalkFunction · 0.85

Calls 2

RoutesMethod · 0.65
MiddlewaresMethod · 0.65

Tested by

no test coverage detected