Bigger applications¶
Real FastAPI applications are split across modules: APIRouters included with prefixes, an app built by a
factory function, sub-applications mounted under a path. py2axum resolves the imports statically and
registers the routes in the order Python would, then routes requests like Starlette.
The documentation's examples are one application split in modules: each page of this tutorial has
its APIRouter in docs_src/tutorial/, and docs_src/main.py includes them all. Imports between them are
relative (from ..db import SessionDep) and resolved statically. Like every example on this site, it is compiled and compared with FastAPI in CI (how).
docs_src/main.py
"""The application of the documentation's examples: each page of the tutorial has its router in tutorial/.
uvicorn docs_src.main:app # the Python application, from the repository's root
py2axum docs_src --root . -o build/docs_axum # the same application, compiled
"""
from contextlib import asynccontextmanager
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.base import BaseHTTPMiddleware
from .db import Base, engine
from .tutorial import (background, body, dependencies, errors, files, models, parameters, responses, security, sql,
websockets)
@asynccontextmanager
async def lifespan(app: FastAPI):
# before the server listens: create the tables that do not exist yet
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield
# after the server has stopped (SIGTERM, Ctrl+C)
print("shutting down")
app = FastAPI(title="py2axum documentation examples", lifespan=lifespan)
@app.middleware("http")
async def add_served_by(request: Request, call_next):
response = await call_next(request)
response.headers["X-Served-By"] = "docs-example"
return response
class RequestIdMiddleware(BaseHTTPMiddleware):
"""Echo the caller's request id, or say there was none."""
async def dispatch(self, request: Request, call_next):
response = await call_next(request)
response.headers["X-Request-Id"] = request.headers.get("x-request-id", "none")
return response
app.add_middleware(RequestIdMiddleware)
app.add_middleware(
CORSMiddleware,
allow_origins=["https://app.example.org"],
allow_credentials=True,
allow_methods=["GET", "POST"],
allow_headers=["authorization", "content-type"],
)
app.add_exception_handler(errors.UnicornException, errors.unicorn_handler)
app.include_router(parameters.router)
app.include_router(body.router)
app.include_router(models.router)
app.include_router(responses.router)
app.include_router(errors.router)
app.include_router(dependencies.router)
app.include_router(security.router)
app.include_router(sql.router)
app.include_router(files.router)
app.include_router(background.router)
app.include_router(websockets.router)
@app.get("/health")
async def health() -> dict:
return {"status": "ok"}
What is native¶
- Custom route classes (FastAPI's "Custom Request and APIRoute class"):
APIRouter(route_class=R)andapp.router.route_class = R(for the app's routes declared after it in the same module), whereRsubclassesAPIRouteand overrides onlyget_route_handler():super().get_route_handler()is the route's own handler, called with the request and returning the Response object (status_code, mutableheaders, its class intype()andrepr()), its exceptions raised as they are (RequestValidationError,HTTPException). The handler is made once per route, at its first request (FastAPI: when the route is created). A project subclass ofRequestbuilt asGzipRequest(request.scope, request.receive)wraps the request: its attributes and methods fall back to it,super().body()reads the raw body, and its ownbody()is what the route parses when it has a body or form parameter (an error there: FastAPI's 400There was an error parsing the body); the endpoint'srequestparameter is that instance. Refused: readingself(the APIRoute) inget_route_handler, other overridden methods,route_classset twice, an app route in another module than the assignment. FastAPI()apps,APIRouter(prefix=, tags=, dependencies=, default_response_class=),include_router, routers split across modules (also outside the translated directory, asrc/layout: the modules the app imports), apps built by a factory function (create_app()), including endpoints defined inside it; a local of the factory assigned once is evaluated once.ifstatements around registrations are evaluated at startup in the factory's scope (e.g.if not settings.TESTING:); the factory's parameters have their default values (the server calls it without arguments).include_router(router, prefix=<expression>)at module level (e.g.prefix=settings.API_V1_PREFIX): the prefix is evaluated at startup, after the module globals, so the environment still overrides the settings; it is checked like FastAPI (must start with/, must not end with/; a failure stops the binary as an import error stops uvicorn).- A project function given the app that includes routers (
def add_routers(app): app.include_router(...)), called once with the app in the factory or at module level: its routers are registered where it is called. Itsprefix=may read its parameters (as the call passes them, or their literal defaults) or module globals (evaluated at startup).for r in ROUTERS: app.include_router(r)over a literal tuple of the module (or a list it never changes) is unrolled. A function without parameters that only builds a router from routers (def build(): r = APIRouter(prefix="/v1"); r.include_router(...); return r) is included by its call (app.include_router(build())). include_routerunder anifat module level (if settings.FASTAPI_ENV == "development":,if os.getenv("ENABLE_X"):,elsebranches, nestedifs, in the app or in a router included later): each test is read once at startup, after the module globals, and the routes of a branch that is off are not served (a 404, as if never registered). A test that calls anything butos.getenv/os.environ.get,bool,isinstanceorstr.lower/upper/strip, or a registration under atry, a loop or awith, is refused.- An import guard on the project's own code is decided like CPython when that code is absent (an optional,
gitignored module): after
try: from app.api import extra / except ImportError: extra = None,if extra is not None: app.include_router(extra.router)never runs andif HAS_EXTRA:keeps one branch. - Imports are resolved statically, without executing code: relative imports, re-exports from
__init__, lazy imports inside functions (visible from nested functions and lambdas),--rootassys.path. - Routing like Starlette: registration order (each
include_routerat its place among the routes of the app or router it is called on), decoded path, first match on path and method wins, then 405 with the methods of the first path match, then a 307 redirect with/without the trailing slash, then 404. No implicit HEAD. The redirect useshttp://(as uvicorn does without trusted proxy headers). - Raw ASGI routes:
app.add_route(path, obj, methods=...)/app.router.add_route(...)at module level, with an instance of a project class definingasync def __call__(self, scope, receive, send)(Starlette runs anything that is not a function or a method as an ASGI app). The app gets an ASGI 3scopedict (the keys uvicorn and Starlette's router set,scope["app"]with an emptydependency_overrides),receive(the body in onehttp.requestmessage, then nothing until the client leaves) andsend(http.response.start/http.response.body, streamed whenmore_bodyis true).Request(scope, receive)(the request being served only), a response object called as an ASGI app (await JSONResponse(...)(scope, receive, send)), a generator dependency called by hand (gen = get_session(); s = await anext(gen); ...; await gen.aclose()). - Routing objects as values:
request.app,app.router.routesas FastAPI 0.141+ lists them (its docs routes, an_IncludedRouterperinclude_routerwith itsoriginal_router,APIRoutes, the addedRoutes), theirpath,path_format,methods,name,matches(scope)returningstarlette.routing.Matchand the child scope,isinstance(r, Route | APIRoute).request.scopeis a snapshot dict withtype,http_version,scheme,method,root_path,path,raw_path,query_string,headers,client,app, and, once the router has run,path_paramsandroute(theAPIRoute, as Starlette sets it); theendpointof scopes is None.FastAPI(docs_url=...)and the other*_urloptions must be literals forapp.routesto be read.
Routes added by a configure(app) function are described in Middleware.
Mounted applications¶
app.mount(path, X)(a sub-application,StaticFiles, FastMCP'sstreamable_http_app()...) is not translated, but the app's last registrations may be mounts left to the Python side (--python-side mount, implied by--python-side auto): withPY2AXUM_PYTHON_URLset, a request under the mount's path (or the path itself, for Starlette's 307 topath/) that no translated route fully matches (path and method) is relayed to the Python application. As in Starlette, a mount's full match beats an earlier partial one: a HEAD, or another method, on a translated GET route under the mount reaches the mounted application, not a- A non-literal mount path relays every unmatched request. Refused with
file:line: a mount followed by another registration (include_router,@app.get,add_api_route,add_route...: a route after the mount is shadowed by it in Python but would be served by the binary), or registrations of the app in another module or function than the mount (their order depends on imports or calls). WebSocket handshakes under the mount are not relayed (see WebSockets).
Routers of libraries¶
app.include_router(router)whereroutercomes from a library (imported from it, or returned by one of its functions:app.include_router(lib.make_router(...), prefix="/x")) is not translated: py2axum reads only the project, so the library's routes are unknown. Included among the app's last registrations, its routes stay on the Python side (--python-side mount, implied by--python-side auto), like a final mount: withPY2AXUM_PYTHON_URLset, a request that no translated route fully matches (path and method) is relayed to the Python application, under the literalprefix=of the call, or every such request without one (the Python application then answers the 404s and 405s itself, the library's routes included: a POST on a translated GET route reaches the library's POST route of the same path, as in Starlette). Refused withfile:line(P2A0315): a registration after it (a route, aninclude_routerof the project, a function given the app that registers routes, even through another function: Starlette would try the library's routes first), a conditional include (underif,try, a loop,with), a prefix that is not a literal string, an include on a router instead of the app, and registrations of the app in another module or function.FastAPI(root_path="/api/v1")(a literal;root_path_in_servers=only changes the OpenAPI schema): the scope'sroot_path, stripped from the path before routing when the path starts with it (Starlette'sget_route_path, so/appand/api/v1/appboth match), part ofrequest.base_url. Refused together with WebSocket routes,app.mount()or Python-side routes.
What stays in Python¶
- Mounted applications (above), left to Python with
--python-side mount. - Routers of libraries (above), left to Python with
--python-side mount. - Raw ASGI routes: a class as endpoint (
HTTPEndpoint) is refused.
Differences¶
include_routerunder anifthat is off at startup: its routes are not served, butapp.routesstill lists them.include_router: a runtime prefix containing a path parameter ({...}) stops the binary; a non-literal prefix inside a function (app factory) is refused at compile time, except in a function given the app (above);--python-sidecannot name a route under a runtime prefix (it is compiled).- Raw ASGI routes:
allowof a 405 lists the methods in declaration order (Starlette iterates a set, so its order varies between processes); mutations ofdependency_overridesare lost. - A 405 on an added
RoutelistsGET, HEADin that order (CPython: set order).