middleware stacking bug fix

This commit is contained in:
2025-08-16 19:25:00 +05:30
parent 5885b42816
commit ec4a0ac231
4 changed files with 19 additions and 23 deletions

View File

@@ -34,19 +34,10 @@ func (m *Mux) Resource(pattern string, fn func(res *Resource), mw ...func(http.H
panic("mux: Resource() requires callback")
}
// Copy root middlewares.
mws := make([]func(http.Handler) http.Handler, len(m.middlewares)+len(mw))
copy(mws, m.middlewares)
// Append inline middlewares.
if len(mw) > 0 {
mws = append(mws, mw...)
}
fn(&Resource{
mux: m.mux,
pattern: pattern,
middlewares: mws,
middlewares: copyMW(m.middlewares, mw),
routes: m.routes,
})
}
@@ -130,7 +121,7 @@ func (res *Resource) handlerFunc(method, pattern string, h http.HandlerFunc) {
}
path := fmt.Sprintf("%s %s", method, pattern)
res.mux.Handle(path, stack(res.middlewares, h))
res.mux.Handle(path, stack(h, res.middlewares))
}
// Use will register middleware(s) on Router stack.