ADR-004: Backend Application Architecture
Context
To support the live AI showcases, a secure server component is required to:
1. Safely proxy generative AI calls without exposing project settings.
2. Execute cryptographic parsing and formatting of SSL/TLS certificates.
3. Implement rate-limiting and security middleware to block abuse.
4. Stand up rapidly during scale-to-zero wakeups.
Decision
Implement a lightweight Express.js server written in TypeScript, compiled into a single CommonJS (CJS) file using esbuild.
The server code is bundled into `dist/server.cjs`, while npm packages stay external (`--packages=external`) and ship in the image as production `node_modules`. This bypasses runtime TypeScript transpilation (e.g. `ts-node`). Cold starts are kept short by loading heavy modules (Firestore, App Check) lazily on first use (amended 2026-11-01: LangGraph lazy compilation was eliminated when the copilot moved to a direct TypeScript state machine, ADR-024).
Consequences
Benefits:
* No runtime transpilation, and heavy SDKs load only when first needed, keeping cold starts short.
* Abuse controls in plain Express: hand-written security-header middleware (no helmet), express-rate-limit per IP (30 requests / 15 min) and a site-wide daily cap on AI calls (AI_DAILY_REQUEST_CAP, default 300).
* Keeps the server-side memory footprint extremely low (~30MB), making it highly cost-effective.
* Type-safety is enforced at compile time, guaranteeing consistent API contracts.
Alternatives considered
1. Python (FastAPI/Flask): Rejected to maintain a unified single-language codebase (TypeScript) across both the client and server, avoiding polyglot container size overhead.
2. Nest.js: Rejected due to excessive boilerplate, slower cold start times, and unnecessary runtime dependency injection overhead.