Files
mux/stack.go

20 lines
501 B
Go
Raw Normal View History

package mux
import "net/http"
// stack middlewares(http handler) in order they are passed (FIFO)
2025-08-16 19:25:00 +05:30
func stack(endpoint http.Handler, middlewares []func(http.Handler) 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
}