# Bridge — Fastify API Layer > **Status:** Active — delivered (BIT-350, BIT-2076) --- ## Overview The bridge is a lightweight Fastify HTTP server that sits between the Astro frontend and WordPress. It abstracts WordPress, normalizes data into a canonical article model, and caches responses in Redis. A single bridge instance can serve multiple domains (each mapped to a different WordPress instance via domain config). --- ## Responsibilities - Route requests to the correct WordPress endpoint per domain - Cache WP REST API responses in Redis - Normalize WordPress post/category/tag responses into a canonical model - Expose cache invalidation endpoints (called by WP `save_post` hook) - Isolate the frontend from WP REST API changes --- ## Tech Stack | Layer | Choice | |-------|--------| | HTTP server | Fastify 5 | | Language | TypeScript (Node.js 24.22.1+) | | Cache | Redis (ioredis) | | HTTP client | `undici` | | Process manager | PM2 or systemd | --- ## Directory Structure ``` bridge/ ├── src/ │ ├── server.ts # Fastify instance │ ├── routes/ │ │ ├── posts.ts │ │ ├── categories.ts │ │ ├── search.ts │ │ ├── sitemap.ts │ │ └── cache.ts # Invalidation endpoints │ ├── services/ │ │ ├── wordpress.ts # WP REST client │ │ └── cache.ts # Redis wrapper │ ├── normalizers/ │ │ ├── post.ts # WP post → Article │ │ └── category.ts # WP category → Category │ └── config/ │ └── domain.ts # Domain → WP endpoint resolver ├── package.json └── tsconfig.json ``` --- ## API Routes | Method | Path | Description | |--------|------|-------------| | `GET` | `/posts?domain=&page=&per_page=` | List posts | | `GET` | `/posts/:slug?domain=` | Single post by slug | | `GET` | `/categories?domain=` | Category list | | `GET` | `/tags?domain=` | Tag list | | `GET` | `/search?domain=&q=` | Full-text search | | `GET` | `/sitemap?domain=` | All posts for sitemap | | `POST` | `/cache/invalidate` | Invalidate specific key | | `POST` | `/cache/invalidate-all` | Flush all keys for domain | --- ## Canonical Article Model ```typescript interface Article { id: number; slug: string; title: string; excerpt: string; content: string; // sanitized HTML published_at: string; // ISO 8601 modified_at: string; author: { id: number; name: string }; featured_image: { url: string; alt: string; width: number; height: number } | null; categories: { id: number; slug: string; name: string }[]; tags: { id: number; slug: string; name: string }[]; canonical_url: string; } ``` --- ## Caching See [architecture.md#7-caching-strategy](./architecture.md#7-caching-strategy). Redis key pattern: `{type}:{domain}:{identifier}` Examples: - `post:newsproject.bitreport.pt:porto-bate-benfica` - `posts-list:newsproject.bitreport.pt:futebol:1` - `categories:newsproject.bitreport.pt` --- ## Development ```bash cd bridge npm install npm run dev # localhost:3200 (tsx watch) npm run build # tsc → dist/ npm run test # Vitest ``` Environment variables: ```env REDIS_URL=redis://localhost:6379 PORT=3200 ```