diff --git a/README.md b/README.md index fb4ee3e..1d3d884 100644 --- a/README.md +++ b/README.md @@ -63,9 +63,9 @@ podman compose logs -f frontend podman compose down # stop ``` -The `Containerfile` is a two-stage build: `bun install --frozen-lockfile` plus `bun run build` produce `dist/`, and `oven/bun:1-slim` serves it with `server.ts` — which also reverse-proxies `/api/*` (plain HTTP and the exam-room WebSocket) to `BACKEND_ORIGIN`. No nginx, no node_modules at runtime, no state: one container and no volumes. The port is published on loopback only — `127.0.0.1::`, 5173 by default — so a server deployment terminates TLS in a reverse proxy in front of it. +The `Containerfile` is a two-stage build: `bun install --frozen-lockfile` plus `bun run build` produce `dist/`, and `oven/bun:1-slim` serves it with `server.ts` — which also reverse-proxies `/api/*` (plain HTTP and the exam-room WebSocket) to `BACKEND_ORIGIN`. No nginx, no node_modules at runtime, no state: one container and no volumes. It runs with `network_mode: host`, so it shares the host's loopback and reaches a backend published on `127.0.0.1:7656` exactly as a host process would (a bridge network would resolve `127.0.0.1` to the container itself). Host networking means no port mapping: the app binds the host's interface directly, so set `HOST=127.0.0.1` and let the reverse proxy on the same loopback be the only door. -**Knobs live in two places and are never mixed.** Locally the `${VAR:-default}` interpolations in `compose.yaml` are enough: the `up` above works with zero extra files. On a server the operator copies `deploy/hezarfen_frontend.env.example` to `$HOME/hezarfen_frontend/hezarfen_frontend.env`, `chmod 0600` it and edits it — nothing automated creates, overwrites or uploads that file. The service declares it as `env_file:`, so **every key it carries reaches the container with no extra flag**; `BACKEND_ORIGIN` (the proxy target) is one of them, and any further key is passed through untouched. Only `PORT` is also interpolated by compose — it has to be, since the published port and the container listener come from one value — so give it `--env-file` if you change it (`podman compose --env-file hezarfen_frontend.env up -d --build`); the systemd unit and the deploy always do. +**Knobs live in two places and are never mixed.** On a laptop nothing is required: with no env file the app falls back to its own defaults (`BACKEND_ORIGIN=https://hezarfen.dizey.sh`, `HOST=0.0.0.0`, `PORT=5173`), so the `up` above works with zero extra files. On a server the operator copies `deploy/hezarfen_frontend.env.example` to `$HOME/hezarfen_frontend/hezarfen_frontend.env`, `chmod 0600` it and edits it — nothing automated creates, overwrites or uploads that file. The service declares it as `env_file:`, so **every key it carries reaches the container with no extra flag**; nothing else is interpolated from it, and the only compose-level variable is `HEZARFEN_TAG`, which the deploy owns in `stack.env`. Environment values are read when the container **starts**: after editing the file, recreate (`podman compose up -d`, or `systemctl --user restart hezarfen_frontend_compose`). `podman restart` keeps the old environment. diff --git a/compose.yaml b/compose.yaml index 648ec02..5316b93 100644 --- a/compose.yaml +++ b/compose.yaml @@ -30,22 +30,21 @@ services: # latest local build on a laptop. image: localhost/hezarfen_frontend:${HEZARFEN_TAG:-latest} container_name: hezarfen_frontend - ports: - # Loopback only: terminate TLS on a reverse proxy in front of this. Host - # and container port both follow PORT, so the publish can never drift - # from what the server listens on. - - "127.0.0.1:${PORT:-5173}:${PORT:-5173}" + # Host networking, on purpose: the process then shares the host's loopback, + # so BACKEND_ORIGIN=http://127.0.0.1:7656 (the backend's published loopback + # port) is reachable from inside the container exactly as it would be from + # a host process — a bridge network would resolve 127.0.0.1 to the + # container itself and refuse the connection. + # + # There is no `ports:` mapping under host networking: the app binds the + # host's interface directly. Keep it off the LAN with HOST=127.0.0.1 in + # hezarfen_frontend.env (server.ts binds what HOST says, 0.0.0.0 by + # default), leaving the reverse proxy on the same loopback as the only door. + network_mode: host env_file: # Written once by the operator on the server (0600); absent on a laptop. - path: hezarfen_frontend.env required: false - environment: - # Listen port, read from hezarfen_frontend.env (passed as --env-file by - # the systemd unit and the deploy). server.ts reads PORT, so this one - # value moves the container, the publish above and the healthcheck - # together — and keeping it here is what stops the publish and the - # listener from drifting when no --env-file is given. - PORT: "${PORT:-5173}" restart: unless-stopped healthcheck: # Bun-based: the slim runtime image ships no curl. Reads the container's diff --git a/deploy/hezarfen_frontend.env.example b/deploy/hezarfen_frontend.env.example index ffca884..35344a2 100644 --- a/deploy/hezarfen_frontend.env.example +++ b/deploy/hezarfen_frontend.env.example @@ -8,41 +8,40 @@ # # Nothing automated creates, overwrites or uploads this file — the operator # owns it; compose only reads it (`podman compose --env-file`, plus `env_file` -# into the frontend container). Plain KEY=value lines: no quotes, no `export`. +# into the frontend container). Plain KEY=value lines: no quotes, no `export`, +# no trailing comment. # -# One file, two paths into the stack: -# * `env_file:` passes this file into the container as-is — keys the compose -# file does not list under `environment:` (BACKEND_ORIGIN among them) apply -# with or without `--env-file`, and are read when the container STARTS. -# Change a value, then recreate: `podman compose up -d` (or -# `systemctl --user restart hezarfen_frontend_compose`). `podman restart` -# keeps the old environment. -# * `--env-file` additionally feeds compose's own ${VAR:-default} -# interpolation. Only PORT needs that (the publish and the container -# listener are derived from one value); compose.yaml passes no other knob -# through it, so nothing here is shadowed by a compose default. +# The service declares this file as `env_file:`, so every key below reaches the +# container with no extra flag and nothing is shadowed by a compose default. +# Values are read when the container STARTS: after editing, recreate +# (`podman compose up -d`, or `systemctl --user restart +# hezarfen_frontend_compose`). `podman restart` keeps the old environment. # -# The app reads exactly two keys today (server.ts): BACKEND_ORIGIN and PORT. -# Any other key here is carried into the container untouched, unread. +# The app reads three keys today (server.ts): BACKEND_ORIGIN, PORT, HOST. # ── Backend origin ─────────────────────────────────────────────────────────── # Where the container proxies /api/* — plain HTTP and the exam-room WebSocket # alike. The browser only ever talks to this frontend, so session cookies stay -# same-origin. Must be reachable FROM INSIDE the container. +# same-origin. # -# The default below is the public origin and always works. A -# container-to-container origin such as http://hezarfen_backend:7656 is NOT -# wired today: neither compose stack declares a shared podman network, and the -# backend publishes on host loopback only (127.0.0.1:7656). -BACKEND_ORIGIN=https://hezarfen.dizey.sh +# The frontend runs with host networking (`network_mode: host` in compose.yaml), +# so it shares the host's loopback and this is simply where the backend +# listens on this machine: the backend's compose publishes 127.0.0.1:7656. +# A bridge network would resolve 127.0.0.1 to the container itself and refuse +# the connection. +BACKEND_ORIGIN=http://127.0.0.1:7656 + +# ── Listen address ─────────────────────────────────────────────────────────── +# HOST=127.0.0.1 keeps the app on loopback: with host networking there is no +# port mapping in front of it, so 0.0.0.0 would put it on every interface and +# bypass the reverse proxy. The proxy (Ferron et al.) proxies to +# http://127.0.0.1: from this same machine. +HOST=127.0.0.1 # ── Listen port ────────────────────────────────────────────────────────────── -# One value moves three things: what server.ts listens on, the host-loopback -# publish (127.0.0.1:: in compose.yaml) and the deploy's health -# gate, which reads this line back out of this file. Unlike BACKEND_ORIGIN this -# one is interpolated by compose, so it needs `--env-file` to take effect (the -# systemd unit and the deploy both pass it). Leave it at 5173 unless something -# else already owns that port on the server. +# The port the app binds (and the port the reverse proxy must target). The +# deploy's health gate reads this line back out of this file, so the two cannot +# drift. PORT=5173 # ── Image tag ──────────────────────────────────────────────────────────────── diff --git a/deploy/hezarfen_frontend_compose.service b/deploy/hezarfen_frontend_compose.service index 24abb20..0cf8ec8 100644 --- a/deploy/hezarfen_frontend_compose.service +++ b/deploy/hezarfen_frontend_compose.service @@ -12,11 +12,13 @@ # # Keys read from hezarfen_frontend.env (full docs in # deploy/hezarfen_frontend.env.example): -# BACKEND_ORIGIN the origin /api/* is proxied to; reaches -# the container through env_file, so it needs -# no flag — but only on (re)start -# PORT listen port + published port (interpolated, -# hence the --env-file below), default 5173 +# BACKEND_ORIGIN the origin /api/* is proxied to; on this +# host that is http://127.0.0.1:7656, which +# the service can reach because compose runs +# it with host networking +# HOST bind address; 127.0.0.1 keeps the app on +# loopback behind the reverse proxy +# PORT listen port, default 5173 # # Env-only restart (re-runs `compose up -d`, recreating the container when its # environment changed): diff --git a/server.ts b/server.ts index 8020897..102cfd7 100644 --- a/server.ts +++ b/server.ts @@ -7,6 +7,10 @@ // container's runtime after `vite build`. const PORT = Number(Bun.env.PORT ?? 5173); +// Interface to bind. 0.0.0.0 is what a published container port needs; under +// host networking the process sits directly on the host, so a server sets +// HOST=127.0.0.1 and only the reverse proxy beside it can reach the app. +const HOST = Bun.env.HOST ?? "0.0.0.0"; // Where the backend lives on the compose network. Override in compose. const BACKEND_ORIGIN = Bun.env.BACKEND_ORIGIN ?? "https://hezarfen.dizey.sh"; const BACKEND_HTTP = BACKEND_ORIGIN.replace(/\/+$/, ""); @@ -70,7 +74,7 @@ async function proxyHttp(req: Request, url: URL): Promise { const server = Bun.serve({ port: PORT, - hostname: "0.0.0.0", + hostname: HOST, // Long-running exam sockets must not be reaped by an idle timeout; the // backend sends its own periodic state ticks but a quiet client can still // sit for minutes between frames. @@ -135,4 +139,4 @@ const server = Bun.serve({ }, }); -console.log(`serving dist/ on :${server.port}, proxying /api -> ${BACKEND_HTTP}`); +console.log(`serving dist/ on ${HOST}:${server.port}, proxying /api -> ${BACKEND_HTTP}`);