diff --git a/README.md b/README.md index 4baf8bab3e..fe528e761a 100644 --- a/README.md +++ b/README.md @@ -205,14 +205,16 @@ command `npm run build` and the deploy command `npm run deploy`. Each Worker set the `ARTCRAFT_APP` build variable to choose what [`cloudflare_app.sh`](./script/website/cloudflare_app.sh) builds: -| Worker | `ARTCRAFT_APP` | Hostname | Wrangler config | Build secrets | -|---------------------|------------------------------|-----------------------|------------------------------------------------|--------------------------------------------------------------| -| `artcraft-services` | `artcraft-website` (default) | `getartcraft.com` | `wrangler.jsonc` | `LAUNCHER_CATALOG_SIGNING_KEY`; optional `LINUX_SIGNING_KEY` | -| `artcraft-webapp` | `artcraft-webapp` | `app.getartcraft.com` | `frontend/apps/artcraft-webapp/wrangler.jsonc` | none | +| Worker | `ARTCRAFT_APP` | Hostname | Wrangler config | Build secrets | +|----------------------|------------------------------|---------------------------|---------------------------------------------------|--------------------------------------------------------------| +| `artcraft-services` | `artcraft-website` (default) | `getartcraft.com` | `wrangler.jsonc` | `LAUNCHER_CATALOG_SIGNING_KEY`; optional `LINUX_SIGNING_KEY` | +| `artcraft-webapp` | `artcraft-webapp` | `app.getartcraft.com` | `frontend/apps/artcraft-webapp/wrangler.jsonc` | none | +| `artcraft-dashboard` | `artcraft-dashboard` | `dashboard.artcraft.team` | `frontend/apps/artcraft-dashboard/wrangler.jsonc` | none | The webapp never builds the website, so its Worker needs no signing keys. Its `VITE_*` variables (for example `VITE_GOOGLE_CLIENT_ID`) are build variables, not -runtime secrets. +runtime secrets. The admin dashboard is a standalone pnpm app and sets +`NODE_VERSION=22`; see [its README](./frontend/apps/artcraft-dashboard/README.md). ## Automated Craft Release Version Updates diff --git a/crates/lib/actix_cors_configs/src/allowlist_regression_tests.rs b/crates/lib/actix_cors_configs/src/allowlist_regression_tests.rs index f4798a1505..6f0b6f147e 100644 --- a/crates/lib/actix_cors_configs/src/allowlist_regression_tests.rs +++ b/crates/lib/actix_cors_configs/src/allowlist_regression_tests.rs @@ -65,6 +65,7 @@ const ALLOWLIST: &[(AddOrigins, Env, &str)] = &[ (add_artcraft_3d, Both, "https://brandons-macbook-pro.taild62114.ts.net"), // add_artcraft_admin_dashboard + (add_artcraft_admin_dashboard, Both, "https://dashboard.artcraft.team"), (add_artcraft_admin_dashboard, Both, "https://artcraft-dashboard.netlify.app"), // add_artcraft_desktop diff --git a/crates/lib/actix_cors_configs/src/configs/artcraft_admin_dashboard.rs b/crates/lib/actix_cors_configs/src/configs/artcraft_admin_dashboard.rs index 6b34014143..fbe60e2a1f 100644 --- a/crates/lib/actix_cors_configs/src/configs/artcraft_admin_dashboard.rs +++ b/crates/lib/actix_cors_configs/src/configs/artcraft_admin_dashboard.rs @@ -3,6 +3,9 @@ use crate::util::netlify_branch_domain_matches::netlify_branch_domain_matches; pub fn add_artcraft_admin_dashboard(cors: Cors, _is_production: bool) -> Cors { cors + // Production domain (Cloudflare Worker). + .allowed_origin("https://dashboard.artcraft.team") + // Legacy Netlify project, kept as the rollback target. .allowed_origin("https://artcraft-dashboard.netlify.app") .allowed_origin_fn(|origin, _req_head| { netlify_branch_domain_matches(origin, "artcraft-dashboard.netlify.app") @@ -11,10 +14,18 @@ pub fn add_artcraft_admin_dashboard(cors: Cors, _is_production: bool) -> Cors { #[cfg(test)] mod tests { - use crate::testing::{assert_netlify_project, isolated_cors}; + use crate::testing::{assert_exact_https_origin, assert_netlify_project, isolated_cors}; use super::add_artcraft_admin_dashboard; + #[actix_rt::test] + async fn dashboard_domain() { + for is_production in [true, false] { + let cors = isolated_cors(add_artcraft_admin_dashboard, is_production); + assert_exact_https_origin(&cors, "https://dashboard.artcraft.team").await; + } + } + #[actix_rt::test] async fn netlify_project() { for is_production in [true, false] { diff --git a/crates/lib/actix_cors_configs/src/cors.rs b/crates/lib/actix_cors_configs/src/cors.rs index 08b482179e..61d9071ca5 100644 --- a/crates/lib/actix_cors_configs/src/cors.rs +++ b/crates/lib/actix_cors_configs/src/cors.rs @@ -121,6 +121,7 @@ mod tests { "https://www.getartcraft.com", "https://artcraft.ai", "https://www.artcraft.ai", + "https://dashboard.artcraft.team", "https://artcraft-dashboard.netlify.app", "https://realseedance.com", "https://www.realseedance.com", diff --git a/frontend/apps/artcraft-dashboard/README.md b/frontend/apps/artcraft-dashboard/README.md index 9e8a61708e..de312ed615 100644 --- a/frontend/apps/artcraft-dashboard/README.md +++ b/frontend/apps/artcraft-dashboard/README.md @@ -1,2 +1,50 @@ # ArtCraft Admin Dashboard -Admin dashboard for managing ArtCraft + +Admin dashboard for managing ArtCraft, served at +. + +## Deployment + +Production is a Cloudflare Worker with static assets +([`wrangler.jsonc`](./wrangler.jsonc)). Deploy from the repository root: + +```bash +npm run build:dashboard +npm run deploy:dashboard +``` + +Workers Builds settings for the `artcraft-dashboard` Worker: + +| Setting | Value | +|----------------|-----------------------------------| +| Build command | `npm run build` | +| Deploy command | `npm run deploy` | +| Build variable | `ARTCRAFT_APP=artcraft-dashboard` | +| Build variable | `NODE_VERSION=22` | + +`ARTCRAFT_APP` selects this app in +[`script/website/cloudflare_app.sh`](../../../script/website/cloudflare_app.sh). +Vite 7 needs Node >= 20.19 / 22.12, so `NODE_VERSION=22` is required; it cannot +be pinned in the repository because the rest of the workspace builds on the same +Node version. + +### `/api` proxy + +[`cloudflare_worker/index.ts`](./cloudflare_worker/index.ts) forwards `/api/*` to +`https://api.storyteller.ai`. The dashboard calls the API with +`credentials: "include"`, so the session cookie has to be first-party; a direct +cross-site call would be dropped by browsers. The Worker forwards the browser's +`Origin`, so `https://dashboard.artcraft.team` must stay in the API's CORS +allowlist +([`artcraft_admin_dashboard.rs`](../../../crates/lib/actix_cors_configs/src/configs/artcraft_admin_dashboard.rs)). +The Worker's `*.workers.dev` host is not on that allowlist, so API calls only +work through `dashboard.artcraft.team` — add the test host to the CORS config if +you need to exercise a deployment before attaching the domain. + +### Netlify rollback + +[`netlify.toml`](./netlify.toml) and [`script/netlify_build.sh`](./script/netlify_build.sh) +are kept so the previous site can still build and serve. Netlify's routing rules +live in [`_redirects`](./_redirects) at the app root and are copied into `dist/` +by that script; Cloudflare does not get them, because Workers reject the +`/* /index.html 200` fallback and cannot proxy external hosts from `_redirects`. diff --git a/frontend/apps/artcraft-dashboard/public/_redirects b/frontend/apps/artcraft-dashboard/_redirects similarity index 100% rename from frontend/apps/artcraft-dashboard/public/_redirects rename to frontend/apps/artcraft-dashboard/_redirects diff --git a/frontend/apps/artcraft-dashboard/cloudflare_worker/index.ts b/frontend/apps/artcraft-dashboard/cloudflare_worker/index.ts new file mode 100644 index 0000000000..8789af9e44 --- /dev/null +++ b/frontend/apps/artcraft-dashboard/cloudflare_worker/index.ts @@ -0,0 +1,40 @@ +// Cloudflare Worker for dashboard.artcraft.team. +// +// The dashboard is a static Vite SPA, so the Worker serves `dist/` and only steps +// in for `/api/*`. The dashboard calls the API with `credentials: "include"`, and +// api.storyteller.ai is a different site than dashboard.artcraft.team, so the +// session cookie would be third-party and browsers would drop it. Proxying keeps +// every API call first-party — the same thing Netlify's `/api/*` rewrite did. + +const API_ORIGIN = "https://api.storyteller.ai"; +const API_PREFIX = "/api"; + +// Minimal shape of the static-assets binding declared in wrangler.jsonc. Wrangler +// bundles this file itself, so no worker runtime types are needed here. +interface AssetsBinding { + fetch(request: Request): Promise; +} + +interface Env { + ASSETS: AssetsBinding; +} + +export default { + async fetch(request: Request, env: Env): Promise { + const url = new URL(request.url); + + if (url.pathname === API_PREFIX || url.pathname.startsWith(`${API_PREFIX}/`)) { + return fetch(proxyToApi(request, url)); + } + + return env.ASSETS.fetch(request); + }, +}; + +function proxyToApi(request: Request, url: URL): Request { + const upstream = new URL(API_ORIGIN); + upstream.pathname = url.pathname.slice(API_PREFIX.length) || "/"; + upstream.search = url.search; + + return new Request(upstream, request); +} diff --git a/frontend/apps/artcraft-dashboard/netlify.toml b/frontend/apps/artcraft-dashboard/netlify.toml index ff48b42250..b204223be3 100644 --- a/frontend/apps/artcraft-dashboard/netlify.toml +++ b/frontend/apps/artcraft-dashboard/netlify.toml @@ -1,5 +1,7 @@ # Netlify configuration for the ArtCraft admin dashboard. # +# Retained only as the rollback target: production now serves from the Cloudflare +# Worker at dashboard.artcraft.team (see README.md). # Set up the Netlify site exactly like the other apps in this workspace: # * Base directory: frontend (the Nx workspace root) # * Build command: bash ./apps/artcraft-dashboard/script/netlify_build.sh diff --git a/frontend/apps/artcraft-dashboard/script/build_cloudflare.sh b/frontend/apps/artcraft-dashboard/script/build_cloudflare.sh new file mode 100755 index 0000000000..1f27eb9f7e --- /dev/null +++ b/frontend/apps/artcraft-dashboard/script/build_cloudflare.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +set -euxo pipefail + +# Build the ArtCraft admin dashboard for Cloudflare Workers. +# +# Run from anywhere: this builds inside the app's own folder, which is deliberate +# (see netlify_build.sh) — the dashboard is a standalone pnpm app with React 19 + +# Vite 7, outside the Nx/npm workspace. + +cd "$(dirname "$0")/.." + +echo "Building artcraft-dashboard (standalone pnpm build)" + +# Use the pnpm version pinned in package.json's `packageManager` field, via +# corepack. Disable the interactive download prompt so CI never blocks waiting +# for confirmation. +export COREPACK_ENABLE_DOWNLOAD_PROMPT=0 + +corepack pnpm install --frozen-lockfile +corepack pnpm build + +echo "Final build files:" +find dist/ diff --git a/frontend/apps/artcraft-dashboard/script/netlify_build.sh b/frontend/apps/artcraft-dashboard/script/netlify_build.sh index a66a01fb39..7648d32806 100755 --- a/frontend/apps/artcraft-dashboard/script/netlify_build.sh +++ b/frontend/apps/artcraft-dashboard/script/netlify_build.sh @@ -28,6 +28,12 @@ export COREPACK_ENABLE_DOWNLOAD_PROMPT=0 corepack pnpm install --frozen-lockfile corepack pnpm build +# Netlify's routing rules live at the app root instead of public/, because the +# Cloudflare build must not upload them: Workers reject the `/* /index.html 200` +# SPA fallback as a loop and cannot proxy external hosts from _redirects. +echo "Copying Netlify _redirects into dist" +cp _redirects dist/_redirects + echo "Final build files:" find dist/ diff --git a/frontend/apps/artcraft-dashboard/tsconfig.app.json b/frontend/apps/artcraft-dashboard/tsconfig.app.json index cf8de40605..cb7b4d7ecf 100644 --- a/frontend/apps/artcraft-dashboard/tsconfig.app.json +++ b/frontend/apps/artcraft-dashboard/tsconfig.app.json @@ -28,5 +28,9 @@ "noFallthroughCasesInSwitch": true, "noUncheckedSideEffectImports": true }, - "include": ["src"] + "include": ["src"], + // Specs are not part of the app bundle and this app has no test runner of its + // own; without this they only type-check when the parent workspace happens to + // be installed (which the standalone Cloudflare build does not do). + "exclude": ["src/**/*.spec.ts", "src/**/*.spec.tsx"] } diff --git a/frontend/apps/artcraft-dashboard/wrangler.jsonc b/frontend/apps/artcraft-dashboard/wrangler.jsonc new file mode 100644 index 0000000000..c42c4b28c3 --- /dev/null +++ b/frontend/apps/artcraft-dashboard/wrangler.jsonc @@ -0,0 +1,23 @@ +{ + // Static-assets Worker for dashboard.artcraft.team. The only server-side code + // is the same-origin /api proxy in cloudflare_worker/index.ts; everything else + // is served from dist/. Build and deploy from the repository root with + // ARTCRAFT_APP=artcraft-dashboard (see script/website/cloudflare_app.sh). + "name": "artcraft-dashboard", + "main": "./cloudflare_worker/index.ts", + "compatibility_date": "2026-11-17", + "assets": { + "directory": "./dist", + "binding": "ASSETS", + // React Router owns every path that is not a file in dist/. + "not_found_handling": "single-page-application", + // Everything except API calls is a static file; only /api needs the Worker. + "run_worker_first": ["/api/*"] + }, + "routes": [ + { + "pattern": "dashboard.artcraft.team", + "custom_domain": true + } + ] +} diff --git a/package.json b/package.json index 6956290f30..fae7bbe383 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,8 @@ "deploy": "bash script/website/cloudflare_app.sh deploy", "build:website": "ARTCRAFT_APP=artcraft-website bash script/website/cloudflare_app.sh build", "build:webapp": "ARTCRAFT_APP=artcraft-webapp bash script/website/cloudflare_app.sh build", - "deploy:webapp": "ARTCRAFT_APP=artcraft-webapp bash script/website/cloudflare_app.sh deploy" + "deploy:webapp": "ARTCRAFT_APP=artcraft-webapp bash script/website/cloudflare_app.sh deploy", + "build:dashboard": "ARTCRAFT_APP=artcraft-dashboard bash script/website/cloudflare_app.sh build", + "deploy:dashboard": "ARTCRAFT_APP=artcraft-dashboard bash script/website/cloudflare_app.sh deploy" } } diff --git a/script/website/cloudflare_app.sh b/script/website/cloudflare_app.sh index 416ecd82c0..58c7970aaa 100755 --- a/script/website/cloudflare_app.sh +++ b/script/website/cloudflare_app.sh @@ -10,6 +10,7 @@ # # artcraft-website (default) getartcraft.com, root wrangler.jsonc # artcraft-webapp app.getartcraft.com, frontend/apps/artcraft-webapp/wrangler.jsonc +# artcraft-dashboard dashboard.artcraft.team, frontend/apps/artcraft-dashboard/wrangler.jsonc # # The webapp never builds artcraft-website-next, so it needs none of the website's # build secrets (LAUNCHER_CATALOG_SIGNING_KEY, and the optional LINUX_SIGNING_KEY). @@ -36,8 +37,16 @@ case "${APP}:${ACTION}" in artcraft-webapp:deploy) npx --yes wrangler@4 deploy --config frontend/apps/artcraft-webapp/wrangler.jsonc ;; + artcraft-dashboard:build) + # Standalone pnpm app outside the Nx/npm workspace; the script installs and + # builds inside its own folder. + bash frontend/apps/artcraft-dashboard/script/build_cloudflare.sh + ;; + artcraft-dashboard:deploy) + npx --yes wrangler@4 deploy --config frontend/apps/artcraft-dashboard/wrangler.jsonc + ;; *) - echo "Usage: ARTCRAFT_APP= $0 " >&2 + echo "Usage: ARTCRAFT_APP= $0 " >&2 echo "Got ARTCRAFT_APP='${APP}', action='${ACTION}'." >&2 exit 1 ;;