Self-host on Coolify (Vultr VPS) #330

Merged
chris-kreidl merged 3 commits from chore/self-host into main 2026-12-04 05:50:41 +00:00
chris-kreidl commented 2026-12-04 05:23:46 +00:00 (Migrated from github.com)

Moves production off Vercel + Prisma Postgres onto the owner's VPS under Coolify.

What changed

  • Dockerfile (oven/bun:1.4, Nitro bun preset): runs prisma migrate deploy, then bun .output/server/index.mjs. Health check on /api/build. Sets NODE_ENV=production, MAP_TILE_DIR=/data/map-tiles, MAP_TILES_BESIDE_DB=1, SERVER_SHUTDOWN_TIMEOUT=60.
  • Background work: runInBackground (src/server/background.ts) replaces waitUntil in all six callers. nitro-shutdown.ts waits up to about 59 s on SIGTERM for that work to finish. Work that runs longer (a long re-plan) is cut off and recovers the way a dead worker already does.
  • Map tiles live on disk only; the Vercel Blob store and @vercel/blob are gone. map:fill and map:refresh run inside the container. Against a remote database they refuse to write unless MAP_TILES_BESIDE_DB=1.
  • Crons: scripts/cron.ts <name> calls the existing /api/cron/* routes with CRON_SECRET. It's meant for Coolify scheduled tasks.
  • Auth: auth-origins no longer reads VERCEL_*. Production (NODE_ENV) without BETTER_AUTH_URL throws.
  • Removed vercel.json, build:vercel, check-vercel-runtime.ts and @vercel/functions. Updated docs: architecture → Hosting, requirements, CLAUDE.md, PROGRESS, decisions, history.

How it was checked

  • Typecheck, lint and format pass. bun test: 1565 pass, 1 fail. The failure also happens on main because it depends on the date (#328).
  • bun run audit fails on main too, from a new cross-spawn advisory (#329), so CI's audit step will be red.
  • docker build succeeds. I ran the image against an empty Postgres database:
    • All migrations applied and the server started.
    • /api/build returned 200, the container reported healthy, and the security headers were present.
    • cron.ts missed-sessions returned 200. A wrong secret was refused.
    • fill-map-tiles --dry-run ran in the container.
    • docker stop shut the server down gracefully.

Cutover (owner, in Coolify)

Merging this does not deploy anywhere until Coolify is set up. Vercel keeps auto-deploying main until you disconnect it, and those builds fail now that vercel.json is gone, so production stays on the last good deploy.

  1. Postgres: New resource → PostgreSQL 18. Set up scheduled backups to S3-compatible storage (Backblaze B2, R2 or Vultr Object Storage). Don't make it public.
  2. App: New resource → this GitHub repo, branch main, build pack Dockerfile, port 3000.
    • Env vars: DATABASE_URL (the Postgres internal URL), BETTER_AUTH_SECRET, BETTER_AUTH_URL=https://trainer.kreidl.org, ANTHROPIC_API_KEY (a new key, since the old one expired), CRON_SECRET, STRAVA_CLIENT_ID, STRAVA_CLIENT_SECRET, TOKEN_ENCRYPTION_KEY. Copy every value except the API key from Vercel, and keep BETTER_AUTH_SECRET and TOKEN_ENCRYPTION_KEY exactly as they are, or sessions and Strava tokens stop working.
    • Persistent storage: a volume mounted at /data.
    • Advanced → Operations → Stop Grace Period: 75 (the default 30 s would kill the 60 s shutdown drain).
    • Watch paths, to skip docs-only deploys: !docs/**, !prompts/**, !**/*.md.
  3. Data, with downtime: pg_dump -Fc --no-owner --exclude-table-data='"MapTile"' --exclude-table-data='"MapTileClaim"' from Prisma Postgres (tile rows left out: they describe Blob's files, and a row without its file blocks the fill), then pg_restore --no-owner into the new database (an SSH tunnel to the VPS works). Then deploy.
  4. Map tiles: in the app's Coolify terminal, run bun scripts/fill-map-tiles.ts (no tile rows came over, so it fetches every ride's tiles).
  5. Scheduled tasks on the app (UTC, timeout 900 s): 0 9 * * * bun scripts/cron.ts missed-sessions, 10 9 * * * … strava-uploads, 20 9 * * * … map-tiles.
  6. DNS: point trainer.kreidl.org at the VPS and set it as the app's domain (Coolify issues the TLS certificate). Sign in with a passkey to check.
  7. Vercel: disconnect Git, then delete the project and the Blob stores once things look good. Delete Prisma Postgres after a week of good backups.

🤖 Generated with Claude Code

Moves production off Vercel + Prisma Postgres onto the owner's VPS under Coolify. ## What changed - **`Dockerfile`** (`oven/bun:1.4`, Nitro `bun` preset): runs `prisma migrate deploy`, then `bun .output/server/index.mjs`. Health check on `/api/build`. Sets `NODE_ENV=production`, `MAP_TILE_DIR=/data/map-tiles`, `MAP_TILES_BESIDE_DB=1`, `SERVER_SHUTDOWN_TIMEOUT=60`. - **Background work:** `runInBackground` (`src/server/background.ts`) replaces `waitUntil` in all six callers. `nitro-shutdown.ts` waits up to about 59 s on SIGTERM for that work to finish. Work that runs longer (a long re-plan) is cut off and recovers the way a dead worker already does. - **Map tiles** live on disk only; the Vercel Blob store and `@vercel/blob` are gone. `map:fill` and `map:refresh` run inside the container. Against a remote database they refuse to write unless `MAP_TILES_BESIDE_DB=1`. - **Crons:** `scripts/cron.ts <name>` calls the existing `/api/cron/*` routes with `CRON_SECRET`. It's meant for Coolify scheduled tasks. - **Auth:** `auth-origins` no longer reads `VERCEL_*`. Production (`NODE_ENV`) without `BETTER_AUTH_URL` throws. - Removed `vercel.json`, `build:vercel`, `check-vercel-runtime.ts` and `@vercel/functions`. Updated docs: architecture → Hosting, requirements, CLAUDE.md, PROGRESS, decisions, history. ## How it was checked - Typecheck, lint and format pass. `bun test`: 1565 pass, 1 fail. The failure also happens on `main` because it depends on the date (#328). - `bun run audit` fails on `main` too, from a new cross-spawn advisory (#329), so CI's audit step will be red. - `docker build` succeeds. I ran the image against an empty Postgres database: - All migrations applied and the server started. - `/api/build` returned 200, the container reported healthy, and the security headers were present. - `cron.ts missed-sessions` returned 200. A wrong secret was refused. - `fill-map-tiles --dry-run` ran in the container. - `docker stop` shut the server down gracefully. ## Cutover (owner, in Coolify) Merging this **does not deploy anywhere until Coolify is set up**. Vercel keeps auto-deploying `main` until you disconnect it, and those builds fail now that `vercel.json` is gone, so production stays on the last good deploy. 1. **Postgres:** New resource → PostgreSQL 18. Set up scheduled backups to S3-compatible storage (Backblaze B2, R2 or Vultr Object Storage). Don't make it public. 2. **App:** New resource → this GitHub repo, branch `main`, build pack **Dockerfile**, port 3000. - Env vars: `DATABASE_URL` (the Postgres internal URL), `BETTER_AUTH_SECRET`, `BETTER_AUTH_URL=https://trainer.kreidl.org`, `ANTHROPIC_API_KEY` (**a new key**, since the old one expired), `CRON_SECRET`, `STRAVA_CLIENT_ID`, `STRAVA_CLIENT_SECRET`, `TOKEN_ENCRYPTION_KEY`. Copy every value except the API key from Vercel, and keep `BETTER_AUTH_SECRET` and `TOKEN_ENCRYPTION_KEY` exactly as they are, or sessions and Strava tokens stop working. - Persistent storage: a volume mounted at `/data`. - Advanced → Operations → **Stop Grace Period: 75** (the default 30 s would kill the 60 s shutdown drain). - Watch paths, to skip docs-only deploys: `!docs/**`, `!prompts/**`, `!**/*.md`. 3. **Data, with downtime:** `pg_dump -Fc --no-owner --exclude-table-data='"MapTile"' --exclude-table-data='"MapTileClaim"'` from Prisma Postgres (tile rows left out: they describe Blob's files, and a row without its file blocks the fill), then `pg_restore --no-owner` into the new database (an SSH tunnel to the VPS works). Then deploy. 4. **Map tiles:** in the app's Coolify terminal, run `bun scripts/fill-map-tiles.ts` (no tile rows came over, so it fetches every ride's tiles). 5. **Scheduled tasks** on the app (UTC, timeout 900 s): `0 9 * * *` `bun scripts/cron.ts missed-sessions`, `10 9 * * *` `… strava-uploads`, `20 9 * * *` `… map-tiles`. 6. **DNS:** point `trainer.kreidl.org` at the VPS and set it as the app's domain (Coolify issues the TLS certificate). Sign in with a passkey to check. 7. **Vercel:** disconnect Git, then delete the project and the Blob stores once things look good. Delete Prisma Postgres after a week of good backups. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
vercel[bot] commented 2026-12-04 05:23:51 +00:00 (Migrated from github.com)

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
trainer Error Error Dec 4, 2026 5:47am UTC

Request Review

[vc]: #C/tqOugn70wF+xNkcyqnXomYVKHmPYB2/27xWb/XY14=:eyJpc01vbm9yZXBvIjp0cnVlLCJ0eXBlIjoiZ2l0aHViIiwicHJvamVjdHMiOlt7Im5hbWUiOiJ0cmFpbmVyIiwicHJvamVjdElkIjoicHJqX3JkNDRKRHhybEp0cjBSd2lXVFRyREMwU2tyR0wiLCJsaXZlRmVlZGJhY2siOnsicmVzb2x2ZWQiOjAsInVucmVzb2x2ZWQiOjAsInRvdGFsIjowLCJsaW5rIjoiIn0sImluc3BlY3RvclVybCI6Imh0dHBzOi8vdmVyY2VsLmNvbS9jaHJpc2tyZWlkbHMtcHJvamVjdHMvdHJhaW5lci80NXR4RXlHYnN1bjdUSmdwQURXTWtITEwyRnVVIiwicHJldmlld1VybCI6IiIsIm5leHRDb21taXRTdGF0dXMiOiJGQUlMRUQiLCJyb290RGlyZWN0b3J5IjpudWxsfV0sInJlcXVlc3RSZXZpZXdVcmwiOiJodHRwczovL3ZlcmNlbC5jb20vdmVyY2VsLWFnZW50L3JlcXVlc3QtcmV2aWV3P293bmVyPWNocmlzLWtyZWlkbCZyZXBvPXRyYWluZXImcHI9MzMwIn0= The latest updates on your projects. Learn more about [Vercel for GitHub](https://vercel.link/github-learn-more). | Project | Deployment | Actions | Updated | | :--- | :----- | :------ | :------ | | <a href="https://vercel.com/chriskreidls-projects/trainer"><sup><img src="https://vercel.com/api/www/avatar?projectId=prj_rd44JDxrlJtr0RwiWTTrDC0SkrGL&teamId=team_tRaZIbYBlKD7c14W9RBhwAFZ&s=32" width="16" height="16" align="middle" alt="" /></sup></a> [trainer](https://vercel.com/chriskreidls-projects/trainer) | ![Error](https://vercel.com/static/status/error.svg) [Error](https://vercel.com/chriskreidls-projects/trainer/45txEyGbsun7TJgpADWMkHLL2FuU) | | <relative-time datetime="2026-12-04T05:47:07.676Z">Dec 4, 2026 5:47am UTC</relative-time> | <a href="https://vercel.com/vercel-agent/request-review?owner=chris-kreidl&repo=trainer&pr=330" rel="noreferrer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://agents-vade-review.vercel.sh/request-review-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://agents-vade-review.vercel.sh/request-review-light.svg"><img src="https://agents-vade-review.vercel.sh/request-review-light.svg" alt="Request Review"></picture></a>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ckreidl/trainer!330
No description provided.