-
Notifications
You must be signed in to change notification settings - Fork 406
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
Feature request: Add support for middlewares in event handler #953
Comments
Hey @BVMiko - thanks for opening this feature request. Perhaps a generic middleware factory decorator for any Python function with an arbitrary number of args, kwargs? That would help make it more explicit so we have one for Lambda and for any function - it also makes it easier to preserve types and function signature (ParamSpec is only supported in later Python versions) It's something I wanted to do for a long time but couldn't prioritise earlier Thanks a lot btw! |
@heitorlessa Here is what would make sense for middleware : def auth_middleware(app, handler):
### auth logic here ###
setattr(app.current_event, "user", user_obj) # or app.current_event.extras["user"] = user_obj - a well defined place through which extra information can be passed on to routes.
return handler() # I haven't seen the code base so I don't know how difficult/easy it would be to implement this` Registering middleware : app.register_middleware(auth_middleware) |
Thanks a lot @karanhere3962 -- that's similar to FastAPI. As this would run for every request, what's the UX you have in mind for a middleware that only runs on a given path? Example:
The details are important so we keep maintenance and extensibility at heart to follow our tenets. Any other ideas would be great. Cc @marcioemiranda who proposed this in the early days |
Both the options that you have given seem great. |
Reviving this feature request now that we finished with all V2 post-release tasks. These are questions I'd like us to answer and get a POC moving. Writing a middleware
Registering a middleware
Built-in middlewares
Suggestion: I think we should revisit the UX for creating/registering middlewares and get inspired about some of these libraries[1] decisions, so that we keep our Tenets in mind and performance. [1] Chalice middlewares, FastAPI Middleware, Django Middleware, Starlite |
Prioritizing it for our next iteration May 8th-May 19th |
reassigning as I'm starting a POC tomorrow, apologies for the delay everyone - security and operations took priority (as it should) |
UPDATE: we had to reprioritize it due to Pydantic v2 launch and the weight it brought in the ecosystem (breaking changes) - @leandrodamascena is on it, but I took on-call responsibilities until the end of next week. Realistically speaking, only resuming a local POC I got by the end of the month. Meanwhile, @rubenfonseca continues to work on OpenAPI + SwaggerUI auto-generation RFC + POC. Apologies for the delays here... |
Thoughts on Middleware (based on @heitorlessa's questions above):
I like this signature - **kwargs enables possible dependency injection from middleware so resolves additional use-cases
Inspiration: Starlette Fast API, Chalice & Starlette implementation is very similar - I like the approach (classic middleware implementation that is easy to reason about). |
Looking deeper: We need to consider how to bring middleware to appsync resolvers - I feel the same DX works. I feel I need to dig deeper into AppSync and Powertools, its very separate from other resolvers (and likely with good cause). |
Sounds great to me @walmsles! On AppSync, @leandrodamascena @mploski are working on a refactor to support Batch event and partial failure, so I'd ignore middleware support in AppSync Event Handler until this work is complete #1303. Let's get Event Handler working POC first then we see how much is transferable as-is so it doesn't become a blocker. |
Thanks @heitorlessa - agree -that was my thinking. |
|
This is now released under 2.24.0 version! |
Is your feature request related to a problem? Please describe.
Event handlers (specifically ApiGatewayResolver) are incompatible with middleware decorators. The
lambda_handler_decorator
is almost exactly what would be needed, except for the function signature.Describe the solution you'd like
I'd like to be able to add middleware when using a router, and for the middleware to have access to the processed event:
Describe alternatives you've considered
Using a regular
lambda_handler_decorator
outside of therouter.get
is executed even when the router isn't called. The decorator wrapper could be stored in individual project code, but since it's so similar to the existing one and would be useful for any router usage, it makes sense to consider it for theaws-lambda-powertools
library.Additional context
The text was updated successfully, but these errors were encountered: