From cd41280248cf4eeb6478b33f86645c0234d7009d Mon Sep 17 00:00:00 2001 From: Nguyen Tan Anh Date: Sat, 14 Nov 2026 03:13:05 +0200 Subject: [PATCH] refactor(repo): apply CONTRIBUTING code rules The template code broke a few of its own rules. New repos copy it, so it must show the rules it asks for. The frontend now gets a domain value from src/api.ts, and the wire value "ok" stays in that file. --- Makefile | 1 - backend/app/api/health.py | 3 ++- backend/app/config.py | 5 ++++- backend/app/main.py | 5 ++++- backend/tests/test_health.py | 5 ++++- frontend/src/App.tsx | 6 +++--- frontend/src/api.ts | 13 ++++++++++--- 7 files changed, 27 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 302778a..d5cbec0 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,6 @@ LINT_IMAGE := rhysd/actionlint@sha256:b1934ee5f1c509618f2508e6eb47ee0d3520686341fec936f3b79331f9315667 LINT_RUN := docker run --rm -v "$(CURDIR):/repo" -w /repo -# Install all deps for local work. setup: cd backend && uv sync cd frontend && npm ci diff --git a/backend/app/api/health.py b/backend/app/api/health.py index cccab52..c1a7f6e 100644 --- a/backend/app/api/health.py +++ b/backend/app/api/health.py @@ -3,6 +3,7 @@ router = APIRouter() +# The container health check and the deploy.sh rollback call this path. Do not move it. @router.get("/health") -def health() -> dict[str, str]: +def get_health() -> dict[str, str]: return {"status": "ok"} diff --git a/backend/app/config.py b/backend/app/config.py index 26ca440..0c2fd39 100644 --- a/backend/app/config.py +++ b/backend/app/config.py @@ -4,6 +4,9 @@ class Settings(BaseSettings): # Root .env for `make dev-be` (cwd is backend/). Missing files are skipped, and in # Docker the values come from Compose env_file instead. - model_config = SettingsConfigDict(env_file=("../.env", ".env"), extra="ignore") + model_config = SettingsConfigDict( + env_file=("../.env", ".env"), + extra="ignore", + ) app_name: str = "junction-2026" diff --git a/backend/app/main.py b/backend/app/main.py index 5cd9653..7772a86 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -10,7 +10,10 @@ def create_app(settings: Settings | None = None) -> FastAPI: settings = settings or Settings() app = FastAPI(title=settings.app_name) - app.include_router(health.router, prefix=_API_PREFIX) + app.include_router( + health.router, + prefix=_API_PREFIX, + ) return app diff --git a/backend/tests/test_health.py b/backend/tests/test_health.py index 8bb9b1a..875a445 100644 --- a/backend/tests/test_health.py +++ b/backend/tests/test_health.py @@ -1,12 +1,15 @@ +from http import HTTPStatus + from fastapi.testclient import TestClient from app.main import create_app def test_health_returns_ok() -> None: + """Deploy waits for this route to be healthy and rolls back when it is not.""" client = TestClient(create_app()) response = client.get("/api/health") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK assert response.json() == {"status": "ok"} diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index eac671a..a6b4dcd 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,8 +1,8 @@ import { useEffect, useState, type ReactElement } from 'react' -import { getHealth } from './api.ts' +import { getHealth, type BackendHealth } from './api.ts' -type BackendStatus = 'loading' | 'ok' | 'down' +type BackendStatus = 'loading' | BackendHealth export function App(): ReactElement { const [status, setStatus] = useState('loading') @@ -10,7 +10,7 @@ export function App(): ReactElement { useEffect(() => { getHealth() .then((health) => { - setStatus(health.status === 'ok' ? 'ok' : 'down') + setStatus(health) }) .catch(() => { setStatus('down') diff --git a/frontend/src/api.ts b/frontend/src/api.ts index a17521f..834be37 100644 --- a/frontend/src/api.ts +++ b/frontend/src/api.ts @@ -1,15 +1,22 @@ // Single place that talks to the backend. Caddy (prod) and the Vite proxy (dev) // route /api/* to FastAPI, so relative paths work in both. const API_BASE = '/api' +const HEALTHY_STATUS = 'ok' -export interface Health { +interface HealthResponse { readonly status: string } -export async function getHealth(): Promise { +export type BackendHealth = 'ok' | 'down' + +export async function getHealth(): Promise { const response = await fetch(`${API_BASE}/health`) + if (!response.ok) { throw new Error(`Health check failed: ${response.status}`) } - return (await response.json()) as Health + + const body = (await response.json()) as HealthResponse + + return body.status === HEALTHY_STATUS ? 'ok' : 'down' }