Files
news-platform/docs/bridge.md
T
David SantosandPaperclip 7b7830263b docs: architecture document and project scaffold for news-platform
- docs/architecture.md: full architecture with Astro 5 SSR vs Next.js/Nuxt
  justification, component diagram, data flow, multi-domain routing, SEO
  strategy, performance targets, and caching strategy
- docs/frontend.md, bridge.md, domains.md, seo.md, deployment.md: stubs
  with implementation detail for each layer
- config/domains/newsproject.bitreport.pt.json: first domain config
- project skeleton: frontend/, bridge/, wordpress/, config/, deploy/

Covers BIT-348 deliverables. Push pending Gitea repo creation.

Co-Authored-By: Paperclip <[email protected]>
2026-03-23 14:11:39 +00:00

3.3 KiB

Bridge — Fastify API Layer

Status: Stub — implementation in progress (BIT-345)


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 22+)
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

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.

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

cd bridge
npm install
npm run dev        # localhost:3200 (tsx watch)
npm run build      # tsc → dist/
npm run test       # Vitest

Environment variables:

REDIS_URL=redis://localhost:6379
PORT=3200