- TypeScript 96%
- CSS 2.2%
- Dockerfile 0.8%
- PLpgSQL 0.7%
- HTML 0.3%
|
|
||
|---|---|---|
| .forgejo/workflows | ||
| apps | ||
| e2e | ||
| scripts | ||
| .dockerignore | ||
| .gitignore | ||
| .oxfmtrc.json | ||
| .oxlintrc.json | ||
| bun.lock | ||
| casting.yaml | ||
| casting.yaml.lock | ||
| CLAUDE.md | ||
| docker-compose.prod.yml | ||
| docker-compose.yml | ||
| Dockerfile | ||
| package.json | ||
| README.md | ||
| tsconfig.json | ||
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
- App: http://localhost:5173
- API docs (Scalar): http://localhost:3000/docs
- Mail inbox (Mailpit, catches every mail the app sends): http://localhost:8025
- File storage console (RustFS, login
rustfs-dev/rustfs-dev-secret): http://localhost:9001/rustfs/console/
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:
- Table:
apps/server/schema/<feature>.ts, export it fromschema/index.ts, relations inschema/relations.ts. Thenbun run db:generate && bun run db:migrate. - Contract:
<feature>/<feature>.contract.tswith zod input/output, register it incontract.ts. - Implementation:
<feature>/<feature>.implement.ts(implement(contract).$context<BaseContext>().use(requireAuth)), register it inrouter.ts. Data of an organization (the normal case)? Also.use(requireOrganization), like the todos. - Test:
<feature>/<feature>.test.tswithcall(router.x.y, input, { context: testContext(user) }). - UI: a route in
apps/client/src/routes/_authed/, data viaorpc.<feature>.<proc>.queryOptions()/.mutationOptions(). New texts:bun run --filter client extract, translatede/messages.po. - 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>, seedrizzle/*_live), the topic inlive/live.contract.tsand what to refetch for it inapps/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: falseinutils/auth.ts. -
Forgot password:
/forgot-passwordmails a link ->/reset-passwordsets a new one and signs out all sessions. -
Mails go out via
SMTP_URL(utils/mail.ts, plain text, English). Links point atBETTER_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
/apiand/rpcto the server). -
API clients / mobile: sign in, take the
set-auth-tokenresponse header, sendAuthorization: Bearer <token>. -
In procedures:
.use(requireAuth)givescontext.userandcontext.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 asavatars/<uuid>.<ext>, writes/api/avatars/<uuid>.<ext>into Better Auth'suser.imageand 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'), seeuser/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-initcreates the bucket before the app starts. - Migrations run on container start, then the server starts.
/healthchecks the DB (Docker healthcheck). docker stopshuts 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.