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,
    interfaces=[OrderRestInterface],
    title="Orders API",
    version="1.0.0",
)

Functions

_register_openapi_components(app, schemas)

Patch app.openapi to inject schemas into components.schemas.

_resolve_executor(result)

Return the application-scoped RuntimeExecutor registered at bootstrap.

create_fastapi_app(result, interfaces, *[, ...])

Create a FastAPI application from a bootstrap result and REST interfaces.

loom.rest.fastapi.app.create_fastapi_app(result, interfaces, *, middleware=(), defaults=None, **fastapi_kwargs)[source]

Create a FastAPI application from a bootstrap result and REST interfaces.

Compiles all RestInterface declarations via RestInterfaceCompiler, binds each compiled route to the FastAPI instance, and returns the ready application.

Compilation is fail-fast: any structural error (missing use-case plan, duplicate route, missing prefix) raises InterfaceCompilationError before the app starts accepting requests.

Parameters:
  • result (BootstrapResult) – Fully initialised BootstrapResult from bootstrap_app().

  • interfaces (Sequence[type[RestInterface[Any]]]) – RestInterface subclasses declaring which endpoints to expose. Compiled in declaration order.

  • middleware (Sequence[Any]) –

    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,
        interfaces=[...],
        middleware=[TraceIdMiddleware, PrometheusMiddleware],
    )
    

  • defaults (RestApiDefaults | None) – Global REST API defaults (pagination mode, profile policy). Falls back to RestApiDefaults when not provided.

  • **fastapi_kwargs (Any) – Additional keyword arguments forwarded to the FastAPI constructor (e.g. title, version, docs_url).

Returns:

Configured fastapi.FastAPI instance ready to serve requests.

Raises:

InterfaceCompilationError – If any interface fails structural validation.

Return type:

fastapi.FastAPI

Example:

app = create_fastapi_app(
    result,
    interfaces=[UserRestInterface, OrderRestInterface],
    defaults=RestApiDefaults(pagination_mode=PaginationMode.CURSOR),
    title="My API",
    version="2.0.0",
)