diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6458e7e..5cc895a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -96,6 +96,17 @@ jobs: - run: uv run ruff check . - run: uv run ruff format --check . - run: uv run pytest + # Starts the server with the same command as render.yaml, so a broken start + # command or import error fails here instead of in a Render deploy. + - name: Server starts and answers /health + run: | + uv run --no-sync uvicorn app.main:app --host 127.0.0.1 --port 8000 & + for i in $(seq 1 20); do + curl -fsS http://127.0.0.1:8000/health && exit 0 + sleep 1 + done + echo "::error::API did not answer /health within 20 seconds" + exit 1 # Applies every migration to a throwaway local Supabase on the runner. Uses no # secrets and never touches a hosted project. diff --git a/AGENTS.md b/AGENTS.md index 6a93f3c..7821a00 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -36,7 +36,8 @@ side the evidence supports. Web app first, mobile later. database directly (the Supabase MCP is read-only on purpose) and never edit a migration that is already on `main`. - `NEXT_PUBLIC_` variables ship to every browser: never put secrets in them. Secrets go in - `api/.env`. Keep `web/.env.example` and `api/.env.example` listing every variable. + `api/.env`. Keep `web/.env.example` and `api/.env.example` listing every variable; a new + backend variable also goes in `render.yaml` `envVars` (secrets as `sync: false`). - Never print, log, commit or paste API keys or `.env` contents. The team shares keys. - Never connect AI tools to the production database. - Read AI models from settings (`CLAIM_MODEL`, `ANALYSIS_MODEL`); never hardcode a provider. diff --git a/api/.env.example b/api/.env.example index a12c4f7..d799ee2 100644 --- a/api/.env.example +++ b/api/.env.example @@ -2,6 +2,10 @@ # The team shares the AWS, AssemblyAI and Exa keys: Hani sends each one through a # one-time link. Never paste keys into chat, commits, logs or AI prompts. +# Browser origins allowed to call the API, comma-separated. Local default shown; on Render, +# set it to the Vercel URL(s). +CORS_ORIGINS=http://localhost:3000 + # Supabase dashboard > Connect > Session pooler connection string (includes the DB password) DATABASE_URL= # Supabase dashboard > Project Settings > API: Project URL diff --git a/api/app/main.py b/api/app/main.py index d0cac05..21e7522 100644 --- a/api/app/main.py +++ b/api/app/main.py @@ -1,7 +1,17 @@ from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware + +from app.settings import get_settings app = FastAPI(title="Dialectic.ai API") +app.add_middleware( + CORSMiddleware, + allow_origins=[o.strip() for o in get_settings().cors_origins.split(",") if o.strip()], + allow_methods=["*"], + allow_headers=["*"], +) + @app.get("/health") def health() -> dict[str, str]: diff --git a/api/app/settings.py b/api/app/settings.py index 32743e7..825e2ee 100644 --- a/api/app/settings.py +++ b/api/app/settings.py @@ -8,6 +8,9 @@ class Settings(BaseSettings): model_config = SettingsConfigDict(env_file=".env", extra="ignore") + # Comma-separated browser origins allowed to call the API, e.g. the Vercel URL. + cors_origins: str = "http://localhost:3000" + database_url: str | None = None supabase_url: str | None = None diff --git a/api/tests/test_cors.py b/api/tests/test_cors.py new file mode 100644 index 0000000..06c8d1e --- /dev/null +++ b/api/tests/test_cors.py @@ -0,0 +1,22 @@ +from fastapi.testclient import TestClient + +from app.main import app + +client = TestClient(app) + + +def preflight(origin: str): + return client.options( + "/health", + headers={"Origin": origin, "Access-Control-Request-Method": "GET"}, + ) + + +def test_allowed_origin() -> None: + response = preflight("http://localhost:3000") + assert response.headers["access-control-allow-origin"] == "http://localhost:3000" + + +def test_other_origin_rejected() -> None: + response = preflight("https://evil.example") + assert "access-control-allow-origin" not in response.headers diff --git a/docs/architecture.md b/docs/architecture.md index db6075e..4a3c944 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -45,6 +45,26 @@ which promotional credits may not cover; Amazon's Nova models are covered). Othe use the Gemini API. At alpha, compare candidate models on sample debates with `pydantic_evals` before settling. +## Backend hosting (Render) + +FastAPI runs on Render as an always-running web service, defined in `render.yaml` at the +repo root. Vercel can't host it: its functions are short-lived and can't hold the long +WebSocket connections a live debate needs (browser audio in, claims and fact-checks out). + +- Render builds from `api/` with `uv sync --locked --no-dev` and starts the same uvicorn + command the CI `api` job tests. It checks `/health` before sending traffic to a new + deploy. +- It deploys a commit on `main` only after that commit's GitHub checks pass, and only + when files under `api/` changed. Region: Virginia, next to Supabase. +- Secrets are entered once in the Render dashboard (`sync: false` in `render.yaml`). + `CORS_ORIGINS` lists the frontend URLs allowed to call the API. +- Plan: **Free** until alpha. It sleeps after 15 minutes without traffic and takes about a + minute to wake. 750 free hours a month is more than one service can use, so it never + runs out. Switch to **Starter** ($7/month, always on) before the first outside testers, + because a sleeping server would stall a live debate. +- The account is Hani's. Developers run the API locally; the Render service is the shared + deployed copy. + ## Accounts and keys - AWS: one account owned by Hani. Each developer gets their own IAM user and Bedrock API @@ -93,6 +113,7 @@ MVP, alpha, pilot, beta, public release (web, then iOS and Android). project; schema changes are tested by CI. - **Alpha:** add Docker: a local Supabase per developer (`supabase start`) and containers for consistent local setup. Google sign-in and a custom email sender. Model evaluation. + Render service on the Starter plan. - **Before the pilot:** create the production database (checklist below). Invite-only sign-ups. - **Beta:** `@virginia.edu` sign-ups. - **Public:** mobile apps, Sign in with Apple. diff --git a/render.yaml b/render.yaml new file mode 100644 index 0000000..c8592ce --- /dev/null +++ b/render.yaml @@ -0,0 +1,44 @@ +# Render Blueprint for the FastAPI backend. Render reads this file from the repo root. +# Secret values are never stored here: `sync: false` makes Render prompt for each one +# in its dashboard. Keep envVars in step with api/.env.example. +services: + - type: web + name: dialectic-api + runtime: python + plan: free + # Same region as the Supabase database (AWS us-east-1) and Vercel's default. + region: virginia + rootDir: api + buildCommand: uv sync --locked --no-dev + # --no-sync: the build already installed everything; don't reinstall on each start. + startCommand: uv run --no-sync uvicorn app.main:app --host 0.0.0.0 --port $PORT + healthCheckPath: /health + # Deploy a commit on main only after its GitHub checks pass. + autoDeployTrigger: checksPass + buildFilter: + paths: + - api/** + envVars: + # Render reads .python-version only from the repo root; ours lives in api/. + - key: PYTHON_VERSION + value: "3.14.10" + - key: CORS_ORIGINS + sync: false + - key: DATABASE_URL + sync: false + - key: SUPABASE_URL + sync: false + - key: AWS_REGION + value: us-east-1 + - key: AWS_BEARER_TOKEN_BEDROCK + sync: false + - key: GEMINI_API_KEY + sync: false + - key: CLAIM_MODEL + sync: false + - key: ANALYSIS_MODEL + sync: false + - key: ASSEMBLYAI_API_KEY + sync: false + - key: EXA_API_KEY + sync: false