loom.rest.fastapi.app¶
FastAPI application factory.
create_fastapi_app() is the composition root that wires together the
domain bootstrap result and REST interface declarations into a runnable
FastAPI instance.
It is intentionally kept thin — all validation happens during
RestInterfaceCompiler compilation (fail-fast at
startup) and all request handling is delegated to
bind_interfaces().
Usage:
result = bootstrap_app(
config=cfg,
use_cases=[CreateOrderUseCase, GetOrderUseCase],
modules=[register_repositories],
)
app = create_fastapi_app(
result,
RouteSources(python=[OrderRestInterface]),
observability_runtime=ObservabilityRuntime.noop(),
title="Orders API",
version="1.0.0",
)
Functions
|
Patch |
|
Return the application-scoped RuntimeExecutor registered at bootstrap. |
|
Resolve the deprecated interfaces alias into routes. |
|
Create a FastAPI application from a bootstrap result and REST interfaces. |
- loom.rest.fastapi.app.create_fastapi_app(result, routes=None, *, interfaces=None, observability_runtime=None, middleware=(), defaults=None, **fastapi_kwargs)[source]¶
Create a FastAPI application from a bootstrap result and REST interfaces.
Compiles all
RestInterfacedeclarations viaRestInterfaceCompiler, binds each compiled route to theFastAPIinstance, and returns the ready application.Compilation is fail-fast: any structural error (missing use-case plan, duplicate route, missing prefix) raises
InterfaceCompilationErrorbefore the app starts accepting requests.- Parameters:
result (BootstrapResult) – Fully initialised
BootstrapResultfrombootstrap_app().routes (RouteSources | None) – Which interfaces to mount and from which origin — see
RouteSources.routes.configcompiles afterroutes.python, deterministically; a(method, path)collision between the two aborts naming both — neither side takes precedence.routes.disabledis applied toroutes.pythonbefore the merge, so a disabled Python route can be redeclared inroutes.configwithout colliding with itself; an entry matching no Python-declared route aborts startup instead of doing nothing. Required unless interfaces is given instead.interfaces (Sequence[type[RestInterface[Any]]] | None) – Deprecated keyword-only alias for
routes=RouteSources(python=interfaces). Kept only so code written beforeRouteSourcesexisted keeps working when it calledcreate_fastapi_app(result, interfaces=[...])— every published example did; emits aDeprecationWarningnaming routes as the replacement. Mutually exclusive with routes.observability_runtime (ObservabilityRuntime | None) – Shared runtime used to emit lifecycle events around each request.
ASGI middleware classes to register on the application. Added in declaration order (first = outermost wrapper). Accepts any class compatible with
FastAPI.add_middleware. Example:from loom.rest.middleware import TraceIdMiddleware from loom.prometheus import PrometheusMiddleware app = create_fastapi_app( result, RouteSources(python=[...]), observability_runtime=ObservabilityRuntime.noop(), middleware=[TraceIdMiddleware, PrometheusMiddleware], )
defaults (RestApiDefaults | None) – Global REST API defaults (pagination mode, profile policy). Falls back to
RestApiDefaultswhen not provided.**fastapi_kwargs (Any) – Additional keyword arguments forwarded to the
FastAPIconstructor (e.g.title,version,docs_url).
- Returns:
Configured
fastapi.FastAPIinstance ready to serve requests.- Raises:
InterfaceCompilationError – If any interface fails structural validation.
TypeError – If neither routes nor interfaces is given, or both are.
- Return type:
fastapi.FastAPI
Example:
app = create_fastapi_app( result, RouteSources(python=[UserRestInterface, OrderRestInterface]), defaults=RestApiDefaults(pagination_mode=PaginationMode.CURSOR), observability_runtime=ObservabilityRuntime.noop(), title="My API", version="2.0.0", )