Skip to content

[T-080a] API app factory: settings, CORS allow-list, GZip, health/ready, /meta, /metrics, request logging #142

Closed

Description

@CodewithJha

Task T-080a · Milestone M7 — API · Size S · Priority P0 · Epic #7

Implements 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 api CLI 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 on app.state.settings; dependencies obtain them through api/deps.py providers (get_settings, get_uow, get_artifact_store, get_services) so tests override with app.dependency_overrides.
  • bootstrap/api.py is 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.request with route template, 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"], expose ETag).
  • Health: /health/live → 200 {status:"ok"}; /health/ready checks SELECT 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).
  • /metrics mounted at root via prometheus_client.make_asgi_app(registry); request histogram roboprobe_api_request_duration_seconds{route,method,status} (doc 21 §5.2).
  • Router registry: api/routers/__init__.py exposes ROUTERS: 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.host
  • api.port
  • api.base_path
  • api.cors_origins
  • observability.metrics_enabled

Acceptance criteria

  • /health/ready returns 503 when DB down or migrations behind.
  • /meta lists 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).
  • route label 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 api starts 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 Unreleased updated 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_overrides fixture api_client in tests/api/conftest.py reused 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.py
  • backend/src/roboprobe/api/deps.py
  • backend/src/roboprobe/api/routers/__init__.py
  • backend/src/roboprobe/api/routers/health.py
  • backend/src/roboprobe/api/routers/meta.py
  • backend/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:

Activity

  1. added this to the M7 — API milestone on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:apiREST + SSE API (FastAPI)milestone:M7M7 — APIpriority:P0Critical path / blocks otherssize:S<= 1 daytaskChild task of a milestone epictype:featureNew functionality

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions