Skip to content

RoutePattern at middleware level? #270

Closed

Description

@jutkko

Hi I am migrating from an echo router codebase to chi. One of the things we use was the parameterised path variable echo provides. I can see a similar thing in chi, chi.RouteContext(r.Context()).RoutePattern(), but this function seems to be returning an empty string when called in middlewares. Is there way out?

Am I missing something obvious? Thanks.

Activity

  1. pkieltyka commented on Oct 10, 2017

    @pkieltyka
    Member

    @jutkko the RoutePattern() will only return a value once its determined the final route. You can either access it in the endpoint handler, or after a request responds in the middleware. See the description in https://github.com/go-chi/chi/blob/master/context.go#L80-L93 and example there:

    func Instrument(next http.Handler) http.Handler {
      return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        next.ServeHTTP(w, r)
        routePattern := chi.RouteContext(r.Context()).RoutePattern() // <---<< after
        measure(w, r, routePattern)
     })
    }

    please let me know if this works for you guys / other feedback

  2. jutkko commented on Oct 10, 2017

    @jutkko
    Author

    Yes that will do the job. But there are some operations (tracing) we want to do before calling next and also want to have the RoutePattern()...

    Thanks for the quick response though!

  3. pkieltyka commented on Oct 10, 2017

    @pkieltyka
    Member

    @jutkko cool - if thats the case, you could use a technique similar in https://github.com/go-chi/chi/blob/master/middleware/get_head.go to pre-process the route and get the RoutePattern() from that context.

  4. rafaeljesus commented on Aug 23, 2018

    @rafaeljesus

    Hi @pkieltyka I didn't understand well the pre-process tip you gave? I tried here having 2 middleware:

    r.Use(NewRoutePattern)
    r.Use(NewTraceRequest)
    
    // route goes here

    NewRoutePattern

    		rctx := chi.RouteContext(r.Context())
    		routePath := rctx.RoutePath
    		if routePath == "" {
    			if r.URL.RawPath != "" {
    				routePath = r.URL.RawPath
    			} else {
    				routePath = r.URL.Path
    			}
    		}
    
    		tctx := chi.NewRouteContext()
    
    		if rctx.Routes.Match(tctx, r.Method, routePath) {
    			rctx.RouteMethod = r.Method
    			rctx.RoutePath = routePath
    			next.ServeHTTP(w, r)
    			return
    		}

    NewTraceRequest

    routePattern := chi.RouteContext(r.Context()).RoutePattern()
    // routePattern does not work

    Any thoughts? Thanks

  5. rliebz commented on Apr 2, 2019

    @rliebz

    For anyone who still needs this, the following seems to work:

    func getRoutePattern(r *http.Request) string {
    	rctx := chi.RouteContext(r.Context())
    	if pattern := rctx.RoutePattern(); pattern != "" {
    		// Pattern is already available
    		return pattern
    	}
    
    	routePath := r.URL.Path
    	if r.URL.RawPath != "" {
    		routePath = r.URL.RawPath
    	}
    
    	tctx := chi.NewRouteContext()
    	if !rctx.Routes.Match(tctx, r.Method, routePath) {
    		// No matching pattern, so just return the request path.
    		// Depending on your use case, it might make sense to
    		// return an empty string or error here instead
    		return routePath
    	}
    
    	// tctx has the updated pattern, since Match mutates it
    	return tctx.RoutePattern()
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址