Custom Domains via Caddy reverse proxy [DONE] `PL-T232`
I korthet
Custom domain support powered by Cloudflare for SaaS — clubs and federations attach their own domain (www.federation.fr, klubb.se), copy-paste DNS instructions, and get auto-provisioned and auto-renewed DV TLS within minutes. A reconciliation job polls every five minutes through pending → pending_validation → active states, the admin UI surfaces clear status badges and copy buttons, and a wildcard `*.web.petanque.life` subdomain stays available as a permanent fallback.
Så fungerar det
When a tenant adds a custom domain, the API creates a CmsDomain document and calls the Cloudflare Custom Hostnames API to register the hostname against the platform's SaaS zone. Cloudflare returns DNS instructions: a CNAME pointing the customer hostname at `*.web.petanque.life` and a TXT record for ownership proof. The admin UI presents these in copy-ready boxes with a one-click copy button so non-technical editors can hand them to their registrar without confusion.
Status flows through a state machine: `pending` (just created, waiting for DNS) → `pending_validation` (CNAME and TXT detected, awaiting Cloudflare's validation) → `active` (certificate issued, traffic flowing). A reconciliation job polls the Cloudflare API every five minutes for any domain not in a terminal state, updates local status, and marks domains as `failed` after a 72-hour timeout so stale entries don't accumulate. Cloudflare handles certificate issuance via free DV certs and automatic renewal — the platform never touches private keys.
The renderer's middleware looks up the incoming Host header against the CmsDomain collection, resolves it to a CmsSite, and serves the right tenant's content; the same site can have multiple custom domains plus its platform subdomain pointing at the same content. The wildcard `*.web.petanque.life` fallback works without any Cloudflare registration, so a freshly created site is reachable instantly while the editor optionally configures a custom domain. Admin can remove a domain at any time; the system deletes the Cloudflare hostname and the next request to that domain falls back to the platform subdomain.
Audit trail captures every domain action — registration, validation, removal — for security and compliance review.
Centrala funktioner
- Add custom hostname with one API call to Cloudflare Custom Hostnames
- Editor-friendly DNS instructions (CNAME + TXT) with copy buttons
- State machine: pending → pending_validation → active with 72h failure timeout
- Auto-provisioned and auto-renewed DV TLS certificates via Cloudflare
- Background reconciliation job polling every five minutes
- Multiple domains per site, all resolving to the same content
- Wildcard `*.web.petanque.life` fallback always available, no DNS required
I praktiken
Erik, secretary at Norrlands BK, decides the club is ready to move from norrlands-bk.petanque-life.com to its newly-purchased domain norrlandbk.se. He opens the Custom Domains tab, types the domain, and clicks 'Add'. The status flips to pending and two boxes appear with the CNAME and TXT records.
He emails them to his registrar's support since he doesn't run his own DNS. Three hours later DNS propagates; the reconciliation job picks it up on its next poll and the status moves to pending_validation. Six minutes later Cloudflare confirms ownership, issues the certificate and the status flips to active.
Erik visits norrlandbk.se in his browser; the site loads with a green padlock. The platform subdomain still resolves so any printed material with the old URL keeps working forever.
Features i detta subsystem
6| ID | Status | Funktioner |
|---|---|---|
| F18.14.01 | Levererad | Custom hostname-registrering via CustomDomain-modell — PL-T232 ✅ PL-T232 |
| F18.14.02 | Levererad | CNAME/TXT-valideringsflöde med dig-baserad DNS-lookup — PL-T232 ✅ PL-T232 |
| F18.14.03 | Levererad | Automatisk TLS-provisionering via Caddy + Let's Encrypt HTTP-01 — PL-T232 ✅ PL-T232 |
| F18.14.04 | Levererad | /public/domains/cert-allowed-gate mot Let's Encrypt-abuse — PL-T232 ✅ PL-T232 |
| F18.14.05 | Levererad | Admin-UI för domänhantering med CNAME-target proxy.web.petanque.life — PL-T232 ✅ PL-T232 |
| F18.14.06 | Levererad | Plattform-subdomän-konvention <slug>.web.petanque.life — PL-T232 (auto-DNS via PL-T235) ✅ PL-T232 |