diff --git a/.env.example b/.env.example index 9a87c63..36c62f4 100644 --- a/.env.example +++ b/.env.example @@ -15,13 +15,16 @@ NODE_ENV=production HOST=127.0.0.1 PORT=8787 -# The canonical browser origin. Also used as the default CORS origin. -PUBLIC_ORIGIN=https://milesallen.site +# The public API origin (the Cloudflare Tunnel hostname for this Node app). +# The static site can stay on https://milesallen.site and call this origin. +PUBLIC_ORIGIN=https://api.milesallen.site -# Extra cross-origin origins, comma-separated. https://milesallen.site and -# https://www.milesallen.site are always allowed. Same-origin requests from the -# browser's current host are also accepted. -# ALLOWED_ORIGINS=https://milesallen.site,https://www.milesallen.site +# Browser origins allowed to call the API. https://milesallen.site, +# https://www.milesallen.site, and https://api.milesallen.site are always +# allowed; same-origin requests from the current host are accepted too. +# api.milesallen.site and milesallen.site are same-site, so the vault cookie +# keeps SameSite=Strict and still works across the two hostnames. +ALLOWED_ORIGINS=https://milesallen.site,https://www.milesallen.site # Set true only when a trusted reverse proxy (Caddy, Cloudflare Tunnel) sets # CF-Connecting-IP or X-Forwarded-For. Never enable this on a direct public @@ -29,7 +32,7 @@ PUBLIC_ORIGIN=https://milesallen.site TRUST_PROXY=true # Where the static site lives. Defaults to the process working directory. -# SITE_ROOT=/opt/miles-web +# SITE_ROOT=/opt/websites/miles-web # SQLite file. Keep it outside the web root if you serve files with another # web server. The Node app already blocks /data and /server. @@ -129,6 +132,6 @@ LOG_LEVEL=info # --------------------------------------------------------------------------- # Optional registry path overrides # --------------------------------------------------------------------------- -# PROJECTS_CONFIG_PATH=/opt/miles-web/server/config/projects.json -# SERVICES_CONFIG_PATH=/opt/miles-web/server/config/services.json -# SERVERS_CONFIG_PATH=/opt/miles-web/server/config/servers.json +# PROJECTS_CONFIG_PATH=/opt/websites/miles-web/server/config/projects.json +# SERVICES_CONFIG_PATH=/opt/websites/miles-web/server/config/services.json +# SERVERS_CONFIG_PATH=/opt/websites/miles-web/server/config/servers.json diff --git a/README.md b/README.md index 24a79b1..d5c0501 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,20 @@ The backend is plain JavaScript on Node.js with two native dependencies (`better-sqlite3` and `argon2`); there is no Docker, Go, Rust, Python, or frontend framework involved. +### Split-origin deployment + +The static site can stay on `https://milesallen.site` while the Node backend is +exposed on `https://api.milesallen.site` through a Cloudflare Tunnel. Both +hostnames are under `milesallen.site`, so the vault cookie keeps +`SameSite=Strict` and still works across them. `site-config.js` holds the +API hostname; the dashboard, vault lock page, and analytics beacon use it +and fall back to same-origin when it is empty (local development). + +The server's CORS allowlist includes `https://milesallen.site`, +`https://www.milesallen.site`, and `https://api.milesallen.site`. The JSON API +and the vault/game pages are served by the Node process; the main static site +can remain on its current host. + ## What is included | Area | What it does | @@ -86,6 +100,7 @@ Do not reuse a development passphrase in production, and never commit `.env`. | `npm start` | Start the server without watch mode | | `npm test` | Run the full automated test suite with `node:test` | | `npm run db:migrate` | Apply migrations and seed the project/service/server registries | +| `npm run preflight` | Validate `.env`, paths, permissions, native modules, and `ping` before deployment | | `npm run hash-password -- "passphrase"` | Print an Argon2id hash to put in `.env` | | `npm run smoke` | Check a running local server's APIs and vault gates | @@ -107,6 +122,16 @@ URLs/enabled flags and server name/IP/enabled flags without editing code. The registries are seeded into SQLite on startup and can be re-seeded by running `npm run db:migrate`. +### API hostname + +`site-config.js` contains the browser-facing API base. In production it maps +`https://milesallen.site` and `https://www.milesallen.site` to +`https://api.milesallen.site`. Override it per deployment by setting +`window.MILES_API_BASE` before the script runs, or by adding a +`` tag. When the +value is empty, all API calls stay same-origin, which is what local development +uses. + ### Adding a project Add an object to `server/config/projects.json`: @@ -172,8 +197,9 @@ and refuses logins; it does not fall back to a default password. - GitHub and FEDL calls happen on the server. The GitHub token is never included in API responses or frontend code. - CORS is an explicit allowlist: `https://milesallen.site`, - `https://www.milesallen.site`, local development origins, and same-origin - requests. Unknown cross-origin API requests are rejected. + `https://www.milesallen.site`, `https://api.milesallen.site`, local + development origins, and same-origin requests. Unknown cross-origin API + requests are rejected. - Request bodies have a hard size limit, API and login routes are rate limited, request timeouts are enforced, and errors returned to clients do not expose stack traces or secrets. @@ -209,6 +235,7 @@ vault/game redirects, and that an analytics hit is accepted. ```text analytics.js Privacy-friendly pageview beacon +site-config.js Browser-facing API hostname for split-origin dashboard.html/.css/.js API-driven developer dashboard moon-vault.html Public vault lock screen server/ @@ -220,7 +247,7 @@ server/ lib/ Env, cookies, crypto, errors, HTTP, logging, limits services/ Ping, HTTP checks, status, monitor, GitHub, FEDL, analytics, vault - scripts/ migrate, hash-password, smoke + scripts/ migrate, preflight, hash-password, smoke views/vault.html Authenticated vault content migrations/001_init.sql Schema and indexes tests/ node:test suite and isolated fixtures diff --git a/analytics.js b/analytics.js index da0fe84..2413365 100644 --- a/analytics.js +++ b/analytics.js @@ -23,8 +23,21 @@ ]; if (skip.some(function (pattern) { return pattern.test(path); })) return; + function defaultEndpoint() { + if (window.MILES_API_URL) return window.MILES_API_URL + '/analytics/hit'; + var metaBase = document.querySelector('meta[name="miles-api-base"]'); + if (metaBase && metaBase.getAttribute('content')) { + return metaBase.getAttribute('content').replace(/\/+$/, '') + '/api/analytics/hit'; + } + var host = window.location.hostname; + if (host === 'milesallen.site' || host === 'www.milesallen.site') { + return 'https://api.milesallen.site/api/analytics/hit'; + } + return '/api/analytics/hit'; + } + var meta = document.querySelector('meta[name="miles-analytics-endpoint"]'); - var endpoint = (meta && meta.getAttribute('content')) || '/api/analytics/hit'; + var endpoint = (meta && meta.getAttribute('content')) || defaultEndpoint(); var payload = { path: path, diff --git a/dashboard.html b/dashboard.html index ad75a20..100defb 100644 --- a/dashboard.html +++ b/dashboard.html @@ -11,7 +11,7 @@ - +
diff --git a/dashboard.js b/dashboard.js index 231911e..b25b169 100644 --- a/dashboard.js +++ b/dashboard.js @@ -1,7 +1,13 @@ (function () { 'use strict'; - var API = document.documentElement.dataset.api || '/api'; + var host = window.location.hostname; + var fallbackBase = (host === 'milesallen.site' || host === 'www.milesallen.site') + ? 'https://api.milesallen.site' + : ''; + var API = document.documentElement.dataset.api || + window.MILES_API_URL || + (fallbackBase ? fallbackBase + '/api' : '/api'); var REFRESH_SECONDS = 60; var countdown = REFRESH_SECONDS; var refreshing = false; diff --git a/docs/API.md b/docs/API.md index 1f20e4a..e30f7f8 100644 --- a/docs/API.md +++ b/docs/API.md @@ -53,12 +53,18 @@ include stack traces or secret values. ### CORS Cross-origin API requests are accepted only from the configured allowlist: -`https://milesallen.site`, `https://www.milesallen.site`, local development -origins, and the request's own origin. Unknown origins receive `403`. +`https://milesallen.site`, `https://www.milesallen.site`, +`https://api.milesallen.site`, local development origins, and the request's own +origin. Unknown origins receive `403`. `OPTIONS` preflight requests receive `204` with `Access-Control-Allow-Origin`, -`Access-Control-Allow-Methods: GET, POST, OPTIONS`, and -`Access-Control-Allow-Headers: Content-Type`. +`Access-Control-Allow-Credentials: true`, +`Access-Control-Allow-Methods: GET, POST, OPTIONS`, +`Access-Control-Allow-Headers: Content-Type`, and +`Access-Control-Expose-Headers: Retry-After`. The vault login, session, and +logout endpoints require credentialed requests; the browser page at +`milesallen.site` uses `site-config.js` to call `api.milesallen.site`. Both +hostnames are same-site, so the vault cookie keeps `SameSite=Strict`. ### Rate limits diff --git a/docs/DEPLOYMENT.md b/docs/DEPLOYMENT.md index 231a2ee..73f5ad6 100644 --- a/docs/DEPLOYMENT.md +++ b/docs/DEPLOYMENT.md @@ -4,15 +4,29 @@ This guide describes how to run the backend on a Debian 13 host behind Caddy or Cloudflare Tunnel. It is written to be followed and verified step by step. The repository does not perform the deployment for you. -The app is a single Node.js process that serves the static site and the API from -the same origin. SQLite is the only database. There is no Docker, external -database, or frontend build step. +The app is a single Node.js process. SQLite is the only database. There is no +Docker, external database, or frontend build step. + +Two layouts are supported: + +- **Split origin (recommended):** keep the static site on + `https://milesallen.site` and expose this Node process on + `https://api.milesallen.site`. The dashboard, vault lock page, and analytics + beacon read the API hostname from `site-config.js`; the server accepts + cross-origin requests from the main site. Because both hostnames are under + `milesallen.site`, the vault cookie stays `SameSite=Strict`. +- **Single origin:** point the tunnel or Caddy at the whole site so + `milesallen.site` is served by this process. Leave `site-config.js` empty. + +The vault page (`/vault`) and games (`/games/*`) are always served by this Node +process, so they live on the API hostname in the split-origin layout. ## 1. Target layout | Path | Purpose | | --- | --- | -| `/opt/miles-web` | Repository, static site, backend, `.env` | +| `/opt/websites/miles-web` | Repository, static site, backend, `.env` | +| `/opt/websites/miles-web/site-config.js` | Browser-facing API hostname | | `/var/lib/miles-dashboard/dashboard.db` | SQLite database | | `/var/backups/miles-dashboard/` | SQLite backups | | `/etc/systemd/system/miles-dashboard.service` | systemd unit | @@ -62,15 +76,15 @@ Do not use `sudo npm`. Install dependencies as the service user. sudo useradd --system --create-home \ --home-dir /var/lib/miles-dashboard \ --shell /usr/sbin/nologin miles -sudo mkdir -p /opt/miles-web /var/lib/miles-dashboard /var/backups/miles-dashboard -sudo chown -R miles:miles /opt/miles-web /var/lib/miles-dashboard /var/backups/miles-dashboard +sudo mkdir -p /opt/websites/miles-web /var/lib/miles-dashboard /var/backups/miles-dashboard +sudo chown -R miles:miles /opt/websites/miles-web /var/lib/miles-dashboard /var/backups/miles-dashboard ``` -Clone the repository into `/opt/miles-web` (the directory must be empty): +Clone the repository into `/opt/websites/miles-web` (the directory must be empty): ```bash -sudo -u miles git clone