Middleware
Code that runs before/after every request. Used for timing, security headers, CORS.
Code example
@app.middleware('http')
async def timing(request: Request, call_next):
t0 = time.perf_counter()
response = await call_next(request)
response.headers['X-Process-Time'] = f'{(time.perf_counter()-t0)*1000:.1f}ms'
return response
How it works
Middleware is a wrapper around the entire app. Each request passes through the middleware stack before reaching the route, and each response passes through it again on the way back. OpenBenchML uses middleware for: GZip compression (smaller responses), CORS (allow cross-origin requests), request timing (X-Process-Time header), security headers (X-Frame-Options, HSTS), and structured logging. Order matters: the last-added middleware runs first.
🔍 Where this is used in OpenBenchML
app/main.py — three middleware decorators set up the full stack.