Middleware, CORS, and lifespan
Middleware, CORS, and lifespan
Some things in a service are not about any one request. The database engine and its connection pool, the httpx client that talks to the model API, the Redis connection: these are created once when the process starts and closed once when it stops. Other things are about every request equally: a request id, a timing header, a check that the calling website is allowed. FastAPI has one mechanism for each. Lifespan is a context manager that wraps the whole life of the app. Middleware is a function that wraps every request. CORS is a specific middleware that answers a question only browsers ask.
# src/relay/main.py
from contextlib import asynccontextmanager
import httpx
from fastapi import FastAPI
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from relay.settings import get_settings
Continue reading
Pro unlocks every video lesson, the full notes and runnable code across the Python with AI course, from the language itself to agents, MCP, Langfuse and deployment.