Files
patriothacks/DEPLOY.md
T

5.0 KiB

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). The one piece that needs a small long-running process is the blob store (api/blob/*), used for cross-device mini-app sharing. api/_lib/blobStore.ts persists blobs to a local SQLite file via Node's built-in node:sqlite module - no database server, no managed service, no signup required for this storage path.

1. Build the frontend

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:

PORT=3001 BLOB_DB_PATH=/var/lib/commons/blobs.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:

// 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:

# /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
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)

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

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 env var and its persistent volume

  • Env var: BLOB_DB_PATH

  • Default (if unset): ./.data/blobs.sqlite3, resolved under the process's own working directory (process.cwd()) - a real file on disk, not os.tmpdir(). This matches the repo's existing .gitignore convention for local persistent state.

  • 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:

    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.

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.