bugfix: proxy /api/* through the SvelteKit container
The compose deploy was unreachable because frontend code reads its API base from `import.meta.env.VITE_API_BASE` at build time, but the shipped image baked in the fallback `/api` and never picked up the `PUBLIC_API_BASE` env var. The browser then hit http://localhost:3000/api/...which the Node adapter doesn't serve, so every request 404'd. Fix the topology at the right layer: hooks.server.ts proxies /api/* requests through to the backend container over docker's internal network. The browser only ever talks to :3000, cookies stay same-origin, and CORS can stay empty. - frontend/src/hooks.server.ts: new proxy. Reads BACKEND_URL (defaults to http://localhost:8080 for ad-hoc node builds). Strips `host` and `content-length` so the backend sees the real client request and recomputes the length. Sets `duplex: 'half'` for streamed POST bodies. GET/HEAD have no body. Non-/api paths fall through to SvelteKit normally. - docker-compose.yml: drop the host port mapping on the backend (browser doesn't reach it directly anymore — use `ports:` instead of `expose:` if you want curl access). Set BACKEND_URL=http://backend:8080 on the frontend service. Drop PUBLIC_API_BASE which was unused. - .env.example: replace PUBLIC_API_BASE with BACKEND_URL, with a note on what it does. - README: explain the new topology in Quick start, update the bot curl examples to hit :3000 (since that's the only published port in the default deploy), and call out that the TLS terminator only needs one upstream now. Lockstep version bump to 0.9.1. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
25
README.md
25
README.md
@@ -16,11 +16,14 @@ cp .env.example .env
|
||||
docker compose up --build
|
||||
```
|
||||
|
||||
| Service | URL |
|
||||
| ----------------- | ---------------------------------- |
|
||||
| Frontend | <http://localhost:3000> |
|
||||
| API base | <http://localhost:8080/api/v1> |
|
||||
| Health check | <http://localhost:8080/api/v1/health> |
|
||||
| Service | URL |
|
||||
| -------------------------- | ------------------------------------------------------------------ |
|
||||
| Frontend (and API browser) | <http://localhost:3000> |
|
||||
| API health | <http://localhost:3000/api/v1/health> |
|
||||
|
||||
The browser only ever talks to the frontend container on `:3000`. SvelteKit's [`hooks.server.ts`](frontend/src/hooks.server.ts) reverse-proxies `/api/*` to the backend service over docker's internal network, so cookies stay same-origin and you don't need to publish the backend port or configure CORS to get a working deploy.
|
||||
|
||||
If you want to hit the backend directly (bot scripts, ops debugging), publish its port by editing the backend service in [docker-compose.yml](docker-compose.yml) — change `expose` to `ports: ["8080:8080"]`.
|
||||
|
||||
The first boot runs the migrations automatically. From there:
|
||||
|
||||
@@ -142,21 +145,23 @@ The frontend handles this for you: register → cookie set → writes work. Cook
|
||||
|
||||
### Bots / scripts (bearer tokens)
|
||||
|
||||
The frontend on `:3000` proxies `/api/*` through to the backend, so any URL below works against `http://localhost:3000` in the default compose deploy. If you publish the backend port directly, swap in `http://localhost:8080`.
|
||||
|
||||
```bash
|
||||
# 1. Log in once via cookies (or register).
|
||||
curl -sb -c cookies.txt -X POST http://localhost:8080/api/v1/auth/login \
|
||||
curl -sb -c cookies.txt -X POST http://localhost:3000/api/v1/auth/login \
|
||||
-H 'content-type: application/json' \
|
||||
-d '{"username":"alice","password":"hunter2hunter2"}'
|
||||
|
||||
# 2. Mint a long-lived bot token. The `bearer` value is shown ONCE.
|
||||
curl -sb cookies.txt -X POST http://localhost:8080/api/v1/auth/tokens \
|
||||
curl -sb cookies.txt -X POST http://localhost:3000/api/v1/auth/tokens \
|
||||
-H 'content-type: application/json' \
|
||||
-d '{"name":"ci-bot"}'
|
||||
# → { "id": "...", "name": "ci-bot", "bearer": "raw-token-here", ... }
|
||||
|
||||
# 3. Use the bearer from anywhere.
|
||||
curl -H 'Authorization: Bearer raw-token-here' \
|
||||
http://localhost:8080/api/v1/auth/me
|
||||
http://localhost:3000/api/v1/auth/me
|
||||
```
|
||||
|
||||
Tokens are stored hashed (sha256) at rest; the raw value never leaves the response that created it. Revoke with `DELETE /api/v1/auth/tokens/{id}` while authenticated as the owner.
|
||||
@@ -177,13 +182,13 @@ All variables can be set in `.env` (for `docker compose`) or your shell (for `ca
|
||||
| `CORS_ALLOWED_ORIGINS` | (empty → same-origin) | Comma-separated origin allowlist. |
|
||||
| `MAX_REQUEST_BYTES` | `209715200` (200 MiB) | Hard cap on multipart request size. |
|
||||
| `MAX_FILE_BYTES` | `20971520` (20 MiB) | Cap on a single image part. |
|
||||
| `PUBLIC_API_BASE` | `http://localhost:8080/api` | Browser-facing API base. |
|
||||
| `BACKEND_URL` | `http://backend:8080` | Where the frontend's `/api/*` proxy points. |
|
||||
|
||||
## Deployment
|
||||
|
||||
For real hosts:
|
||||
|
||||
- **Front Mangalord with a TLS terminator** (Caddy, nginx, traefik). Point `:443` at the frontend on `:3000` and proxy `/api/*` to the backend on `:8080`. With same-origin routing you can leave `CORS_ALLOWED_ORIGINS` empty and the session cookie's `SameSite=Lax` does its job.
|
||||
- **Front Mangalord with a TLS terminator** (Caddy, nginx, traefik). Point `:443` at the frontend container on `:3000` — the SvelteKit proxy handles `/api/*` internally, so you only need one upstream. With same-origin routing you can leave `CORS_ALLOWED_ORIGINS` empty and the session cookie's `SameSite=Lax` does its job.
|
||||
- **Set a strong Postgres password** in `.env` before the first `docker compose up`. The defaults are fine for local dev only.
|
||||
- **Keep `COOKIE_SECURE=true`** behind HTTPS. Browsers drop `Secure` cookies on plain HTTP; the dev compose accepts `COOKIE_SECURE=false` for that case.
|
||||
- **Watch `RUST_LOG`** if you're noisy with `debug` — drop the `,mangalord=debug` suffix in production to log at `info` for the app's spans.
|
||||
|
||||
Reference in New Issue
Block a user