package surf import "net/http" // RouteInfo is a read-only snapshot of one registered route after Assemble // or BuildRouter. type RouteInfo struct { Method string Pattern string PluginID string Middleware []string Raw bool } // AdminPrefix reports the cabana admin mount path set by BuildRouter. False // means no plugin registers an admin controller, so no admin routes exist. func (r *Router) AdminPrefix() (string, bool) { if r == nil || !r.adminMounted { return "", false } return r.adminPrefix, true } // Routes returns a defensive copy of every registered route. func (r *Router) Routes() []RouteInfo { if r == nil { return nil } out := make([]RouteInfo, len(r.routes)) for i, rt := range r.routes { mw := append([]string{}, rt.middleware...) out[i] = RouteInfo{ Method: rt.method, Pattern: rt.path, PluginID: rt.pluginID, Middleware: mw, Raw: rt.raw, } } return out } // Handler returns the compiled middleware chain of the route registered for // method and pattern (as listed by Routes), for an in-process dispatch that // must run the same constraints, guards, throttles, body limit, locale and // recovery as an HTTP request. The caller sets the path values and Pattern // on its request before serving, as the mux would. False when no such route // exists or BuildRouter has not compiled the routes yet. func (r *Router) Handler(method, pattern string) (http.Handler, bool) { if r == nil || r.handlers == nil { return nil, false } h, ok := r.handlers[method+" "+pattern] return h, ok && h != nil }