added reverse proxy

This commit is contained in:
Jakub Dorfman 2026-06-05 15:32:23 +02:00
parent afd7fd7686
commit 246b1990ca
57 changed files with 4174 additions and 4139 deletions

View file

@ -5,7 +5,7 @@
- **SvelteKit 2** (SSR, Svelte 5 runes mode) — all `.svelte` files use `$state`, `$derived`, `$props()` etc.
- **PocketBase** — REST API via the `pocketbase` SDK (v0.26). Auth, DB, and image storage.
- **Tailwind CSS v4** + **DaisyUI 5** — utility-first CSS with DaisyUI components for admin; custom theme for the public site.
- **Docker Compose** — two services: `app` (SvelteKit) and `pocketbase`.
- **Docker Compose** — three services: `app` (SvelteKit), `pocketbase`, and `caddy` (reverse proxy).
## Project Structure
@ -88,18 +88,19 @@ npm run build
# Type checking
npm run check
# Docker full stack
# Docker full stack (single entry point on port 8080)
docker compose up --build
# App: http://localhost:3000
# PocketBase admin: http://localhost:8090/_/
# App + PocketBase via Caddy: http://localhost:8080
# PocketBase admin: http://localhost:8080/_/
# PocketBase only (for local dev)
# PocketBase only (for local dev outside Docker)
docker compose up pocketbase
# Direct access: http://localhost:8090/_/
```
## Environment
- `PUBLIC_POCKETBASE_URL` — public-facing PocketBase URL (default `http://localhost:8090`)
- `PUBLIC_POCKETBASE_URL` — public-facing PocketBase URL, now accessed through the Caddy reverse proxy (default `http://localhost:8080`)
- `POCKETBASE_INTERNAL_URL` — internal Docker network URL (`http://pocketbase:80`)
- `ORIGIN` — SvelteKit origin for SSR (`http://localhost:3000`)
- `ORIGIN` — SvelteKit origin for SSR (`http://localhost:8080`)
- Copy `app/.env.example` → `app/.env` for local dev

17
Caddyfile Normal file
View file

@ -0,0 +1,17 @@
# Reverse proxy — single port for both the SvelteKit app and PocketBase
# Route: /api/* and /_/* → PocketBase
# everything else → SvelteKit app
:8080 {
handle /api/* {
reverse_proxy pocketbase:80
}
handle /_/* {
reverse_proxy pocketbase:80
}
handle {
reverse_proxy app:3000
}
}

View file

@ -7,7 +7,7 @@ A full-stack SvelteKit CMS for a single-operator massage studio. PocketBase prov
- SvelteKit + SSR
- PocketBase REST API via the `pocketbase` SDK
- Tailwind CSS v4 + DaisyUI
- Docker Compose with two services: `app` and `pocketbase`
- Docker Compose with three services: `app`, `pocketbase`, and `caddy` (reverse proxy)
## Local development
@ -28,6 +28,8 @@ A full-stack SvelteKit CMS for a single-operator massage studio. PocketBase prov
```
5. Visit `http://localhost:5173`.
> **Note:** During local development (outside Docker), PocketBase is accessed directly at `http://localhost:8090`. When running the full Docker stack, everything goes through the Caddy reverse proxy on port `8080`.
## Docker Compose
Run the full stack:
@ -45,19 +47,20 @@ docker compose up
Subsequent builds reuse the npm cache declared in the `Dockerfile`.
- Public SvelteKit app: `http://localhost:3000`
- PocketBase admin UI: `http://localhost:8090/_/`
- Single entry point (Caddy reverse proxy): **`http://localhost:8080`**
- SvelteKit app: `http://localhost:8080/` (or any non-API path)
- PocketBase admin UI: `http://localhost:8080/_/`
- PocketBase REST API: `http://localhost:8080/api/`
- PocketBase direct access (internal only, not exposed): `http://localhost:8090`
The app container uses two PocketBase URLs:
- `POCKETBASE_INTERNAL_URL=http://pocketbase:80` for server-side SvelteKit calls from the app container when using the included `spectado/pocketbase` image.
- `PUBLIC_POCKETBASE_URL=http://localhost:8090` for browser-side admin uploads and public file URLs during local Docker development.
In production, set `PUBLIC_POCKETBASE_URL` to the browser-accessible PocketBase URL, or to the reverse-proxied PocketBase path/origin you expose publicly.
- `PUBLIC_POCKETBASE_URL=http://localhost:8080` for browser-side admin uploads and public file URLs. Since the Caddy reverse proxy routes `/api/*` to PocketBase, the public URL points to the proxy.
## PocketBase setup
1. Open `http://localhost:8090/_/`.
1. Open `http://localhost:8080/_/` (or `http://localhost:8090/_/` for direct access).
2. Create the first PocketBase admin user when prompted.
3. Import `pb_schema.json` from the PocketBase admin UI collection settings/import flow. This file uses the newer PocketBase `fields` schema format; if PocketBase still reports `Invalid collections configuration`, create the three collections manually using the fields below.
4. Create starter records:

View file

@ -1 +1 @@
PUBLIC_POCKETBASE_URL=http://localhost:8090
PUBLIC_POCKETBASE_URL=http://localhost:8080

View file

@ -5,7 +5,7 @@ import { createPocketBase } from '$lib/pb';
export const handle: Handle = async ({ event, resolve }) => {
event.locals.pb = createPocketBase(
event.request.headers.get('cookie') ?? '',
env.POCKETBASE_INTERNAL_URL || env.PUBLIC_POCKETBASE_URL || 'http://localhost:8090'
env.POCKETBASE_INTERNAL_URL || env.PUBLIC_POCKETBASE_URL || 'http://localhost:8080'
);
event.locals.user = event.locals.pb.authStore.record;

View file

@ -2,7 +2,7 @@ import PocketBase from 'pocketbase';
import { env } from '$env/dynamic/public';
import { browser } from '$app/environment';
export const pbUrl = env.PUBLIC_POCKETBASE_URL || 'http://localhost:8090';
export const pbUrl = env.PUBLIC_POCKETBASE_URL || 'http://localhost:8080';
export function createPocketBase(authCookie?: string, url = pbUrl) {
const pb = new PocketBase(url);

View file

@ -37,7 +37,7 @@ export const POST: RequestHandler = async ({ request, locals }) => {
formData.append('alt', filename.replace(/\.[^/.]+$/, '').replace(/[-_]/g, ' '));
const record = await locals.pb.collection('media').create(formData);
const publicUrl = env.PUBLIC_POCKETBASE_URL || 'http://localhost:8090';
const publicUrl = env.PUBLIC_POCKETBASE_URL || 'http://localhost:8080';
const newUrl = `${publicUrl}/api/files/${record.collectionId}/${record.id}/${record.file}`;
keyToUrl.set(key, newUrl);

View file

@ -17,7 +17,7 @@ export const POST: RequestHandler = async ({ request, locals }) => {
try {
const record = await locals.pb.collection('media').create(uploadForm);
const publicUrl = env.PUBLIC_POCKETBASE_URL || 'http://localhost:8090';
const publicUrl = env.PUBLIC_POCKETBASE_URL || 'http://localhost:8080';
const url = `${publicUrl}/api/files/${record.collectionId}/${record.id}/${record.file}`;
return json({ record, url });
} catch (e) {

View file

@ -3,8 +3,9 @@ services:
image: spectado/pocketbase:latest
container_name: massage-pocketbase
restart: unless-stopped
ports:
- "8090:80"
# Internal only — accessed via the caddy reverse proxy
expose:
- "80"
volumes:
- pocketbase:/pb/pb_data
@ -18,11 +19,24 @@ services:
- pocketbase
environment:
POCKETBASE_INTERNAL_URL: http://pocketbase:80
PUBLIC_POCKETBASE_URL: http://localhost:8090
ORIGIN: http://localhost:3000
PUBLIC_POCKETBASE_URL: http://localhost:8080
ORIGIN: http://localhost:8080
BODY_SIZE_LIMIT: 5242880 # 5MB
# Internal only — served via the caddy reverse proxy
expose:
- "3000"
caddy:
image: caddy:2-alpine
container_name: massage-caddy
restart: unless-stopped
ports:
- "3000:3000"
- "8080:8080"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile:ro
depends_on:
- app
- pocketbase
volumes:
pocketbase: