Add comprehensive test suite for application functionality

- Implement tests for HTTP API endpoints, ensuring correct responses and security headers.
- Create configuration tests to validate loading of defaults and environment overrides.
- Add tests for FEDL service to verify data handling and snapshot functionality.
- Introduce GitHub service tests to check API data normalization and caching behavior.
- Develop monitor service tests for network checks and uptime calculations.
- Implement persistence tests to ensure database migrations are idempotent and data persists across restarts.
- Add vault service tests for session management and brute-force protection mechanisms.
- Include helper functions for test context creation and JSON response handling.
This commit is contained in:
miles committed 2026-11-08 16:24:06 -06:00
1 parent 84b7a7e302
commit c250653961
55 files changed
+8174 -226

No files matched your search

+656
View File
@@ -0,0 +1,656 @@
# API reference
The backend serves both the static site and a JSON API from the same origin.
The default local base URL is `http://127.0.0.1:8787`.
All API responses are JSON with `Cache-Control: no-store` unless noted. There
are no API keys, cookies, or visitor-supplied URLs involved in public read
endpoints. The only cookie in the system is the Moon Vault session cookie.
## Conventions
### Authentication
Public read endpoints require no authentication. The Moon Vault endpoints set
and read a signed, HttpOnly session cookie named `miles_vault` (configurable
with `VAULT_COOKIE_NAME`).
Protected content:
- `GET /vault` — authenticated vault HTML. Redirects to `/moon-vault.html`
with `302` when there is no valid session.
- `GET /games/*` — authenticated game files. Redirects to
`/moon-vault.html` with `302` when there is no valid session.
### Errors
All API errors use one shape:
```json
{
"error": {
"code": "not_found",
"message": "API endpoint not found",
"details": {}
}
}
```
`details` is optional and is omitted for server errors. Internal errors never
include stack traces or secret values.
| Status | Typical `code` values |
| --- | --- |
| 400 | `bad_request` |
| 401 | `unauthorized` |
| 403 | `forbidden` |
| 404 | `not_found`, `github_not_found` |
| 405 | `method_not_allowed` |
| 429 | `rate_limited` (includes `Retry-After`) |
| 500 | `internal_error` |
| 503 | `unavailable` |
### 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`.
`OPTIONS` preflight requests receive `204` with `Access-Control-Allow-Origin`,
`Access-Control-Allow-Methods: GET, POST, OPTIONS`, and
`Access-Control-Allow-Headers: Content-Type`.
### Rate limits
Limits are per client IP and use `429` with a `Retry-After` header when
exceeded.
| Route group | Default |
| --- | --- |
| Public API | 300 requests / minute |
| `POST /api/analytics/hit` | 120 requests / minute |
| `POST /api/vault/login` | 15 requests / 15 minutes |
| Vault failed attempts | 5 / 15 minutes per hashed IP (configurable) |
When `TRUST_PROXY=true`, the client IP is read from `CF-Connecting-IP` or the
first `X-Forwarded-For` value. Leave it false when the Node process is directly
exposed, otherwise clients can spoof their IP.
### Security headers
All responses include:
```text
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: geolocation=(), microphone=(), camera=()
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Resource-Policy: same-origin
X-Request-Id: <16 hex characters>
```
In production, `Strict-Transport-Security: max-age=31536000; includeSubDomains`
is added.
---
## Index and health
### `GET /api`
Returns service metadata and the endpoint list.
```json
{
"name": "milesallen.site dashboard API",
"version": "1.0.0",
"endpoints": ["/api/status", "/api/status/servers", "..."]
}
```
### `GET /api/health`
```json
{
"status": "ok",
"uptimeSeconds": 123,
"time": "2026-11-08T04:50:13.155Z"
}
```
Also accepts `HEAD`.
---
## Infrastructure and service status
### `GET /api/status`
Combined snapshot used by the dashboard.
```json
{
"generatedAt": "2026-11-08T04:50:13.145Z",
"overall": {
"status": "operational",
"counts": { "operational": 5, "degraded": 0, "offline": 0, "unknown": 0, "online": 2 },
"total": 7,
"healthy": 5,
"checkedAt": "2026-11-08T04:49:21.746Z"
},
"network": {
"onHomeNetwork": true,
"monitoredSubnet": "10.0.0.0/24"
},
"servers": [
{
"id": "home-1",
"name": "Home Server 1",
"ip": "10.0.0.87",
"status": "online",
"reason": "ok",
"networkContext": true,
"lastCheckAt": "2026-11-08T04:49:21.000Z",
"lastSuccessAt": "2026-11-08T04:49:21.000Z",
"lastChangeAt": "2026-11-08T04:47:24.000Z",
"previousStatus": null,
"responseMs": 2.1,
"uptime": { "24h": 100, "7d": 100, "30d": 100 },
"checks": 120
}
],
"services": [
{
"id": "fedl",
"name": "FEDL",
"url": "https://fedl.site/",
"status": "operational",
"reason": "ok",
"lastCheckAt": "2026-11-08T04:49:21.000Z",
"lastSuccessAt": "2026-11-08T04:49:21.000Z",
"lastChangeAt": "2026-11-08T04:47:24.000Z",
"previousStatus": null,
"responseMs": 180,
"httpStatus": 200,
"uptime": { "24h": 100, "7d": 100, "30d": 100 },
"checks": 60
}
]
}
```
Status values:
| Component | Values |
| --- | --- |
| Server | `online`, `offline`, `unknown` |
| Service | `operational`, `degraded`, `offline`, `unknown` |
| Overall | `operational`, `degraded`, `offline`, `unknown` |
Server failures are reported as `unknown` with
`reason: "not_on_home_network"` when the Node host is not on `HOME_SUBNET`.
Monitoring failures such as a missing `ping` binary are reported as `unknown`
with `reason` beginning `monitor_error:` so a broken monitor is not mistaken for
a down server.
Uptime percentages are `null` when there is no data for the window.
### `GET /api/status/servers`
```json
{
"generatedAt": "2026-11-08T04:50:13.149Z",
"servers": [ /* same server objects as /api/status */ ]
}
```
### `GET /api/status/services`
```json
{
"generatedAt": "2026-11-08T04:50:13.149Z",
"services": [ /* same service objects as /api/status */ ]
}
```
### `GET /api/status/history?days=30`
`days` is clamped to 1–90. The response always contains one entry per day for
every configured server and service.
```json
{
"generatedAt": "2026-11-08T04:50:13.149Z",
"days": 30,
"servers": {
"home-1": [
{ "day": "2026-10-10", "availability": 100, "up": 1440, "total": 1440 }
]
},
"services": {
"fedl": [
{ "day": "2026-10-10", "availability": 100, "up": 720, "total": 720 }
]
}
}
```
`availability` is `null` for a day with no checks.
---
## GitHub
Data comes from the official GitHub REST API through the server. Responses are
cached in SQLite and served stale when GitHub is unavailable or rate limited.
The token is never included in any response.
### `GET /api/github/overview`
```json
{
"generatedAt": "2026-11-08T04:49:20.871Z",
"username": "mileswolfallen2",
"profile": {
"login": "mileswolfallen2",
"name": "wolf_reaper90",
"avatarUrl": "https://avatars.githubusercontent.com/u/115095719?v=4",
"bio": "A dev and 3D artist from Minnesota",
"company": null,
"location": "Minnesota",
"blog": "milesallen.site",
"followers": 16,
"following": 42,
"publicRepos": 165,
"createdAt": "2022-10-05T15:02:14Z"
},
"rateLimit": {
"authenticated": true,
"remaining": 4949,
"resetAt": "2026-11-08T05:31:15.000Z"
},
"totals": {
"stars": 18,
"forks": 2,
"openIssues": 13,
"trackedRepos": 14,
"publicRepos": 165
},
"languages": [{ "name": "JavaScript", "count": 6 }],
"recentCommits": [
{
"repo": "mileswolfallen2/miles-web",
"project": "milesallen.site",
"branch": "main",
"sha": "abcdef1",
"message": "Commit subject",
"author": "Miles",
"date": "2026-11-08T03:00:00Z",
"url": "https://github.com/mileswolfallen2/miles-web/commit/abcdef1"
}
],
"recentlyUpdated": [
{ "id": "miles-web", "name": "milesallen.site", "pushedAt": "2026-11-08T03:00:00Z", "latestCommit": {} }
],
"topActive": [{ "repo": "mileswolfallen2/miles-web", "events": 4, "pushes": 2, "project": "milesallen.site" }]
}
```
`recentCommits` is capped at 12 and can be empty when GitHub's public events
window has no push events for the account.
### `GET /api/github/activity`
```json
{
"generatedAt": "2026-11-08T04:49:20.871Z",
"username": "mileswolfallen2",
"rateLimit": {},
"recentCommits": [],
"activity": {
"days": [{ "day": "2026-11-08", "events": 2, "pushes": 1 }],
"byRepo": [{ "repo": "mileswolfallen2/miles-web", "events": 2, "pushes": 1, "project": "milesallen.site" }]
}
}
```
`activity.days` always contains 30 entries.
### `GET /api/github/repositories`
```json
{
"generatedAt": "2026-11-08T04:49:20.871Z",
"rateLimit": {},
"repositories": [
{
"id": "miles-web",
"name": "milesallen.site",
"description": "This portfolio: static HTML, hand-written CSS, no build step.",
"fullName": "mileswolfallen2/miles-web",
"url": "https://github.com/mileswolfallen2/miles-web",
"website": "https://milesallen.site",
"language": "HTML",
"stars": 0,
"forks": 0,
"openIssues": 0,
"watchers": 0,
"defaultBranch": "main",
"archived": false,
"fork": false,
"topics": [],
"pushedAt": "2026-11-08T03:00:00Z",
"updatedAt": "2026-11-08T03:00:00Z",
"createdAt": "2025-01-01T00:00:00Z",
"latestCommit": {},
"release": null,
"contributors": null,
"featured": false
}
]
}
```
Only projects marked `visible` and configured with a GitHub repo are enriched.
`release` and `contributors` are populated only when `GITHUB_ENRICH=true` and a
token is configured. Per-repo enrichment failures may appear in an `errors`
array on that repository instead of failing the whole response.
---
## Projects
### `GET /api/projects`
Returns visible projects from `server/config/projects.json` (seeded into
SQLite), ordered by `display_order`.
```json
{
"generatedAt": "2026-11-08T04:50:13.153Z",
"projects": [
{
"id": "fedl",
"name": "FEDL",
"description": "Geometry Dash demon list ...",
"github": {
"owner": "mileswolfallen2",
"repo": "fedl",
"url": "https://github.com/mileswolfallen2/fedl"
},
"website": "https://fedl.site",
"api": { "type": "fedl", "base": "https://server.fedl.site/fedl/api" },
"featured": true,
"health": "operational"
}
]
}
```
`github` and `api` are `null` when not configured. `health` is the matching
website-check status or `null` when the project has no monitored website.
### `GET /api/projects/:id`
Returns one visible project by registry id. Returns `404` when the project does
not exist or is hidden.
```json
{
"project": {
"id": "fedl",
"name": "FEDL",
"description": "Geometry Dash demon list ...",
"github": { "owner": "mileswolfallen2", "repo": "fedl", "url": "https://github.com/mileswolfallen2/fedl" },
"website": "https://fedl.site",
"api": { "type": "fedl", "base": "https://server.fedl.site/fedl/api" },
"featured": true
}
}
```
---
## FEDL
FEDL data is fetched only from the configured real API:
- `GET {FEDL_API_BASE}/list`
- `GET {FEDL_API_BASE}/runs`
Expected upstream shapes are `{ "items": [...] }`. The server stores snapshots,
computes diffs between the latest two snapshots, and serves the last good stats
if an upstream refresh fails.
### `GET /api/fedl/stats`
```json
{
"generatedAt": "2026-11-08T04:46:24.485Z",
"site": "https://fedl.site",
"apiBase": "https://server.fedl.site/fedl/api",
"github": { "owner": "mileswolfallen2", "repo": "fedl" },
"totals": {
"rankedLevels": 342,
"records": 3841,
"submissions": 3843,
"rejected": 2,
"pending": 0,
"players": 769
},
"topLevels": [
{ "title": "GRIEF", "position": 1, "url": "https://youtu.be/gB8slBPlOjc" }
],
"recentRecords": [
{
"id": "run_1",
"playerName": "player1",
"levelTitle": "Alpha Demon",
"percent": "100",
"videoUrl": "https://youtu.be/run1",
"status": "approved",
"notes": "",
"submittedAt": "2026-11-01T00:00:00.000Z",
"updatedAt": "2026-11-01T00:00:00.000Z",
"reviewedBy": null
}
],
"latestUpdates": [],
"changes": {
"comparedTo": "2026-11-07T12:00:00.000Z",
"added": [{ "title": "Delta Demon", "position": 4 }],
"removed": [{ "title": "Gamma Demon", "position": 3 }],
"rankChanges": [{ "title": "Beta Demon", "from": 2, "to": 1, "delta": 1 }],
"counts": { "added": 1, "removed": 1, "rankChanges": 1 }
},
"stale": false
}
```
- `comparedTo` is `null` until there are at least two snapshots.
- `stale` is `true` when the served data is a cached copy after a failed
refresh.
- `topLevels` is capped at 15, `recentRecords` at 12, and `latestUpdates` at 10.
- Diff lists are capped at 25 entries each.
### `GET /api/fedl/activity`
```json
{
"generatedAt": "2026-11-08T04:46:24.485Z",
"stale": false,
"site": "https://fedl.site",
"github": { "owner": "mileswolfallen2", "repo": "fedl" },
"changes": {},
"recentRecords": [],
"latestUpdates": []
}
```
---
## Analytics
Analytics is cookieless and stores no raw IP addresses. See the privacy notes in
the README.
### `POST /api/analytics/hit`
Request:
```http
POST /api/analytics/hit
Content-Type: application/json
{
"path": "/projects/fedl.html?utm=1#top",
"referrer": "https://www.google.com/search?q=test"
}
```
`path` is required and must be a same-site path. Query strings and fragments are
stripped, duplicate slashes are collapsed, `/index.html` becomes `/`, and a
trailing slash is removed except for `/`. `referrer` is optional; only its
hostname is retained.
Response `202`:
```json
{ "recorded": true, "reason": null }
```
`reason` can be `"bot"` or `"duplicate"` when the hit is intentionally ignored.
An invalid path returns `400`. Bots receive `202` with
`{ "recorded": false, "reason": "bot" }` rather than an error.
### `GET /api/analytics/overview`
```json
{
"generatedAt": "2026-11-08T04:50:13.154Z",
"retentionDays": 30,
"totals": {
"pageviews": 1,
"pageviewsToday": 1,
"pageviews7d": 1,
"pageviews30d": 1,
"uniqueVisitorsToday": 1,
"uniqueVisitors7d": 1,
"uniqueVisitors30d": 1
},
"daily": [{ "day": "2026-11-08", "views": 1, "visitors": 1 }],
"topPages": [{ "path": "/", "views": 1, "visitors": 1 }],
"recentActivity": [
{
"path": "/",
"referrer_domain": "www.google.com",
"created_at": "2026-11-08T04:49:35.266Z"
}
]
}
```
`daily` always has 30 entries. `topPages` is limited to 10 and `recentActivity`
to 20 rows.
### `GET /api/analytics/pages?days=30&limit=50`
`days` is clamped to the configured retention period (default 30). `limit` is
clamped to 1–200.
```json
{
"generatedAt": "2026-11-08T04:50:13.155Z",
"range": {
"days": 30,
"from": "2026-10-10T00:00:00.000Z",
"to": "2026-11-09T00:00:00.000Z"
},
"totalPageviews": 1,
"pages": [
{
"path": "/",
"views": 1,
"visitors": 1,
"share": 100,
"lastSeen": "2026-11-08T04:49:35.266Z"
}
]
}
```
---
## Moon Vault
### `GET /api/vault/session`
```json
{ "enabled": true, "authenticated": false, "expiresAt": null }
```
`enabled` is false when no Argon2 hash is configured. `expiresAt` is an ISO
timestamp when authenticated.
### `POST /api/vault/login`
Request:
```http
POST /api/vault/login
Content-Type: application/json
{ "password": "the passphrase" }
```
Success `200`:
```json
{ "ok": true, "expiresAt": "2026-11-08T16:49:45.039Z" }
```
Response headers include:
```text
Set-Cookie: miles_vault=<sessionId>.<token>.<signature>; Path=/; Max-Age=43200; HttpOnly; SameSite=Strict
```
`Secure` is appended when `COOKIE_SECURE=true`.
Failures:
- `401 unauthorized` — wrong passphrase or malformed password.
- `429 rate_limited` — too many attempts, with `Retry-After`.
- `503 vault_disabled` — no hash is configured (`VAULT_PASSWORD_HASH` missing).
The session cookie is a random server-side session id plus a random token, both
signed with `SESSION_SECRET`. Only a SHA-256 hash of the token is stored in
SQLite. The session survives server restarts until it expires.
### `POST /api/vault/logout`
```json
{ "ok": true }
```
Clears the cookie and deletes the server-side session.
---
## Static and protected pages
| Route | Behavior |
| --- | --- |
| `GET /` | Serves `index.html` with `Cache-Control: no-cache` |
| `GET /dashboard.html` | Dashboard UI |
| `GET /moon-vault.html` | Public vault lock screen; contains no passphrase |
| `GET /vault` | `200` authenticated vault HTML, otherwise `302` to `/moon-vault.html` |
| `GET /games/*` | `200` authenticated game assets, otherwise `302` to `/moon-vault.html` |
| `GET /analytics.js` | Privacy-friendly pageview beacon |
| Missing file | Serves `404.html` with status `404` |
Static serving blocks path traversal and private paths (`.env`, `.git`,
`node_modules`, `server`, `tests`, `data`, `docs`, `share-server`,
`package.json`, database files). HTML is served with `no-cache`; other files use
`public, max-age=3600`. Range requests are supported for media files.
+554
View File
@@ -0,0 +1,554 @@
# Deployment guide — Debian 13
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.
## 1. Target layout
| Path | Purpose |
| --- | --- |
| `/opt/miles-web` | Repository, static site, backend, `.env` |
| `/var/lib/miles-dashboard/dashboard.db` | SQLite database |
| `/var/backups/miles-dashboard/` | SQLite backups |
| `/etc/systemd/system/miles-dashboard.service` | systemd unit |
| `/etc/caddy/Caddyfile` | Caddy reverse proxy (if used) |
| `/etc/cloudflared/config.yml` | Cloudflare Tunnel config (if used) |
Use Caddy **or** Cloudflare Tunnel as the public entry point. Running both is
possible but adds a second moving part; if you do, point the tunnel at Caddy and
do not expose the Node process directly.
## 2. Prerequisites
```bash
sudo apt update
sudo apt install -y git curl ca-certificates build-essential python3 iputils-ping sqlite3
```
Check `ping` exists:
```bash
command -v ping
ping -c 1 10.0.0.87
```
### Node.js 20 or newer
Debian's `nodejs` package may be old enough to be a problem. Verify:
```bash
node -v
```
If it is below 20, install Node 20 LTS from NodeSource:
```bash
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
node -v
npm -v
```
Do not use `sudo npm`. Install dependencies as the service user.
## 3. Service user and directories
```bash
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
```
Clone the repository into `/opt/miles-web` (the directory must be empty):
```bash
sudo -u miles git clone <YOUR_REPOSITORY_URL> /opt/miles-web
cd /opt/miles-web
sudo -u miles npm ci --omit=dev
```
If you are deploying from a checkout rather than a remote, copy the files and
then run `npm ci --omit=dev` as `miles`.
### If native modules fail to build
`better-sqlite3` and `argon2` compile native code. If installation fails:
```bash
sudo apt install -y build-essential python3
cd /opt/miles-web
sudo -u miles npm rebuild better-sqlite3 argon2
```
## 4. Environment file
```bash
cd /opt/miles-web
sudo -u miles cp .env.example .env
sudo -u miles chmod 600 .env
sudo -u miles editor .env
```
Production values to set:
```dotenv
NODE_ENV=production
HOST=127.0.0.1
PORT=8787
PUBLIC_ORIGIN=https://milesallen.site
TRUST_PROXY=true
DB_PATH=/var/lib/miles-dashboard/dashboard.db
SITE_ROOT=/opt/miles-web
COOKIE_SECURE=true
GITHUB_USERNAME=mileswolfallen2
GITHUB_TOKEN=<server-only token>
VAULT_PASSWORD_HASH=<argon2id hash>
```
Generate the session secret:
```bash
openssl rand -hex 32
```
Put the output in `SESSION_SECRET`. It must be at least 32 characters in
production. Do not reuse the development secret.
Generate the vault hash without putting the passphrase in shell history:
```bash
read -rsp "Vault passphrase: " VAULT_PASS; echo
printf '%s' "$VAULT_PASS" | sudo -u miles npm run hash-password
unset VAULT_PASS
```
Copy the printed `VAULT_PASSWORD_HASH=...` line into `.env`.
Notes:
- `HOST=127.0.0.1` is correct when Caddy or Cloudflare Tunnel runs on the same
host. Do not bind `0.0.0.0` unless you intend to expose port 8787 directly.
- `TRUST_PROXY=true` is safe only behind a trusted local proxy that overwrites
`CF-Connecting-IP` or `X-Forwarded-For`. On a directly exposed listener it
would let clients spoof their IP.
- `COOKIE_SECURE=true` requires HTTPS in the browser. Use it in production.
- `GITHUB_TOKEN` is optional for public data but raises the rate limit. A
classic token with no scopes is sufficient. Never put it in frontend files.
- `VAULT_PASSWORD_HASH` must start with `$argon2`. If it is missing, the vault
is disabled and refuses logins.
## 5. Initialize the database
```bash
cd /opt/miles-web
sudo -u miles npm run db:migrate
```
Expected output includes the database path and the table list. The directory
`/var/lib/miles-dashboard` must be writable by `miles`.
SQLite runs in WAL mode. The app creates `dashboard.db`, `dashboard.db-wal`,
and `dashboard.db-shm`. Back up all three safely with the SQLite backup command
in section 9; do not copy only the main file while the service is running.
## 6. systemd service
Create `/etc/systemd/system/miles-dashboard.service`:
```ini
[Unit]
Description=milesallen.site dashboard
Documentation=https://github.com/mileswolfallen2/miles-web
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=miles
Group=miles
WorkingDirectory=/opt/miles-web
EnvironmentFile=/opt/miles-web/.env
ExecStart=/usr/bin/node server/index.js
Restart=on-failure
RestartSec=5
TimeoutStopSec=20
KillSignal=SIGTERM
StandardOutput=journal
StandardError=journal
# Hardening. /opt/miles-web is read-only to the service; the DB directory is
# the only writable path.
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=strict
ProtectHome=yes
ReadWritePaths=/var/lib/miles-dashboard
CapabilityBoundingSet=CAP_NET_RAW
AmbientCapabilities=CAP_NET_RAW
[Install]
WantedBy=multi-user.target
```
`CAP_NET_RAW` lets the child `ping` process send ICMP without running the whole
service as root. If your host's `ping` works through unprivileged ICMP sockets
and you prefer not to grant any capability, remove the two capability lines and
verify monitoring still works. If pings fail only under systemd, see
[Troubleshooting](#12-troubleshooting).
Reload and start:
```bash
sudo systemctl daemon-reload
sudo systemctl enable --now miles-dashboard
sudo systemctl status miles-dashboard --no-pager
sudo journalctl -u miles-dashboard -n 100 --no-pager
```
Expected log line:
```text
dashboard listening url=http://127.0.0.1:8787 env=production vault=enabled github=authenticated
```
Verify locally:
```bash
curl -fsS http://127.0.0.1:8787/api/health
curl -fsS http://127.0.0.1:8787/api/status | head -c 500
```
The smoke script is intended for local verification. On a production database
it records one synthetic `/smoke-test` analytics pageview:
```bash
cd /opt/miles-web
sudo -u miles npm run smoke
```
## 7. Reverse proxy with Caddy
Install Caddy from the official repository:
```bash
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' \
| sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' \
| sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install -y caddy
```
Replace `/etc/caddy/Caddyfile`:
```caddyfile
milesallen.site, www.milesallen.site {
encode zstd gzip
reverse_proxy 127.0.0.1:8787
}
```
Caddy obtains and renews TLS certificates automatically. Validate and reload:
```bash
sudo caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddy
sudo journalctl -u caddy -n 50 --no-pager
```
Make sure DNS points `milesallen.site` and `www.milesallen.site` at the host
before Caddy tries to issue certificates. Open only the ports you need:
```bash
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
```
Do **not** expose port 8787.
With Caddy in front:
- `TRUST_PROXY=true`
- `COOKIE_SECURE=true`
- `PUBLIC_ORIGIN=https://milesallen.site`
- Keep `HOST=127.0.0.1`
Caddy sets `X-Forwarded-For`; the app reads it only when `TRUST_PROXY=true`.
## 8. Cloudflare Tunnel alternative
Use this instead of public ports and Caddy. The origin can stay on loopback and
no inbound 80/443 is required.
Install `cloudflared` using Cloudflare's current Debian instructions, then:
```bash
cloudflared tunnel login
cloudflared tunnel create miles-dashboard
cloudflared tunnel route dns miles-dashboard milesallen.site
cloudflared tunnel route dns miles-dashboard www.milesallen.site
```
Create `/etc/cloudflared/config.yml`:
```yaml
tunnel: <TUNNEL_ID>
credentials-file: /etc/cloudflared/<TUNNEL_ID>.json
ingress:
- hostname: milesallen.site
service: http://127.0.0.1:8787
- hostname: www.milesallen.site
service: http://127.0.0.1:8787
- service: http_status:404
```
Validate and install the service:
```bash
sudo cloudflared tunnel ingress validate
sudo cloudflared service install
sudo systemctl enable --now cloudflared
sudo systemctl status cloudflared --no-pager
```
Keep `HOST=127.0.0.1`, set `TRUST_PROXY=true` (Cloudflare sets
`CF-Connecting-IP`), and keep `COOKIE_SECURE=true`. No `ufw` rule for 8787,
80, or 443 is needed for the tunnel path.
## 9. Persistence, backups, and restore
### What persists
- `projects`, `services`, and `servers` are seeded from the JSON registry on
every startup.
- Checks, pageviews, cache entries, FEDL snapshots, and vault sessions live in
`dashboard.db`.
- `.env` holds secrets and deployment configuration. It is not in git.
### Retention enforced by the app
A cleanup pass runs every six hours and at startup:
- Analytics rows older than `ANALYTICS_RETENTION_DAYS` (default 30).
- Monitor checks older than `MONITOR_RETENTION_DAYS` (default 90).
- FEDL snapshots are pruned to the newest 50.
- Expired vault sessions and vault attempts older than 24 hours.
### Online SQLite backup
```bash
sudo mkdir -p /var/backups/miles-dashboard
sudo sqlite3 /var/lib/miles-dashboard/dashboard.db \
".backup '/var/backups/miles-dashboard/dashboard-$(date +%F-%H%M).db'"
```
This is safe while the service is running. Verify the backup:
```bash
sudo sqlite3 /var/backups/miles-dashboard/dashboard-YYYY-MM-DD-HHMM.db \
"PRAGMA integrity_check; SELECT COUNT(*) FROM pageviews;"
```
Daily cron example (`/etc/cron.d/miles-dashboard-backup`):
```cron
17 3 * * * miles /usr/bin/sqlite3 /var/lib/miles-dashboard/dashboard.db ".backup '/var/backups/miles-dashboard/dashboard-$(date +\%F).db'" && find /var/backups/miles-dashboard -name 'dashboard-*.db' -mtime +14 -delete
```
The `%` characters must be escaped as `\%` in cron.
### Restore
```bash
sudo systemctl stop miles-dashboard
sudo rm -f /var/lib/miles-dashboard/dashboard.db-wal /var/lib/miles-dashboard/dashboard.db-shm
sudo cp /var/lib/miles-dashboard/dashboard.db /var/lib/miles-dashboard/dashboard.db.before-restore
sudo cp /var/backups/miles-dashboard/dashboard-YYYY-MM-DD.db /var/lib/miles-dashboard/dashboard.db
sudo chown miles:miles /var/lib/miles-dashboard/dashboard.db
sudo systemctl start miles-dashboard
curl -fsS http://127.0.0.1:8787/api/health
```
## 10. Upgrades
```bash
cd /opt/miles-web
sudo -u miles git pull --ff-only
sudo -u miles npm ci --omit=dev
sudo -u miles npm run db:migrate
sudo systemctl restart miles-dashboard
sudo journalctl -u miles-dashboard -n 50 --no-pager
```
Run `npm test` on a development machine before deploying changes. Migrations are
idempotent and tracked in `schema_migrations`.
After a Node major-version upgrade, rebuild native modules if needed:
```bash
cd /opt/miles-web
sudo -u miles npm rebuild better-sqlite3 argon2
sudo systemctl restart miles-dashboard
```
## 11. Verification checklist
Run these after deployment:
```bash
# Process and logs
sudo systemctl status miles-dashboard --no-pager
sudo journalctl -u miles-dashboard -n 50 --no-pager
# Local health
curl -fsS http://127.0.0.1:8787/api/health
curl -fsS http://127.0.0.1:8787/api/status
# Public site through the proxy
curl -fsS https://milesallen.site/api/health
curl -fsS -o /dev/null -w '%{http_code}\n' https://milesallen.site/
curl -fsS -o /dev/null -w '%{http_code}\n' https://milesallen.site/dashboard.html
# Vault is locked without a session
curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' https://milesallen.site/vault
curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' https://milesallen.site/games/dino.html
# Private files are not served
curl -s -o /dev/null -w '%{http_code}\n' https://milesallen.site/.env
curl -s -o /dev/null -w '%{http_code}\n' https://milesallen.site/server/config.js
curl -s -o /dev/null -w '%{http_code}\n' https://milesallen.site/data/dashboard.db
```
Expected result for the private-file checks is `403`. The vault and game checks
should return `302` to `/moon-vault.html` before login.
Then verify in a browser:
- The dashboard loads and shows live values (or explicit unavailable states).
- Home-server checks are `online` when the server is on the home LAN.
- The vault login works with the production passphrase; the session survives a
server restart; logout returns to the lock page.
- A game loads only after login.
- The analytics totals increase after a page view (allow for deduplication and
bot filtering).
## 12. Troubleshooting
### `better-sqlite3` or `argon2` fails to install
```bash
sudo apt install -y build-essential python3
cd /opt/miles-web
sudo -u miles npm rebuild better-sqlite3 argon2
```
### Servers show `unknown` with `monitor_error:ping_unavailable`
The monitor could not run `ping`. Check:
```bash
command -v ping
ping -c 1 10.0.0.87
getcap "$(command -v ping)" 2>/dev/null || true
sysctl net.ipv4.ping_group_range
```
Fix options, in order of preference:
1. Allow unprivileged ICMP for the service group by setting
`net.ipv4.ping_group_range` appropriately.
2. Keep the systemd `AmbientCapabilities=CAP_NET_RAW` and
`CapabilityBoundingSet=CAP_NET_RAW` lines.
3. Remove `NoNewPrivileges=yes` temporarily to test whether it is blocking a
setuid/capability-based `ping`, then choose a narrower fix.
Never make the Node process run as root just to ping two hosts.
### Servers show `unknown` with `not_on_home_network`
The host's local addresses do not match `HOME_SUBNET`. Set it to the real
subnet (for example `HOME_SUBNET=10.0.0.0/24`) and restart. This is intentional:
a failed ping from a remote network is not reported as a home-server outage.
### Website checks all fail
Check outbound HTTPS and DNS from the server:
```bash
curl -I https://milesallen.site/
curl -I https://fedl.site/
curl -I https://server.fedl.site/fedl/api/list
```
The app only checks configured URLs. It never accepts a visitor-supplied URL.
### Vault is disabled
The log says `vault: disabled` or the API returns `503 vault_disabled` when no
`VAULT_PASSWORD_HASH` is set. Generate one with `npm run hash-password`, add it
to `.env`, and restart. The hash must start with `$argon2`.
### Login succeeds but the browser is not authenticated
- If serving over plain HTTP with `COOKIE_SECURE=true`, the browser discards
the cookie. Use HTTPS or set `COOKIE_SECURE=false` for local-only testing.
- Confirm the browser is using the same origin as `PUBLIC_ORIGIN`.
- Confirm `SESSION_SECRET` did not change and the database was not wiped.
- Check that `TRUST_PROXY` matches the actual proxy setup.
### API returns `403 forbidden` cross-origin
The request origin is not in the allowlist. Set `PUBLIC_ORIGIN` and
`ALLOWED_ORIGINS` to the real HTTPS origins. Same-origin requests are always
accepted.
### `SQLITE_BUSY` or database locked
Run one service instance only. The app uses WAL mode and short statements; a
second process writing the same file is the usual cause. Do not run
`npm run dev` and the systemd service against the same database.
### GitHub data is stale or missing
- Check the token and rate limit: `curl -H "Authorization: Bearer $TOKEN" https://api.github.com/rate_limit`.
- Without a token, the anonymous limit is low and the dashboard will serve
cached data.
- `GITHUB_ENRICH=true` requires a token and makes more API calls.
### FEDL data is stale
The dashboard sets `stale: true` and serves the last good snapshot when the FEDL
API is unreachable. Check the upstream API directly:
```bash
curl -fsS https://server.fedl.site/fedl/api/list | head -c 200
curl -fsS https://server.fedl.site/fedl/api/runs | head -c 200
```
## 13. Security checklist
- [ ] `.env` is mode `600`, owned by the service user, and not in git.
- [ ] `SESSION_SECRET` is a fresh 32+ byte random value.
- [ ] `VAULT_PASSWORD_HASH` is Argon2id and the plaintext passphrase is not in
any file, git history, or shell history.
- [ ] `HOST=127.0.0.1` and port 8787 is not reachable from the internet.
- [ ] `COOKIE_SECURE=true` and the public site is HTTPS.
- [ ] `TRUST_PROXY=true` only behind Caddy/Cloudflare Tunnel.
- [ ] GitHub token is server-only, minimal scope, and never in frontend code.
- [ ] Backups are enabled and a restore has been tested.
- [ ] `/vault` and `/games/*` redirect without a session.
- [ ] Vault and game responses send `Cache-Control: no-store`, so protected
content is not replayed from browser cache after logout.
- [ ] `/.env`, `/server/`, and `/data/` return `403`.
- [ ] `npm test` passes on the deployed revision.
- [ ] Node.js receives security updates.