No description
  • TypeScript 96%
  • CSS 2.2%
  • Dockerfile 0.8%
  • PLpgSQL 0.7%
  • HTML 0.3%
Find a file
Lars Hampe c5173228a9
Some checks failed
ci / check (push) Failing after 1m39s
ci / e2e (push) Failing after 4m28s
Merge pull request 'Organizations, invitation page, member management and live updates' (#2) from feat/organizations into main
Reviewed-on: #2
2026-11-24 10:46:06 +01:00
.forgejo/workflows Avatar upload to S3 (RustFS), stored in Better Auth's user.image 2026-11-23 21:25:38 +01:00
apps feat: live updates via Postgres NOTIFY and oRPC event streams 2026-11-23 23:36:45 +01:00
e2e feat: live updates via Postgres NOTIFY and oRPC event streams 2026-11-23 23:36:45 +01:00
scripts Avatar upload to S3 (RustFS), stored in Better Auth's user.image 2026-11-23 21:25:38 +01:00
.dockerignore App Starter: full-stack TypeScript boilerplate 2026-11-23 20:26:55 +01:00
.gitignore App Starter: full-stack TypeScript boilerplate 2026-11-23 20:26:55 +01:00
.oxfmtrc.json App Starter: full-stack TypeScript boilerplate 2026-11-23 20:26:55 +01:00
.oxlintrc.json App Starter: full-stack TypeScript boilerplate 2026-11-23 20:26:55 +01:00
bun.lock App Starter: full-stack TypeScript boilerplate 2026-11-23 20:26:55 +01:00
casting.yaml App Starter: full-stack TypeScript boilerplate 2026-11-23 20:26:55 +01:00
casting.yaml.lock App Starter: full-stack TypeScript boilerplate 2026-11-23 20:26:55 +01:00
CLAUDE.md feat: live updates via Postgres NOTIFY and oRPC event streams 2026-11-23 23:36:45 +01:00
docker-compose.prod.yml Avatar upload to S3 (RustFS), stored in Better Auth's user.image 2026-11-23 21:25:38 +01:00
docker-compose.yml feat: expose mailpit ports 2026-11-23 21:33:23 +01:00
Dockerfile App Starter: full-stack TypeScript boilerplate 2026-11-23 20:26:55 +01:00
package.json Avatar upload to S3 (RustFS), stored in Better Auth's user.image 2026-11-23 21:25:38 +01:00
README.md feat: live updates via Postgres NOTIFY and oRPC event streams 2026-11-23 23:36:45 +01:00
tsconfig.json App Starter: full-stack TypeScript boilerplate 2026-11-23 20:26:55 +01:00

App Starter

Full-stack TypeScript boilerplate: Bun + oRPC + Drizzle/Postgres + Better Auth on the server, React + TanStack Router/Query/Form + shadcn/ui + Lingui in the client. One Docker image for production.

Quickstart

Needs Bun 1.4+ and Docker.

bun run setup   # .env with a fresh secret, install, start Postgres/Mailpit/RustFS (+ bucket), migrate
bun run dev     # server :3000 + client :5173

New project from this boilerplate

git clone https://git.hashdot.co/hampe.work/app-starter.git my-app && cd my-app
rm -rf .git && git init
bun run setup my-app "My App"   # renames everything (package names, DB, titles, texts), then sets up

Structure

apps/
  server/                 Bun, node:http
    index.ts              entry: listen + graceful shutdown
    app.ts                HTTP routing: /api/auth (Better Auth), /rpc + /api (oRPC), /docs, /health, client
    env.ts                every env var, validated at startup
    contract.ts           API contract the client imports (types only)
    router.ts             contract implementations
    <feature>/            <feature>.contract.ts, <feature>.implement.ts, <feature>.test.ts
    live/                 live updates: Postgres NOTIFY -> SSE stream -> clients refetch
    schema/               Drizzle tables (auth.ts is generated by Better Auth)
    drizzle/              generated SQL migrations
    middleware/auth.ts    requireAuth / withSession
    test/                 test DB setup + helpers, HTTP tests
  client/                 Vite + React
    src/routes/           file-based routes; _authed/* require login + an organization
    src/components/ui/    shadcn/ui (add with: cd apps/client && bunx shadcn add <name>)
    src/components/form.tsx   TanStack Form setup (useAppForm)
    src/lib/              orpc client, auth client, i18n, live updates (live.ts)
    src/locales/          Lingui catalogs (de, en)
scripts/setup.ts          setup / rename

Scripts

Command What
bun run dev server (watch) + client (Vite)
bun run check typecheck, lint, format check, tests (what CI runs)
bun run test server tests against a real Postgres (*_test DB, see below)
bun run e2e Playwright smoke tests (needs bun run db:up, see below)
bun run format / lint / typecheck oxfmt / oxlint / tsc
bun run build client production build
bun run db:up / db:down dev Postgres + Mailpit + RustFS (docker-compose.yml), bucket
bun run db:generate SQL migration from schema changes
bun run db:migrate apply migrations
bun run db:studio Drizzle Studio
bun run auth:generate regenerate schema/auth.ts after changing Better Auth plugins
bun run --filter client extract extract new UI texts into src/locales/*/messages.po

Adding a feature

Copy apps/server/todo/ + apps/client/src/routes/_authed/todos.tsx (the example feature) as the template:

  1. Table: apps/server/schema/<feature>.ts, export it from schema/index.ts, relations in schema/relations.ts. Then bun run db:generate && bun run db:migrate.
  2. Contract: <feature>/<feature>.contract.ts with zod input/output, register it in contract.ts.
  3. Implementation: <feature>/<feature>.implement.ts (implement(contract).$context<BaseContext>().use(requireAuth)), register it in router.ts. Data of an organization (the normal case)? Also .use(requireOrganization), like the todos.
  4. Test: <feature>/<feature>.test.ts with call(router.x.y, input, { context: testContext(user) }).
  5. UI: a route in apps/client/src/routes/_authed/, data via orpc.<feature>.<proc>.queryOptions() / .mutationOptions(). New texts: bun run --filter client extract, translate de/messages.po.
  6. Live (others' changes show up without reload): a trigger live_notify('<topic>', 'organization_id') on the table in a custom migration (bun run --filter server db:generate -- --custom --name <name>, see drizzle/*_live), the topic in live/live.contract.ts and what to refetch for it in apps/client/src/lib/live.ts.

Live updates

Every change to a watched table (members, invitations, organizations, todos) fires pg_notify on commit. Each server instance LISTENs and passes a signal ({ topic, organizationId }, never data) to the open streams of the affected users (live.subscribe, oRPC event iterator = SSE). The client refetches what that topic shows; a removed member is moved on right away. One stream per browser: one tab holds it (Web Locks) and forwards the events (BroadcastChannel), so tabs don't use up HTTP/1.1's 6 connections. After a gap everything is refetched.

Auth

Better Auth with email + password under /api/auth/*.

  • Sign-up sends a confirmation link; sign-in is refused (EMAIL_NOT_VERIFIED, with a fresh link mailed) until it was clicked. The link signs the user in. Don't want that? requireEmailVerification: false in utils/auth.ts.

  • Forgot password: /forgot-password mails a link -> /reset-password sets a new one and signs out all sessions.

  • Mails go out via SMTP_URL (utils/mail.ts, plain text, English). Links point at BETTER_AUTH_URL, so in dev that is the Vite URL (http://localhost:5173), in production the public URL.

  • Browser: session cookie (same origin; in dev Vite proxies /api and /rpc to the server).

  • API clients / mobile: sign in, take the set-auth-token response header, send Authorization: Bearer <token>.

  • In procedures: .use(requireAuth) gives context.user and context.session.

Uploads (avatars)

Files go to S3-compatible storage (S3_* in env.ts) through Bun's built-in S3Client (utils/storage.ts). In dev that is RustFS from docker-compose.yml; bun run db:up (and so bun run setup) creates the bucket via the one-shot storage-init service, a no-op once it exists.

  • user.setAvatar (dashboard: "Change picture") checks the file by its first bytes (PNG, JPEG, GIF, WebP; max. 2 MB), stores it as avatars/<uuid>.<ext>, writes /api/avatars/<uuid>.<ext> into Better Auth's user.image and deletes the previous upload.
  • The server serves /api/avatars/* from the bucket (cached forever, every upload gets a new name), so the bucket stays private and needs no CORS or public URL.
  • Tests mock the storage (spyOn(storage, 'write'), see user/user.test.ts); the E2E tests use the real one.

Tests

bun test (in apps/server) loads .env.test: a separate *_test database that is created and migrated automatically and truncated before every test. Procedures are tested with oRPC's call(), HTTP behaviour in test/http.test.ts.

bun run e2e (Playwright, e2e/) builds the client and starts the server on :3100 the way the Docker image runs it, then clicks through sign-up (with the confirmation mail from Mailpit), todos, avatar upload and password reset. It uses the dev Postgres + Mailpit + RustFS, so bun run db:up first; the first run downloads Chromium. Failed runs leave a trace: bunx playwright show-trace e2e/test-results/<test>/trace.zip.

Environment

All variables are listed and validated in apps/server/env.ts; local values live in apps/server/.env (template: .env.example). A missing or invalid value stops the server at startup with a readable message.

Deployment (Docker)

# .env next to docker-compose.prod.yml (see the comment at its top):
#   BETTER_AUTH_SECRET, BETTER_AUTH_URL (public URL), POSTGRES_PASSWORD, S3_SECRET_ACCESS_KEY, SMTP_URL
docker compose -f docker-compose.prod.yml up -d --build
  • One image: the server serves the API and the built client on port 3000. Put a TLS reverse proxy (Caddy, Traefik, …) in front.
  • Uploads go to the RustFS container (not published, the app serves the avatars); storage-init creates the bucket before the app starts.
  • Migrations run on container start, then the server starts. /health checks the DB (Docker healthcheck).
  • docker stop shuts down gracefully (running requests finish, DB pool closed, spans flushed).

Observability

Request logs are evlog wide events (one structured event per request; add fields with useLogger().set({...})). With OTEL_EXPORTER_OTLP_ENDPOINT set, traces (HTTP, oRPC, Postgres) and logs go via OTLP to e.g. a self-hosted SigNoz (casting.yaml); without it, only console logs. Live streams are no requests: instead of an hours-long span they log live.connected / live.disconnected and trace each delivered change as a short live.dispatch span.

CI

.forgejo/workflows/ci.yml: bun run check against a Postgres service, then the client build; in parallel the E2E tests with Postgres + Mailpit + RustFS services (curl creates the bucket). For GitHub, see the comment at the top of the file.