Conversation
|
BTW, middleware may change request passed to inner handler. We have no API for that right now but it can be done easy by adding |
|
Looks good to me. Maybe, should be useful to define also middlewares for a specific route , to override application middlewares. |
|
I think adding enabling/disabling middlewares for routes makes a mess. If you need to wrap some specific handlers -- do it explicitly, e.g. Can the way satisfy your requirements? |
|
Yes, indeed. |
|
lgtm. i think we need Application.register_middleware() as well. |
|
Adding middleware to application should be done at configuration stage, current api allows to do that. Also I doubt that we need a way to unregister middleware. I guess to keep API as-is and add |
|
+1 for @asvetlov please to keep it simple :-) |
Applicationacceptsmiddlewareskeyword-only parameter, which should be sequence of middleware factories.Every factory is a coroutine that accepts two parameters:
app(Applicationinstance) andhandler(next handler in middleware chain. The last handler isweb-handlerselected by routing itself. Middleware should return new coroutine by wrappinghandlerparameter. Signature of returned handler should be the same as forweb-handler: accept singlerequestparameter, return response or raise exception. Factory is coroutine, thus it can do extrayield fromcalls on making new handler.After constructing outermost handler by applying middleware chain to web-handler in reversed order
RequestHandlerexecutes that outermost handler as regular web-handler.N.B. Middlewares are executed after routing, so it cannot process route exceptions.
Thoughts?