Website initialization¶
POST /admin/v1/websites (AISD-159) stands up a complete-but-empty CMS website
in one call: it creates the Site row and default settings (including
presentation via template_style_id when provided) and then attaches the
resolved host to the already-deployed multi-site Cloudflare worker (AISD-157).
It lives in cms_api.admin.routes.websites and inherits the block-level
validate_admin gate from main.py -- there is no per-route auth dependency
(see Auth flow).
End to end¶
sequenceDiagram
participant C as Admin client
participant API as POST /admin/v1/websites
participant DB as cms_db (Site, SiteSetting)
participant CF as Cloudflare API
C->>API: WebsiteInitRequest {domain, acronym, name, target}
API->>API: resolve_target_host (main-domain guard)
API->>API: host_in_zone? (else 422 domain_out_of_zone)
API->>DB: store.init_website(payload, host)
DB-->>API: (Site, created) or 424 / 409
API->>CF: PUT /accounts/{id}/workers/domains (custom domain upsert)
CF-->>API: 2xx (attached) or "already" (present) or error (502)
API-->>C: WebsiteInitResult
Step 1 (the Site row + settings) is committed before step 2
(the Cloudflare attach). If the Cloudflare call fails, the Site persists and
the response is a 502 whose details report the created site_id; an
idempotent retry of the same request heals the binding. There is no
cross-system transaction by design.
Request¶
WebsiteInitRequest (cms_api.admin.schemas.models):
| Field | Type | Required | Notes |
|---|---|---|---|
domain |
str |
yes | A bare hostname (no scheme, no path, no whitespace) -- validated against a hostname-shape regex at the schema level. Zone membership is checked separately in the route. |
acronym |
str |
yes | The site's short identifier; used together with the resolved host to detect an idempotent replay vs. a conflict. |
name |
str |
yes | Site display name. |
meta_title |
str \| null |
no | -- |
meta_description |
str \| null |
no | -- |
default_language |
str \| null |
no | -- |
template_id |
int \| null |
no | Accepted for backward compatibility only — not written to sites (column dropped AISD-226). Presentation uses the global/default template_style_id pairing. |
target |
"dev" \| "prod" |
no, default "dev" |
See dev vs. prod below. |
Response¶
WebsiteInitResult:
| Field | Type | Notes |
|---|---|---|
site_id |
int |
The persisted (or matched) Site.id. |
acronym |
str |
Echoes the site's acronym. |
host |
str |
The resolved host that was attached to Cloudflare (dev.<domain> or the bare domain). |
domain |
str |
The persisted Site.domain (equal to host). |
created |
bool |
false when the call matched an existing site (idempotent replay) rather than creating a new row. |
dns_status |
str |
"provisioned" on a fresh Cloudflare upsert, "present" when the host was already attached. |
worker_binding_status |
str |
"attached" on a fresh upsert, "present" when already bound. |
live_url |
str |
https://<host>. |
Dev vs. prod and the main-domain guard¶
target defaults to "dev". resolve_target_host enforces a main-domain
guard:
target="dev"(default): the resolved host is alwaysdev.<bare domain>. An already-dev.-prefixed input is not double-prefixed, but a dev target can never resolve to the bare apex.target="prod": the resolved host is the bare domain itself. This is the only way to provision the main domain -- there is no way to reach it by accident.
The resolved host must then belong to the managed Cloudflare zone
(fashionradar.net): it must equal the zone apex or be a subdomain of it (dot
boundary, so a look-alike suffix like notfashionradar.net is rejected). A
host outside the zone is rejected with 422 before any DB or Cloudflare
call is made:
{
"error": {
"code": "domain_out_of_zone",
"message": "Resolved host 'dev.example.com' is outside the managed Cloudflare zone 'fashionradar.net'; only hosts within 'fashionradar.net' can be provisioned",
"details": {"host": "dev.example.com", "zone": "fashionradar.net"}
}
}
Idempotency¶
Re-running the same (domain, acronym, target) request returns the existing
site (created=false) rather than duplicating it, and still confirms the
Cloudflare binding (dns_status/worker_binding_status come back "present"
instead of "provisioned"/"attached"). The store looks up the resolved host
and the acronym separately and reconciles the pair, rather than running a
single domain == host OR acronym == acronym query (which could match two
different rows and raise an ambiguous 500, or match on only one condition):
| Host match | Acronym match | Result |
|---|---|---|
| none | none | create a new Site |
| same site | same site | idempotent replay (created=false) |
| different sites, or exactly one matches | -- | 409 website_conflict |
Error cases¶
| Status | code |
When |
|---|---|---|
422 |
domain_out_of_zone |
The resolved host is outside the configured Cloudflare zone. |
424 |
default_preset_missing |
The id=-1 Template or Style default preset has not been seeded yet. details.missing lists which of template:-1 / style:-1 is absent. |
409 |
website_conflict |
The resolved host and the acronym identify different sites (or exactly one of them is already claimed). |
502 |
cloudflare_config_missing |
The /common/cloudflare secret is missing, unreadable, or has no account token. |
502 |
cloudflare_error |
The Cloudflare zone lookup (including a malformed response with no matching zone or account id) or the custom-domain attach call failed for any other reason. details never include the token. |
A 424/409 is raised before the Cloudflare client is built at all -- an
invalid or conflicting request never touches Secrets Manager or the Cloudflare
API (cloudflare_factory is only invoked after every store guard has passed).
Cloudflare configuration¶
Provisioning goes through cms_api.admin.cloudflare.CloudflareClient, which
attaches the host to the multi-site worker using the Workers Custom Domain
API (PUT /accounts/{account_id}/workers/domains) -- one idempotent upsert
that provisions the DNS record, the TLS certificate, and the worker binding
together.
- Credentials: just one Cloudflare account's
token, read fromaccounts[0]of the/common/cloudflareSecrets Manager secret and loaded lazily viafs_config.get_secretonly once the request has passed every validation and store guard. The secret carries only{mail, token, base_url}per account -- there is no account-id field and no SSM parameter to configure. - Zone and account id: both resolved (and cached on the client instance)
from a single
GET /zones?name=fashionradar.net-- the response yields thezone_idand the owningaccount.id. The account id is derived from the zone rather than stored, soPUT /accounts/{account_id}/workers/domainsneeds nothing beyond the token. A malformed zone response (no matching zone, or a result missingid/account.id) raisescloudflare_error(502), never a raw 500. - Worker script: the constant
DEFAULT_WORKER_SCRIPT(cms-cf-worker-dev) -- the multi-site worker deployed fordev.fashionradar.net(seecms-cf-worker/wrangler.toml,[env.site-dev]). There is no per-environment override. - Idempotent by design: a Cloudflare response carrying error code
100117(workers.api.error.origin_conflict_existing_dns_record), or any error message containing"already"as a fallback, is treated as success (dns_status="present",worker_binding_status="present") rather than an error. - Secrets are never logged, and never appear in a raised error's message
or
details-- Cloudflare error bodies are joined fromerrors[].messageonly.
This endpoint depends on two prerequisites already being in place:
- the id=-1 default preset (AISD-158), or
init_websitefails with424; - the multi-site Cloudflare worker itself already deployed (AISD-157) -- this endpoint only attaches a host to it, it does not deploy the worker.
Example¶
# 1. Authenticate (OAuth2 password grant, form-urlencoded).
curl -s -X POST https://cms-api-stg.skyneto.com/admin/v1/auth/login \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'username=admin@example.com&password=<password>'
# -> {"access_token": "<jwt>", "refresh_token": "...", "token_type": "bearer", "expires_in": 86400}
# 2. Initialize a dev website.
curl -s -X POST https://cms-api-stg.skyneto.com/admin/v1/websites \
-H 'Authorization: Bearer <jwt>' \
-H 'Content-Type: application/json' \
-d '{
"domain": "fashionradar.net",
"acronym": "fr",
"name": "Fashion Radar"
}'
# -> {
# "site_id": 42,
# "acronym": "fr",
# "host": "dev.fashionradar.net",
# "domain": "dev.fashionradar.net",
# "created": true,
# "dns_status": "provisioned",
# "worker_binding_status": "attached",
# "live_url": "https://dev.fashionradar.net"
# }
To provision the bare production domain, pass "target": "prod" explicitly in
the request body -- that is the only path that can ever resolve to the apex.