middleware #11570
Unanswered
hanzhenbjm
asked this question in
Questions
middleware
#11570
Replies: 1 comment
-
I think with this sort of middleware, you'll want to use a more low level approach. class LogMiddleware:
def __init__(self, app):
self.app = app
async def __call__(self, scope, receive, send):
if scope["type"] != "http":
await self.app(scope, receive, send)
return
with logger.contextualize(...):
request_time = datetime.datetime.now()
try:
await self.app(scope, receive, send)
finally:
response_time = datetime.datetime.now()
logger.bind(...).info("finish") Let the app handle the response. You only need to worry about the timing. If you want to pass data from the endpoint to the middleware, use the scope. Wrap send and receive if you want to get data from the request or response body. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Privileged issue
Issue Content
Are there any problems with writing middleware like this?
Will this affect performance? Or how to write it more perfectly?
Apart from this, is there any better way to write other codes?
thank you very much!
Beta Was this translation helpful? Give feedback.
All reactions