runSync pushed every local event type to the pubkey-stamped relay, letting a puller correlate healthpost/sky.morale/wallet under one identity. Push is now a default-deny allowlist (health/mood/lightclock/mentor/fairdraw/testimony stay strictly local; only merge-needing types sync). Default WebRTC signaling is now self-hostable (VITE_SIGNALING_URL -> ws://localhost:4444), public host opt-in only; DEPLOY.md documents self-hosting.
227 lines
7.7 KiB
Markdown
227 lines
7.7 KiB
Markdown
# Self-hosting THE COMMONS
|
|
|
|
This app is designed to be self-hosted on your own machine or server - no
|
|
managed cloud database, no third-party storage account, no external
|
|
credential of any kind. Everything below runs with tools already in this
|
|
repo and Node's built-ins.
|
|
|
|
## What needs a server at all
|
|
|
|
Almost everything in this app is a client-only PWA (identity, event log,
|
|
ledger all live in the browser's IndexedDB and keep working offline). Two
|
|
small pieces use the long-running API process, both persisting to local
|
|
SQLite files via Node's built-in `node:sqlite` module - no database server,
|
|
no managed service, no signup required:
|
|
|
|
- the **blob store** (`api/blob/*`, `api/_lib/blobStore.ts`), for
|
|
cross-device mini-app sharing (`BLOB_DB_PATH`, default
|
|
`./.data/blobs.sqlite3`);
|
|
- the **signed-event relay** (`api/sync`, `api/_lib/eventRelayStore.ts`),
|
|
the best-effort cross-device sync for the event log. It survives restarts,
|
|
so synced history keeps accumulating and a fresh device pulls the whole
|
|
"lived-in" history on load (`EVENT_DB_PATH`, default
|
|
`./.data/events.sqlite3`). The relay only ever stores signature-verified
|
|
events and never blocks a local write - the event-log append/read/replay
|
|
path itself stays fully offline.
|
|
|
|
## 1. Build the frontend
|
|
|
|
```bash
|
|
npm ci
|
|
npm run build
|
|
```
|
|
|
|
This produces the static site in `dist/`.
|
|
|
|
## 2. Run the API as a long-running process
|
|
|
|
`api/_lib/devServer.ts` already implements a plain `node:http` server that
|
|
serves `api/blob` and `api/blob/[id]` by calling the same handler functions
|
|
Vercel's serverless runtime would call - it just does it as a normal
|
|
long-running process instead of per-request cold starts. Running that same
|
|
file directly with `node` (via its own `if (import.meta.url === ...)` entry
|
|
point) gives you a minimal production-equivalent entry point:
|
|
|
|
```bash
|
|
PORT=3001 \
|
|
BLOB_DB_PATH=/var/lib/commons/blobs.sqlite3 \
|
|
EVENT_DB_PATH=/var/lib/commons/events.sqlite3 \
|
|
node api/_lib/devServer.ts
|
|
```
|
|
|
|
(This is the same command `npm run dev:api` already runs in this repo -
|
|
Node 24 strips TypeScript types natively, no build step needed for the API.)
|
|
|
|
For a dedicated production entry point (recommended over reusing the dev
|
|
server file directly), add a tiny wrapper that imports the same
|
|
`createApiServer()` export:
|
|
|
|
```ts
|
|
// api/_lib/prodServer.ts
|
|
import { createApiServer } from "./devServer.ts";
|
|
|
|
const port = Number(process.env.PORT ?? 3001);
|
|
createApiServer().listen(port, () => {
|
|
console.log(`commons API listening on :${port}`);
|
|
});
|
|
```
|
|
|
|
Run it as a persistent process with your process manager of choice
|
|
(systemd, pm2, Docker's own restart policy, etc.) so it survives reboots
|
|
and crashes:
|
|
|
|
```ini
|
|
# /etc/systemd/system/commons-api.service
|
|
[Unit]
|
|
Description=The Commons API (blob store)
|
|
After=network.target
|
|
|
|
[Service]
|
|
WorkingDirectory=/opt/commons
|
|
Environment=PORT=3001
|
|
Environment=BLOB_DB_PATH=/var/lib/commons/blobs.sqlite3
|
|
Environment=EVENT_DB_PATH=/var/lib/commons/events.sqlite3
|
|
ExecStart=/usr/bin/node api/_lib/prodServer.ts
|
|
Restart=always
|
|
User=commons
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
```
|
|
|
|
## 3. Serve the static frontend + proxy `/api`
|
|
|
|
Put a reverse proxy in front of both the static `dist/` build and the API
|
|
process, so the whole app is reachable on a normal domain with TLS. Two
|
|
equivalent sketches:
|
|
|
|
### Caddy (automatic TLS)
|
|
|
|
```caddyfile
|
|
commons.example.com {
|
|
handle /api/* {
|
|
reverse_proxy 127.0.0.1:3001
|
|
}
|
|
handle {
|
|
root * /opt/commons/dist
|
|
try_files {path} /index.html
|
|
file_server
|
|
}
|
|
}
|
|
```
|
|
|
|
### nginx
|
|
|
|
```nginx
|
|
server {
|
|
listen 443 ssl;
|
|
server_name commons.example.com;
|
|
|
|
location /api/ {
|
|
proxy_pass http://127.0.0.1:3001;
|
|
proxy_set_header Host $host;
|
|
}
|
|
|
|
location / {
|
|
root /opt/commons/dist;
|
|
try_files $uri /index.html;
|
|
}
|
|
}
|
|
```
|
|
|
|
This mirrors `vercel.json`'s own routing convention (`/api/*` goes to the
|
|
API, everything else falls back to `index.html`) so the same build works
|
|
unchanged whether it's proxied this way or (still) deployed to Vercel.
|
|
|
|
## 4. The `BLOB_DB_PATH` / `EVENT_DB_PATH` env vars and their persistent volume
|
|
|
|
- Env vars: `BLOB_DB_PATH` (mini-app bundles) and `EVENT_DB_PATH`
|
|
(signed-event relay). Both behave identically.
|
|
- Defaults (if unset): `./.data/blobs.sqlite3` and `./.data/events.sqlite3`,
|
|
resolved under the process's own working directory (`process.cwd()`) -
|
|
real files on disk, not `os.tmpdir()`. This matches the repo's existing
|
|
`.gitignore` convention for local persistent state (the whole `.data/`
|
|
directory is gitignored).
|
|
- On a self-hosted box, point it at a path that survives restarts and
|
|
(if containerized) outlives the container filesystem - e.g.
|
|
`/var/lib/commons/blobs.sqlite3` on bare metal, or a bind-mounted host
|
|
directory in Docker:
|
|
|
|
```bash
|
|
docker run -d \
|
|
-e BLOB_DB_PATH=/data/blobs.sqlite3 \
|
|
-v /srv/commons-data:/data \
|
|
-p 3001:3001 \
|
|
commons-api
|
|
```
|
|
|
|
The host directory `/srv/commons-data` is what must persist; the
|
|
container's own filesystem is disposable.
|
|
|
|
**No external account or credential is required for this storage path.**
|
|
The SQLite file is created automatically on first use
|
|
(`CREATE TABLE IF NOT EXISTS`) and needs nothing beyond a writable
|
|
directory on disk.
|
|
|
|
## 5. (Optional) Self-hosted WebRTC signaling for peer-to-peer mini-apps
|
|
|
|
Two mini-apps discover nearby devices over WebRTC and sync peer-to-peer with
|
|
no server-side source of truth: **Assembly** (CRDT governance votes,
|
|
`src/lib/assemblySync.ts`) and **Find Family** (private-set-intersection
|
|
search, `src/miniapps/findfamily/findFamilySync.ts`). WebRTC still needs a
|
|
tiny **signaling** server to broker the initial peer handshake - it only
|
|
relays "who is in this room"; it never sees document state or search
|
|
payloads (those travel directly between peers, encrypted).
|
|
|
|
To honor the self-host preference, the signaling URL is **configurable and
|
|
defaults to a self-hostable value**, not a third-party host:
|
|
|
|
- **Default (no config):** `ws://localhost:4444` - the port the
|
|
[`y-webrtc-signaling`](https://github.com/yjs/y-webrtc) server listens on
|
|
by default. Run your own with one command, no account or build step:
|
|
|
|
```bash
|
|
npx y-webrtc-signaling # listens on ws://0.0.0.0:4444
|
|
```
|
|
|
|
Put it behind the same reverse proxy as the rest of the app so browsers
|
|
reach it over TLS, e.g. add to the Caddy block above:
|
|
|
|
```caddyfile
|
|
handle /signaling {
|
|
reverse_proxy 127.0.0.1:4444
|
|
}
|
|
```
|
|
|
|
- **Override (recommended for production):** set the `VITE_SIGNALING_URL`
|
|
build-time env var to your deployed signaling URL before `npm run build`:
|
|
|
|
```bash
|
|
VITE_SIGNALING_URL=wss://commons.example.com/signaling npm run build
|
|
```
|
|
|
|
- **Public fallback (opt-in only):** y-webrtc's bundled public server
|
|
`wss://y-webrtc-eu.fly.dev` still works with zero infrastructure, but it is
|
|
a **third-party host that can observe which device keys join which rooms**,
|
|
so it is never the default. Opt into it explicitly only if you accept that
|
|
trade-off:
|
|
|
|
```bash
|
|
VITE_SIGNALING_URL=wss://y-webrtc-eu.fly.dev npm run build
|
|
```
|
|
|
|
These mini-apps' peer sync is off by default and opt-in per resident, so a
|
|
deployment that never configures signaling simply keeps them local-only.
|
|
|
|
## Vercel caveat (if you ever go that route instead)
|
|
|
|
`vercel.json`'s `/api/*` file-routing convention still works unchanged if
|
|
you deploy this exact code to Vercel. But Vercel's serverless functions
|
|
only get a writable `/tmp`, which is **ephemeral per-invocation, not a
|
|
persistent disk** - a real Vercel deployment would need `BLOB_DB_PATH`
|
|
pointed at a genuinely persistent mounted volume, which Vercel's
|
|
serverless `api/` functions do not provide. The supported path for
|
|
persistent blob storage is the long-running self-hosted process described
|
|
above (steps 1-4); don't assume the Vercel path "just works" for
|
|
persistence without that caveat.
|