fasthttp-routing icon indicating copy to clipboard operation
fasthttp-routing copied to clipboard

How to get named route path only?

Open acidvertigo opened this issue 8 years ago • 2 comments

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?

acidvertigo avatar Jul 30 '16 14:07 acidvertigo

How are you going to use the route? Does Route.URL() satisfy your need?

qiangxue avatar Aug 01 '16 00:08 qiangxue

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
        }

acidvertigo avatar Aug 03 '16 12:08 acidvertigo