fasthttp-routing
fasthttp-routing copied to clipboard
How to get named route path only?
I'm using router.Route("route name") method to get the path of the named route. As I understand it returns a &Route struct but path field is not accessible because not exported.
I like this function because is useful for reverse routing.
Is possible to get the path from named route?
How are you going to use the route? Does Route.URL() satisfy your need?
Not properly for example i'm wrapping every fasthttp request with this code:
func (app *AppContext) DefaultWrapper(r *routing.Router) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
// skip middleware if we are serving static file
if strings.HasPrefix(string(ctx.Path()), "/public") {
r.HandleRequest(ctx)
return
}
// middleware execution
// ...Middleware 1
// ... Middleware2
r.HandleRequest(ctx)
return
}
}
I'm using string prefix to tell to this wrapper to not execute middleware if a static file like an image is requested.
It can be more simple to write this:
route := router.Route("assets")
if route.Path == ctx.Path() {
r.HandleRequest(ctx)
return
}
instead of this
if strings.HasPrefix(string(ctx.Path()), "/public") {
r.HandleRequest(ctx)
return
}