FastAPI में middleware वह कोड है जो प्रत्येक request और response के लिए चलता है — आपके endpoints को लपेटता है logging, timing, CORS, और headers जोड़ने जैसे cross-cutting concerns को संभालने के लिए। FastAPI Starlette की middleware system का उपयोग करता है (यह ASGI-आधारित है)।
कस्टम middleware लिखना
time
():
start = time.time()
response = call_next(request)
response.headers[] = (time.time() - start)
response
