pkg/web: closure-style middlewares (#51238)
* pkg/web: closure-style middlewares Switches the middleware execution model from web.Handlers in a slice to web.Middleware. Middlewares are temporarily kept in a slice to preserve ordering, but prior to execution they are applied, forming a giant call-stack, giving granular control over the execution flow. * pkg/middleware: adapt to web.Middleware * pkg/middleware/recovery: use c.Req over req c.Req gets updated by future handlers, while req stays static. The current recovery implementation needs this newer information * pkg/web: correct middleware ordering * pkg/webtest: adapt middleware * pkg/web/hack: set w and r onto web.Context By adopting std middlewares, it may happen they invoke next(w,r) without putting their modified w,r into the web.Context, leading old-style handlers to operate on outdated fields. pkg/web now takes care of this * pkg/middleware: selectively use future context * pkg/web: accept closure-style on Use() * webtest: Middleware testing adds a utility function to web/webtest to obtain a http.ResponseWriter, http.Request and http.Handler the same as a middleware that runs would receive * *: cleanup * pkg/web: don't wrap Middleware from Router * pkg/web: require chain to write response * *: remove temp files * webtest: don't require chain write * *: cleanup
This commit is contained in:
+42
-47
@@ -53,28 +53,16 @@ type Handler interface{}
|
||||
//go:linkname hack_wrap github.com/grafana/grafana/pkg/api/response.wrap_handler
|
||||
func hack_wrap(Handler) http.HandlerFunc
|
||||
|
||||
// validateAndWrapHandler makes sure a handler is a callable function, it panics if not.
|
||||
// When the handler is also potential to be any built-in inject.FastInvoker,
|
||||
// it wraps the handler automatically to have some performance gain.
|
||||
func validateAndWrapHandler(h Handler) http.Handler {
|
||||
// wrapHandler turns any supported handler type into a http.Handler by wrapping it accordingly
|
||||
func wrapHandler(h Handler) http.Handler {
|
||||
return hack_wrap(h)
|
||||
}
|
||||
|
||||
// validateAndWrapHandlers preforms validation and wrapping for each input handler.
|
||||
// It accepts an optional wrapper function to perform custom wrapping on handlers.
|
||||
func validateAndWrapHandlers(handlers []Handler) []http.Handler {
|
||||
wrappedHandlers := make([]http.Handler, len(handlers))
|
||||
for i, h := range handlers {
|
||||
wrappedHandlers[i] = validateAndWrapHandler(h)
|
||||
}
|
||||
|
||||
return wrappedHandlers
|
||||
}
|
||||
|
||||
// Macaron represents the top level web application.
|
||||
// Injector methods can be invoked to map services on a global level.
|
||||
type Macaron struct {
|
||||
handlers []http.Handler
|
||||
// handlers []http.Handler
|
||||
mws []Middleware
|
||||
|
||||
urlPrefix string // For suburl support.
|
||||
*Router
|
||||
@@ -119,43 +107,50 @@ func SetURLParams(r *http.Request, vars map[string]string) *http.Request {
|
||||
return r.WithContext(context.WithValue(r.Context(), paramsKey{}, vars))
|
||||
}
|
||||
|
||||
// UseMiddleware is a traditional approach to writing middleware in Go.
|
||||
// A middleware is a function that has a reference to the next handler in the chain
|
||||
// and returns the actual middleware handler, that may do its job and optionally
|
||||
// call next.
|
||||
// Due to how Macaron handles/injects requests and responses we patch the web.Context
|
||||
// to use the new ResponseWriter and http.Request here. The caller may only call
|
||||
// `next.ServeHTTP(rw, req)` to pass a modified response writer and/or a request to the
|
||||
// further middlewares in the chain.
|
||||
func (m *Macaron) UseMiddleware(middleware func(http.Handler) http.Handler) {
|
||||
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
c := FromContext(req.Context())
|
||||
c.Req = req
|
||||
if mrw, ok := rw.(*responseWriter); ok {
|
||||
c.Resp = mrw
|
||||
} else {
|
||||
c.Resp = NewResponseWriter(req.Method, rw)
|
||||
}
|
||||
c.Next()
|
||||
})
|
||||
m.handlers = append(m.handlers, middleware(next))
|
||||
type Middleware = func(next http.Handler) http.Handler
|
||||
|
||||
// UseMiddleware registers the given Middleware
|
||||
func (m *Macaron) UseMiddleware(mw Middleware) {
|
||||
m.mws = append(m.mws, mw)
|
||||
}
|
||||
|
||||
// Use adds a middleware Handler to the stack,
|
||||
// and panics if the handler is not a callable func.
|
||||
// Middleware Handlers are invoked in the order that they are added.
|
||||
func (m *Macaron) Use(handler Handler) {
|
||||
h := validateAndWrapHandler(handler)
|
||||
m.handlers = append(m.handlers, h)
|
||||
// Use registers the provided Handler as a middleware.
|
||||
// The argument may be any supported handler or the Middleware type
|
||||
// Deprecated: use UseMiddleware instead
|
||||
func (m *Macaron) Use(h Handler) {
|
||||
m.mws = append(m.mws, mwFromHandler(h))
|
||||
}
|
||||
|
||||
func mwFromHandler(handler Handler) Middleware {
|
||||
if mw, ok := handler.(Middleware); ok {
|
||||
return mw
|
||||
}
|
||||
|
||||
h := wrapHandler(handler)
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mrw, ok := w.(*responseWriter)
|
||||
if !ok {
|
||||
mrw = NewResponseWriter(r.Method, w).(*responseWriter)
|
||||
}
|
||||
|
||||
h.ServeHTTP(mrw, r)
|
||||
if mrw.Written() {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.Context().Value(macaronContextKey{}).(*Context)
|
||||
next.ServeHTTP(ctx.Resp, ctx.Req)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Macaron) createContext(rw http.ResponseWriter, req *http.Request) *Context {
|
||||
c := &Context{
|
||||
handlers: m.handlers,
|
||||
index: 0,
|
||||
Router: m.Router,
|
||||
Resp: NewResponseWriter(req.Method, rw),
|
||||
logger: log.New("macaron.context"),
|
||||
mws: m.mws,
|
||||
Router: m.Router,
|
||||
Resp: NewResponseWriter(req.Method, rw),
|
||||
logger: log.New("macaron.context"),
|
||||
}
|
||||
|
||||
c.Req = req.WithContext(context.WithValue(req.Context(), macaronContextKey{}, c))
|
||||
|
||||
Reference in New Issue
Block a user