K8s: use contexthandler in standalone handler chain (#90102)

This commit is contained in:
Charandas
2024-07-08 12:22:10 -07:00
committed by GitHub
parent 3b6a8775bb
commit c210617735
21 changed files with 203 additions and 88 deletions
+11
View File
@@ -139,6 +139,17 @@ func mwFromHandler(handler Handler) Middleware {
}
}
// a convenience function that is provided for users of contexthandler package (standalone apiservers)
// who have an implicit dependency on Macron in context but don't want to take a dependency on
// router additionally
func EmptyMacronMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
m := New()
c := m.createContext(writer, request)
next.ServeHTTP(writer, c.Req) // since c.Req has the newer context attached
})
}
func (m *Macaron) createContext(rw http.ResponseWriter, req *http.Request) *Context {
// NOTE: we have to explicitly copy the middleware chain here to avoid
// passing a shared slice to the *Context, which leads to racy behavior in