Repository navigation
Description
Task
T-080a· Milestone M7 — API · Size S · Priority P0 · Epic #7Implements FR-API-001 · NFR-OBS-001 · NFR-SEC-001
Branch
feat/t-080a-api-app-factory-settings-cors— one task = one branch = one PR (doc 32 §6)
Summary
Implement api/app.py create_app(settings) -> FastAPI (doc 05 §10), bootstrap/api.py, dependency providers in api/deps.py, GET /health/live|ready, GET /api/v1/meta (version, git SHA, backends, strategies, oracles), root /metrics, api.request logs with duration_ms.
Context / motivation
Foundation for all routers; readiness checks DB connectivity, migrations at head and artefact root writability (doc 21 §5.4).
Scope
In scope
- Router registration list in
api/routers/__init__.py(one line per router). - CORS from
api.cors_origins(no wildcard);GZipMiddleware(minimum_size=1024). roboprobe apiCLI command wired (T-045c stub).
Implementation guidance
roboprobe/api/app.py:def create_app(settings: RoboProbeSettings | None = None) -> FastAPI(doc 05 §10). Settings resolved once (T-003a) and stored onapp.state.settings; dependencies obtain them throughapi/deps.pyproviders (get_settings,get_uow,get_artifact_store,get_services) so tests override withapp.dependency_overrides.bootstrap/api.pyis the composition root (doc 34 §3): builds engine/session factory, ArtifactStore, plugin registries (backends, strategies, oracles, analyzers) and application services; routers never construct infrastructure.- Lifespan (
@asynccontextmanager): open DB pool, start EventHub (T-083a), dispose on shutdown; no work at import time. - Middleware order: request-id/trace-id (generate
trace_id, bind to structlog contextvars) → access log (api.requestwithroutetemplate,method,status,duration_ms) → GZip (minimum_size=1024) → CORS (allow_origins=settings.api.cors_origins,allow_credentials=False,allow_headers=["Authorization","Content-Type","If-Match","Idempotency-Key","Last-Event-ID"], exposeETag). - Health:
/health/live→ 200{status:"ok"};/health/readychecksSELECT 1, Alembic head == DB revision (alembic.script.ScriptDirectory), artefact root writable (tempfile create/delete) → 503 problem+json listing failing checks (doc 21 §5.4). /api/v1/meta:{version, git_sha, api_version:"v1", backends:[{name, capabilities}], strategies:[{name, description, params_schema}], oracles:[…], analyzers:[…]}— populated from registries (strategy param schemas from T-060)./metricsmounted at root viaprometheus_client.make_asgi_app(registry); request histogramroboprobe_api_request_duration_seconds{route,method,status}(doc 21 §5.2).- Router registry:
api/routers/__init__.pyexposesROUTERS: list[APIRouter]; each router task appends exactly one import + list entry (append-only shared file). - Standards: respect the layering contracts (doc 16 §2) and conventions in doc 34; no hard-coded tunables — thresholds/defaults come from typed settings (doc 27); no magic numbers (ruff
PLR2004); public APIs typed (mypy strict) and documented.
Configuration keys
From the configuration reference (doc 27). Keys marked (new) must be added to the typed settings models and appear in the generated reference (T-003c).
api.hostapi.portapi.base_pathapi.cors_originsobservability.metrics_enabled
Acceptance criteria
-
/health/readyreturns 503 when DB down or migrations behind. -
/metalists registered plugins (incl. strategy params schemas from T-060 once available). - Request log line per request with route template (not raw path).
-
create_app()has no side effects at import (test imports module without DB). -
routelabel uses the template (/api/v1/runs/{run_id}), never raw ids (cardinality test). - CORS preflight from a disallowed origin gets no
Access-Control-Allow-Origin. -
roboprobe apistarts uvicorn with host/port from settings. - Tests listed below added and green in CI; coverage of new code meets the gate in doc 18 §3.
- Docs, config reference and CHANGELOG
Unreleasedupdated where behaviour, keys or interfaces changed. - Definition of Done (doc 32 §3) satisfied.
Tests required
tests/api/test_app_health_meta.py(httpx TestClient).dependency_overridesfixtureapi_clientintests/api/conftest.pyreused by all router tests.
Dependencies
Blocked by: #61 (T-011a), #23 (T-004a), #24 (T-004b), #94 (T-046)
Blocks: #143 (T-080b), #144 (T-080c), #145 (T-080d), #155 (T-083a), #214 (T-125)
Parallelization notes
Files / paths owned by this task — avoid editing other paths; shared files (pyproject.toml entry points, cli/main.py, api/routers/__init__.py, router.tsx, docker/compose.yaml, ci.yml) are append-only:
backend/src/roboprobe/api/app.pybackend/src/roboprobe/api/deps.pybackend/src/roboprobe/api/routers/__init__.pybackend/src/roboprobe/api/routers/health.pybackend/src/roboprobe/api/routers/meta.pybackend/src/roboprobe/bootstrap/api.py
Interface task for the API stream; routers then proceed in parallel (one file each).
Definition of Done
Per doc 32 §3 and AGENTS.md §5: lint, format, mypy strict and import-linter green; tests added and passing; docs updated; PR template completed with Closes #<this issue>; Conventional Commit messages referencing T-080a (doc 16 §6); commits authored only by the repository owner identity (AGENTS.md §4); CI green; reviewer checklist (doc 32 §5.1) satisfied.
References
Design docs: doc 05 §10 · doc 12 §1 · doc 12 §2 · doc 21 §5.4 · ADR-0009 · doc 34 §3 · doc 21 §5.2
Requirements: FR-API-001 · NFR-OBS-001 · NFR-SEC-001 (traceability: doc 28)
External:
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsBacklog
Activity
roboprobe.bootstrap) and plugin entry-point registries #94?token=#144