Skip to content

Commit

Permalink
exhttp: add middleware utility
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <[email protected]>
  • Loading branch information
sumnerevans committed Oct 7, 2024
1 parent 3b49d3e commit d608488
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions exhttp/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package exhttp

import "net/http"

// Middleware represents a middleware that can be applied to an [http.Handler].
type Middleware func(http.Handler) http.Handler

// ApplyMiddleware applies the provided [Middleware] functions to the given
// router. The middlewares will be applied in the order they are provided.
func ApplyMiddleware(router http.Handler, middlewares ...Middleware) http.Handler {
// Apply middlewares in reverse order because the first middleware provided
// needs to be the outermost one.
for i := len(middlewares) - 1; i >= 0; i-- {
router = middlewares[i](router)
}
return router
}

0 comments on commit d608488

Please sign in to comment.