Files
Artur Moreira c75ae37054 docs: sync local frontend/domain documentation
Publish the local docs updates for domain and frontend notes so guidance is available in Git.
2026-04-08 08:58:21 +01:00

176 lines
6.5 KiB
Markdown

# Multi-Domain Configuration
> **Status:** Active — domain JSON + theme selection (see also [frontend.md](./frontend.md) for theme authoring).
---
## Overview
The platform supports multiple domains without duplicating page code. Each domain has a JSON config file in `config/domains/`. The Astro frontend reads the config at request time based on the `host` header; the bridge routes to the correct WordPress endpoint using the same config.
Switching look-and-feel is done with the `theme` field: it selects a bundle of Astro components under `frontend/src/themes/<name>/`. Adding a **new** theme id still requires registering it in code (see [Theme Authoring](./frontend.md#theme-authoring)).
---
## Adding a New Domain
1. Create `config/domains/<hostname>.json` (see schema below)
2. Set `theme` to a registered theme id (`sports`, `institutional`, or one you added in `frontend/src/lib/theme.ts`)
3. Configure DNS to point the domain at the server
4. If WordPress is a new instance, deploy it and update `wp_endpoint`
5. Redeploy when the config file or theme code changes (SSR reads JSON from disk at runtime, but theme registration is build-time)
---
## Choosing a Theme
Built-in themes:
| Theme id | Directory | When to use |
|----------|-----------|-------------|
| `sports` | `frontend/src/themes/sports/` | Default. Bold sports-portal layout; colours come mainly from domain JSON via CSS variables on `:root`. |
| `institutional` | `frontend/src/themes/institutional/` | Calmer serif-led layout; optional `theme.css` aligns tokens with an institutional palette; still honours domain `primary_color` / `secondary_color` / `accent_color` where the layout uses shared variables. |
**Rules:**
- `theme` in JSON must equal the folder name under `frontend/src/themes/` **and** be listed in `ThemeId` / `THEME_BY_ID` in `frontend/src/lib/theme.ts`. Astro needs static imports for each theme; a folder alone is not enough.
- If `theme` is missing or not registered, the app falls back to **`sports`** (`getThemeComponents()` in `theme.ts`).
**Example — point a domain at the institutional theme:**
```json
{
"site_name": "FPA Example",
"tagline": "Institutional news and announcements",
"theme": "institutional",
"logo": "/assets/logo.svg",
"favicon": "/favicon.svg",
"primary_color": "#1a3a5c",
"secondary_color": "#4a6fa5",
"accent_color": "#f5f5f5",
"categories": [
{ "slug": "news", "label": "News" },
{ "slug": "events", "label": "Events" }
],
"bridge_endpoint": "http://localhost:3200",
"wp_endpoint": "http://localhost:8080/wp-json",
"locale": "en-US",
"timezone": "Europe/Lisbon",
"ads_enabled": false,
"analytics_id": null
}
```
A full sample lives at `config/domains/fpa-example.json`.
**Example — `andebol.net` (institutional theme, Portuguese):**
```json
{
"site_name": "Andebol.net",
"tagline": "Notícias de andebol nacional e internacional",
"theme": "institutional",
"logo": "/assets/logo.svg",
"favicon": "/favicon.svg",
"primary_color": "#1a3a5c",
"secondary_color": "#4a6fa5",
"accent_color": "#f5f5f5",
"categories": [
{ "slug": "nacional", "label": "Nacional" },
{ "slug": "internacional", "label": "Internacional" },
{ "slug": "selecao", "label": "Seleção" },
{ "slug": "clubes", "label": "Clubes" },
{ "slug": "competicoes", "label": "Competições" },
{ "slug": "resultados", "label": "Resultados" }
],
"bridge_endpoint": "http://127.0.0.1:3200",
"wp_endpoint": "http://127.0.0.1:8081/wp-json",
"locale": "pt-PT",
"timezone": "Europe/Lisbon",
"ads_enabled": false,
"analytics_id": null
}
```
Live file: `config/domains/andebol.net.json`. The HTML document language uses the first segment of `locale` (e.g. `pt-PT` → `<html lang="pt">` in `Base.astro`).
**Local smoke test:** the dev server does not read a `HOST` env var for the HTTP `Host` header. Map the hostname and open the site with that host (or send the header manually):
- Add `127.0.0.1 andebol.net` to `/etc/hosts`, run `npm run dev` in `frontend/`, then open `http://andebol.net:4321/`.
- Or: `curl -sI -H 'Host: andebol.net' http://127.0.0.1:4321/` once the dev server is up (response should reflect institutional branding / site name from the JSON).
---
## Config Schema
Shape matches `DomainConfig` in `frontend/src/types/index.ts`:
```jsonc
{
"site_name": "Sports News",
"tagline": "The best sports coverage in Portugal",
// Must match a registered theme: folder frontend/src/themes/<theme>/ + entry in src/lib/theme.ts
"theme": "sports",
"logo": "/assets/newsproject/logo.svg",
"favicon": "/assets/newsproject/favicon.ico",
"primary_color": "#e63946",
"secondary_color": "#1d3557",
"accent_color": "#f1faee",
"categories": [
{ "slug": "football", "label": "Football" },
{ "slug": "basket", "label": "Basket" }
],
"bridge_endpoint": "http://127.0.0.1:3200",
"wp_endpoint": "http://127.0.0.1:8080/wp-json",
"locale": "pt-PT",
"timezone": "Europe/Lisbon",
"ads_enabled": false,
"analytics_id": null
}
```
---
## Current Domains
| Domain | Config file | Notes |
|--------|-------------|--------|
| `newsproject.bitreport.pt` | `newsproject.bitreport.pt.json` | `theme`: `sports` |
| `andebol.net` | `andebol.net.json` | `theme`: `institutional`; `wp_endpoint`: `:8081`; andebol news automation |
| Local dev | `localhost.json` | Fallback when hostname file is missing |
---
## Domain Resolution in Astro
Configs live in the monorepo at `config/domains/`. The loader discovers that directory by walking up from `import.meta.dirname` and from `process.cwd()` until it finds a `config/domains/localhost.json` marker (same idea as the bridge). Override with `DOMAINS_CONFIG_PATH` or `DOMAIN_CONFIG_DIR` if the process working directory is not inside the monorepo checkout.
It then loads `hostname.json`, falls back to `localhost.json`, then to a minimal in-code default if both are missing or invalid.
Usage in an Astro page:
```astro
---
import { getDomainConfig } from "../lib/domain.js";
import { getThemeComponents } from "../lib/theme.js";
const host = Astro.request.headers.get("host") ?? "localhost";
const config = getDomainConfig(host);
const { Header, Footer } = getThemeComponents(config.theme);
---
```
---
## Notes
- Domain config files are **not secrets** — they contain only public-facing site settings
- WordPress endpoints in `wp_endpoint` are internal network addresses; they must not be exposed publicly
- **`theme`**: folder under `frontend/src/themes/` **plus** registration in `frontend/src/lib/theme.ts`; unknown ids fall back to **`sports`**