BuildMux constructs a new mux [http.Handler] instance from the current router configurations.
()
| 59 | |
| 60 | // BuildMux constructs a new mux [http.Handler] instance from the current router configurations. |
| 61 | func (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 | |
| 82 | func (r *Router[T]) loadMux(mux *http.ServeMux, group *RouterGroup[T], parents []*RouterGroup[T]) error { |
| 83 | for _, child := range group.children { |