MCPcopy
hub / github.com/pocketbase/pocketbase / BuildMux

Method BuildMux

tools/router/router.go:61–80  ·  view source on GitHub ↗

BuildMux constructs a new mux [http.Handler] instance from the current router configurations.

()

Source from the content-addressed store, hash-verified

59
60// BuildMux constructs a new mux [http.Handler] instance from the current router configurations.
61func (r *Router[T]) BuildMux() (http.Handler, error) {
62 // Note that some of the default std Go handlers like the [http.NotFoundHandler]
63 // cannot be currently extended and requires defining a custom "catch-all" route
64 // so that the group middlewares could be executed.
65 //
66 // https://github.com/golang/go/issues/65648
67 if !r.HasRoute("", "/") {
68 r.Route("", "/", func(e T) error {
69 return NewNotFoundError("", nil)
70 })
71 }
72
73 mux := http.NewServeMux()
74
75 if err := r.loadMux(mux, r.RouterGroup, nil); err != nil {
76 return nil, err
77 }
78
79 return mux, nil
80}
81
82func (r *Router[T]) loadMux(mux *http.ServeMux, group *RouterGroup[T], parents []*RouterGroup[T]) error {
83 for _, child := range group.children {

Callers 9

TestRouterFunction · 0.80
TestRouterUnbindFunction · 0.80
ServeFunction · 0.80
TestBodyLimitMiddlewareFunction · 0.80
testMethod · 0.80
TestRouterBindsFunction · 0.80

Calls 4

loadMuxMethod · 0.95
HasRouteMethod · 0.80
RouteMethod · 0.80
NewNotFoundErrorFunction · 0.70

Tested by 7

TestRouterFunction · 0.64
TestRouterUnbindFunction · 0.64
TestBodyLimitMiddlewareFunction · 0.64
TestRouterBindsFunction · 0.64