Skip to content

Chi middleware wrapping logic executes the beginning middlewares at the later part #687

Closed

Description

@juddbaguio

Current implementation server middlewares to a handler goes like this:

var handler = func(w http.ResponseWriter, r *http.Request) {
	siw.Handler.YourRouteHandler(w, r)
}

for _, middleware := range siw.HandlerMiddlewares {
	handler = middleware(handler)
}

The snippet above tends to execute beginning middlewares at the later than the last ones.

The looping is supposed to be like this:

var handler = func(w http.ResponseWriter, r *http.Request) {
	siw.Handler.YourRouteHandler(w, r)
}

for i := len(siw.HandlerMiddlewares) - 1; i >= 0; i--  {
	h := siw.HandlerMiddlewares[i]
        if h != nil {
          handler = h(handler)
        }
}

The solution is referenced from Ardan Lab's Service repository on wrapping middlewares

Activity

  1. changed the title [-]Chi middleware looping logic execute the beginning middlewares at the later part[/-] [+]Chi middleware looping logic executes the beginning middlewares at the later part[/+] on Jul 20, 2022
  2. changed the title [-]Chi middleware looping logic executes the beginning middlewares at the later part[/-] [+]Chi middleware wrarping logic executes the beginning middlewares at the later part[/+] on Jul 20, 2022
  3. changed the title [-]Chi middleware wrarping logic executes the beginning middlewares at the later part[/-] [+]Chi middleware wrapping logic executes the beginning middlewares at the later part[/+] on Jul 20, 2022
  4. chanced commented on Aug 5, 2022

    @chanced

    Here is the source from chi:

    // chain builds a http.Handler composed of an inline middleware stack and endpoint
    // handler in the order they are passed.
    func chain(middlewares []func(http.Handler) http.Handler, endpoint http.Handler) http.Handler {
    	// Return ahead of time if there aren't any middlewares for the chain
    	if len(middlewares) == 0 {
    		return endpoint
    	}
    
    
    	// Wrap the end handler with the middleware chain
    	h := middlewares[len(middlewares)-1](endpoint)
    	for i := len(middlewares) - 2; i >= 0; i-- {
    		h = middlewares[i](h)
    	}
    
    
    	return h
    }

    https://github.com/go-chi/chi/blob/9db25e5b381fbd6350583b857e74ecaffe355819/chain.go#L34-L49

  5. jazware commented on Oct 22, 2022

    @jazware
    Contributor

    This should now be supported in the next release (including this PR) using the following options in your config YAML:

    compatibility:
      apply-chi-middleware-first-to-last: true

    This issue can be closed (but I don't have permissions to close it).

  6. mromaszewicz commented on Feb 7, 2026

    @mromaszewicz
    Member

    This has been fixed via the compatibility.apply-chi-middleware-first-to-last configuration option, which reverses the middleware wrapping order so that middlewares execute in the order they are listed. Closing as resolved.

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