qiangxue / fasthttp-routing

A fast and powerful routing package for fasthttp servers
BSD 3-Clause "New" or "Revised" License
345 stars 50 forks source link

How to get named route path only? #2

Open acidvertigo opened 8 years ago

acidvertigo commented 8 years ago

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?

qiangxue commented 8 years ago

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

acidvertigo commented 8 years ago

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
        }