diff --git a/config/domains/andebol.net.json b/config/domains/andebol.net.json new file mode 100644 index 0000000..d99919a --- /dev/null +++ b/config/domains/andebol.net.json @@ -0,0 +1,24 @@ +{ + "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 +} diff --git a/config/domains/fpa-example.json b/config/domains/fpa-example.json new file mode 100644 index 0000000..e71f1ab --- /dev/null +++ b/config/domains/fpa-example.json @@ -0,0 +1,23 @@ +{ + "site_name": "FPA Example", + "tagline": "Institutional news and announcements — demo configuration", + "theme": "institutional", + "logo": "/assets/logo.svg", + "favicon": "/favicon.svg", + "primary_color": "#1a3a5c", + "secondary_color": "#4a6fa5", + "accent_color": "#f5f5f5", + "categories": [ + { "slug": "federations", "label": "Federations" }, + { "slug": "news", "label": "News" }, + { "slug": "events", "label": "Events" }, + { "slug": "results", "label": "Results" }, + { "slug": "media", "label": "Media" } + ], + "bridge_endpoint": "http://localhost:3200", + "wp_endpoint": "http://localhost:8080/wp-json", + "locale": "en-US", + "timezone": "Europe/Lisbon", + "ads_enabled": false, + "analytics_id": null +} diff --git a/deploy/scripts/smoke-andebol.sh b/deploy/scripts/smoke-andebol.sh new file mode 100755 index 0000000..082650b --- /dev/null +++ b/deploy/scripts/smoke-andebol.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +# Smoke tests for andebol.net (or local Nginx on the CT). +# Usage: +# BASE_URL=https://andebol.net ./deploy/scripts/smoke-andebol.sh +# BASE_URL=http://127.0.0.1 HOST=andebol.net ./deploy/scripts/smoke-andebol.sh # on the LXC +set -euo pipefail + +BASE_URL="${BASE_URL:-https://andebol.net}" +BASE_URL="${BASE_URL%/}" +HOST_HEADER="${HOST:-andebol.net}" + +fail() { echo "SMOKE FAIL: $*" >&2; exit 1; } +ok() { echo "OK: $*"; } + +code_for() { + curl -fsS -o /dev/null -w '%{http_code}' --max-time 25 -H "Host: $HOST_HEADER" "$1" +} + +log_json() { + curl -fsS --max-time 15 -H "Host: $HOST_HEADER" "$1" | head -c 400 + echo "" +} + +# Homepage (Astro SSR with institutional theme) +_c="$(code_for "$BASE_URL/")" +[[ "$_c" == "200" ]] || fail "GET $BASE_URL/ expected 200, got $_c (andebol.net homepage)" +ok "GET / → $_c" + +# Bridge health +_c="$(code_for "$BASE_URL/health")" +[[ "$_c" == "200" ]] || fail "GET $BASE_URL/health expected 200, got $_c" +_body="$(log_json "$BASE_URL/health")" +echo "$_body" | grep -q '"status"' || fail "GET /health body missing JSON status field" +ok "GET /health → $_c" + +# WordPress REST API (port 8081 via nginx) +_c="$(code_for "$BASE_URL/wp-json/wp/v2/")" +[[ "$_c" == "200" ]] || fail "GET $BASE_URL/wp-json/wp/v2/ expected 200, got $_c (WP andebol :8081)" +ok "GET /wp-json/wp/v2/ → $_c" + +echo "All smokes passed for BASE_URL=$BASE_URL (host: $HOST_HEADER)" diff --git a/docs/domains.md b/docs/domains.md index 306c99d..c4a72e1 100644 --- a/docs/domains.md +++ b/docs/domains.md @@ -1,59 +1,137 @@ # Multi-Domain Configuration -> **Status:** Stub — implementation in progress (BIT-345) +> **Status:** Active — domain JSON + theme selection (see also [frontend.md](./frontend.md) for theme authoring). --- ## Overview -The platform supports multiple domains without code changes. 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. +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//`. 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/.json` (see schema below) -2. Configure DNS to point the domain at the server -3. If WordPress is a new instance, deploy it and update `wp_endpoint` -4. No code change. No redeploy. (In SSR mode, Astro reads configs from disk per request.) +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` → `` 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 { - // Identity "site_name": "Sports News", - "site_description": "The best sports coverage in Portugal", + "tagline": "The best sports coverage in Portugal", + + // Must match a registered theme: folder frontend/src/themes// + entry in src/lib/theme.ts + "theme": "sports", - // Visual - "theme": "sports", // maps to src/themes// "logo": "/assets/newsproject/logo.svg", "favicon": "/assets/newsproject/favicon.ico", "primary_color": "#e63946", + "secondary_color": "#1d3557", + "accent_color": "#f1faee", - // Navigation - "categories": ["Football", "Basket", "F1", "Tennis", "Rugby"], - "nav_categories": ["Football", "Basket", "F1"], + "categories": [ + { "slug": "football", "label": "Football" }, + { "slug": "basket", "label": "Basket" } + ], - // Backend endpoints - "wp_endpoint": "http://wp.newsproject.local/wp-json", - "bridge_endpoint": "http://bridge:3200", + "bridge_endpoint": "http://127.0.0.1:3200", + "wp_endpoint": "http://127.0.0.1:8080/wp-json", - // Locale - "locale": "pt_PT", + "locale": "pt-PT", "timezone": "Europe/Lisbon", - // Social - "social": { - "twitter": "@handle", - "facebook": "page-name" - }, - - // Features (future) - "ads": { "enabled": false, "provider": null }, - "analytics": { "ga4": null } + "ads_enabled": false, + "analytics_id": null } ``` @@ -61,45 +139,31 @@ The platform supports multiple domains without code changes. Each domain has a J ## Current Domains -| Domain | Config file | WordPress | -|--------|-------------|-----------| -| `newsproject.bitreport.pt` | `newsproject.bitreport.pt.json` | `http://wp.newsproject.local` | +| 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 -```typescript -// src/lib/domain.ts -import { readFileSync, existsSync } from "node:fs"; -import path from "node:path"; +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. -export function loadDomainConfig(host: string): DomainConfig { - const hostname = host.replace(/:\d+$/, ""); - const configPath = path.join(process.cwd(), "config/domains", `${hostname}.json`); - - if (!existsSync(configPath)) { - throw new Error(`Domain config not found: ${hostname}`); - } - - return JSON.parse(readFileSync(configPath, "utf-8")) as DomainConfig; -} -``` +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 --- -// src/pages/index.astro -import { loadDomainConfig } from "../lib/domain"; +import { getDomainConfig } from "../lib/domain.js"; +import { getThemeComponents } from "../lib/theme.js"; const host = Astro.request.headers.get("host") ?? "localhost"; -const config = loadDomainConfig(host); +const config = getDomainConfig(host); +const { Header, Footer } = getThemeComponents(config.theme); --- - - {config.site_name} - ... - ``` --- @@ -108,4 +172,4 @@ const config = loadDomainConfig(host); - 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 names map to a directory under `frontend/src/themes/`; if a theme is missing, the default theme is used +- **`theme`**: folder under `frontend/src/themes/` **plus** registration in `frontend/src/lib/theme.ts`; unknown ids fall back to **`sports`** diff --git a/docs/frontend.md b/docs/frontend.md index 94d93ed..80a1ceb 100644 --- a/docs/frontend.md +++ b/docs/frontend.md @@ -1,6 +1,6 @@ # Frontend — Astro 5 SSR -> **Status:** Stub — implementation in progress (BIT-345) +> **Status:** Active — SSR, multi-domain config, pluggable themes under `src/themes/`. --- @@ -8,6 +8,8 @@ The frontend is an Astro 5 SSR application. It renders all pages server-side, resolves the active domain config at request time, fetches data from the Fastify bridge, and returns complete HTML to the client. +**Themes** swap header, footer, and homepage shells (hero, trending, category blocks, article cards) without duplicating routes. Domain JSON field `theme` selects which component set loads; see [domains.md — Choosing a Theme](./domains.md#choosing-a-theme). + --- ## Tech Stack @@ -28,34 +30,39 @@ The frontend is an Astro 5 SSR application. It renders all pages server-side, re ``` frontend/ ├── src/ +│ ├── layouts/ +│ │ └── Base.astro # HTML shell, OG/meta, theme CSS hooks, Header/Footer from theme │ ├── pages/ -│ │ ├── index.astro # Homepage -│ │ ├── [category]/ -│ │ │ ├── index.astro # Category listing -│ │ │ └── [slug].astro # Article page -│ │ ├── search.astro # Search results -│ │ ├── sitemap.xml.ts # Dynamic sitemap -│ │ └── robots.txt.ts # Dynamic robots.txt -│ ├── components/ -│ │ ├── layout/ +│ │ ├── index.astro # Homepage (uses themed blocks + LatestGrid) +│ │ ├── category/[slug].astro +│ │ ├── article/[slug].astro +│ │ ├── sitemap.xml.ts +│ │ └── robots.txt.ts +│ ├── themes/ # One subfolder per theme id (must match domain JSON + theme.ts) +│ │ ├── sports/ │ │ │ ├── Header.astro │ │ │ ├── Footer.astro -│ │ │ └── BaseLayout.astro -│ │ ├── article/ +│ │ │ ├── HeroSection.astro │ │ │ ├── ArticleCard.astro -│ │ │ ├── ArticleBody.astro -│ │ │ └── ArticleMeta.astro +│ │ │ ├── CategorySection.astro +│ │ │ └── TrendingBar.astro +│ │ ├── institutional/ +│ │ │ ├── … (same six components) +│ │ │ └── theme.css # optional token overrides +│ │ └── README.md # short in-repo contract summary +│ ├── components/ # shared, non-themed pieces │ │ ├── home/ -│ │ │ ├── HeroBlock.astro -│ │ │ ├── TrendingBlock.astro -│ │ │ └── CategoryBlock.astro -│ │ └── islands/ -│ │ ├── SearchWidget.tsx # React island (client:load) -│ │ └── MobileMenu.tsx # React island (client:load) +│ │ │ └── LatestGrid.astro # picks ArticleCard via getThemeComponents(theme) +│ │ └── article/ +│ │ ├── ArticleHeader.astro +│ │ └── RelatedArticles.astro │ ├── lib/ -│ │ ├── domain.ts # Domain config loader +│ │ ├── domain.ts # Domain config loader (config/domains/*.json) +│ │ ├── theme.ts # Theme registry + getThemeComponents() │ │ ├── bridge.ts # Bridge API client -│ │ └── seo.ts # Meta tag helpers +│ │ └── format.ts +│ ├── types/ +│ │ └── index.ts # DomainConfig, ArticleListItem, etc. │ └── styles/ │ └── global.css ├── astro.config.mjs @@ -65,9 +72,66 @@ frontend/ --- +## Theme Authoring + +Use this when adding a third theme or adjusting an existing one. The canonical one-page summary also lives in `frontend/src/themes/README.md`. + +### Directory layout + +Create `frontend/src/themes//` with **exactly these six** Astro modules at the theme root (names matter — `theme.ts` imports them by path): + +| File | Responsibility | Props (contract) | +|------|----------------|------------------| +| `Header.astro` | Site header and primary nav | `config: DomainConfig` | +| `Footer.astro` | Site footer | `config: DomainConfig` | +| `HeroSection.astro` | Homepage hero | `articles: ArticleListItem[]` (bridge `homepage.hero`) | +| `TrendingBar.astro` | Trending strip | `articles: ArticleListItem[]` (`homepage.trending`) | +| `CategorySection.astro` | One homepage category block | `category: CategoryConfig`, `articles: ArticleListItem[]` | +| `ArticleCard.astro` | Article teaser | `article: ArticleListItem`, optional `size?: 'sm' \| 'md' \| 'lg'`, optional `show_excerpt?: boolean` (default `true`) | + +Types are defined in `frontend/src/types/index.ts`. Callers (e.g. `pages/index.astro`, `components/home/LatestGrid.astro`) assume this shape; keep field usage aligned with `ArticleListItem` (`slug`, `title`, `excerpt`, `featured_image`, `featured_image_alt`, `author`, `category`, `published_at`, `reading_time`, flags, etc.). + +**Self-contained themes:** inside a theme folder, import sibling components with relative paths (e.g. `./ArticleCard.astro`), not `~/themes/other/…`. + +### CSS tokens and `theme.css` + +`layouts/Base.astro` injects **global** custom properties from domain config: + +- `--color-primary` ← `config.primary_color` +- `--color-secondary` ← `config.secondary_color` +- `--color-accent` ← `config.accent_color` + +Themes should use these variables in CSS where possible so domains can rebrand without code changes. + +Optional **`theme.css`** in the theme folder can override or extend tokens. Example from `institutional/theme.css`: + +- `:root` — `--color-primary`, `--color-secondary`, `--color-accent`, `--font-heading` +- `[data-theme="dark"]` — dark-mode tweaks for the same variables + +**Wiring:** `Base.astro` currently loads `institutional/theme.css` only when `config.theme === 'institutional'`. If you add another theme that needs its own sheet, add a matching `` (or shared import) in `Base.astro` for that theme id. + +### Registering a theme in `src/lib/theme.ts` + +Astro cannot `import()` a `.astro` file from a runtime string. Every theme must be: + +1. **Imported** at the top of `theme.ts` (one import per component, or grouped by theme). +2. **Declared** on the `ThemeId` union type (e.g. `'sports' | 'institutional' | 'mytheme'`). +3. **Mapped** in `THEME_BY_ID: Record` with all six components. + +`getThemeComponents(theme: string)` normalizes the string to a `ThemeId` and falls back to **`sports`** if the id is unknown. + +**Checklist for a new theme `mytheme`:** + +1. Add folder `src/themes/mytheme/` with the six required `.astro` files (and optional `theme.css`). +2. Extend `ThemeId` and add `mytheme` to `THEME_BY_ID`. +3. If using `theme.css`, load it from `Base.astro` when `config.theme === 'mytheme'`. +4. Set `"theme": "mytheme"` in the domain JSON for sites that should use it. + +--- + ## Domain Resolution -See [architecture.md#4-multi-domain-routing](./architecture.md#4-multi-domain-routing). +See [architecture.md#4-multi-domain-routing](./architecture.md#4-multi-domain-routing) and [domains.md](./domains.md). --- diff --git a/frontend/src/components/article/RelatedArticles.astro b/frontend/src/components/article/RelatedArticles.astro index 8d264aa..60c7e38 100644 --- a/frontend/src/components/article/RelatedArticles.astro +++ b/frontend/src/components/article/RelatedArticles.astro @@ -1,11 +1,13 @@ --- import type { ArticleListItem } from '../../types/index.js'; -import ArticleCard from '../shared/ArticleCard.astro'; +import { getThemeComponents } from '../../lib/theme.js'; interface Props { + theme: string; articles: ArticleListItem[]; } -const { articles } = Astro.props; +const { theme, articles } = Astro.props; +const { ArticleCard } = getThemeComponents(theme); --- {articles.length > 0 && (