Adding support for route based middleware #1781
-
In other frameworks (i.e. Laravel, Express) there is an way of adding middleware to a route easily which has access to the request object and is able to send response. When I tried this (FastApi -> Starlette) it was missing. These middlewares do make the job a lot easier and in some cases I think it's essential to have. The route middlewares behaves more like an endpoint than a middleware. async def check_admin(request: Request, next):
if request.headers['Authorization'] == 'I am admin':
request.isAdmin = True
request.perms = ['a', 'b', 'c']
return await next()
else:
return JSONResponse(['error':'Unauthorized access'])
async def check_perm(request: Request, next):
if request.isAdmin and 'c' in request.perms:
return await next()
else:
return JSONResponse(['error':'Unauthorized access'])
routes = [
Route("/admin", endpoint = admin_page, middlewares = check_admin),
Route(
"/admin/secret", endpoint = admin_secret,
middlewares = [check_admin, check_perm] # Middlewares will be ran in the same order
)
] How's the idea? And can someone help me to run the tests? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Please take a look at #1649 |
Beta Was this translation helpful? Give feedback.
-
Test fails for trio when ran together, but succeeds when ran separately! |
Beta Was this translation helpful? Give feedback.
Please take a look at #1649