-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Middleware, second edition #209
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 :-) |
Application
acceptsmiddlewares
keyword-only parameter, which should be sequence of middleware factories.Every factory is a coroutine that accepts two parameters:
app
(Application
instance) andhandler
(next handler in middleware chain. The last handler isweb-handler
selected by routing itself. Middleware should return new coroutine by wrappinghandler
parameter. Signature of returned handler should be the same as forweb-handler
: accept singlerequest
parameter, return response or raise exception. Factory is coroutine, thus it can do extrayield from
calls on making new handler.After constructing outermost handler by applying middleware chain to web-handler in reversed order
RequestHandler
executes that outermost handler as regular web-handler.N.B. Middlewares are executed after routing, so it cannot process route exceptions.
Thoughts?