diff --git a/README.md b/README.md index 4d0cdbc..fb4ee3e 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,9 @@ 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. -**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. That one file drives the whole stack: `BACKEND_ORIGIN` sets the proxy target, `PORT` moves the container's listener, the host-loopback publish and the deploy's health gate together, and any further key is passed into the container untouched (the service also declares the file as its optional `env_file:`). Compose reads it through `--env-file`: plain `podman compose up` uses the defaults in `compose.yaml`, so pass the file when you have one (`podman compose --env-file hezarfen_frontend.env up -d --build`). +**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. + +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. `deploy/hezarfen_frontend_compose.service` is a systemd **user** unit that brings the stack up at boot: diff --git a/compose.yaml b/compose.yaml index 19f7b6f..648ec02 100644 --- a/compose.yaml +++ b/compose.yaml @@ -8,12 +8,18 @@ # from deploy/hezarfen_frontend.env.example) # # Interpolation reads the --env-file arguments (hezarfen_frontend.env first, -# deploy-owned stack.env second). The `env_file:` on the service passes the -# same file into the container, so extra keys reach it without being listed in -# `environment:`. `environment:` wins over `env_file`, which is why every knob -# is interpolated here: one operator file then drives compose AND the -# container. PORT moves the listener, the published port and the healthcheck -# together; BACKEND_ORIGIN moves the proxy target. +# deploy-owned stack.env second); the service's `env_file:` passes that same +# file into the container. BACKEND_ORIGIN is deliberately NOT listed under +# `environment:` — a listed key wins over `env_file:`, which would shadow the +# file with this file's default whenever compose runs without --env-file. +# server.ts keeps the same default built in, so nothing is lost on a laptop and +# the operator's file is authoritative on a server. PORT stays listed: the +# publish above is interpolated from it, and one source has to move the +# listener and the publish together. +# +# The value is read when the container STARTS: editing the file needs a +# recreate (`podman compose up -d`, or `systemctl --user restart +# hezarfen_frontend_compose`), not a `podman restart`. services: frontend: @@ -35,14 +41,11 @@ services: required: false environment: # Listen port, read from hezarfen_frontend.env (passed as --env-file by - # the systemd unit and the deploy; see the header note). server.ts reads - # PORT, so this one value moves the container, the publish above and the - # healthcheck together. + # 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}" - # Remote backend origin; server.ts keeps browser traffic same-origin by - # proxying /api/* (HTTP and WebSocket) here. Must be reachable FROM - # INSIDE the container. - BACKEND_ORIGIN: ${BACKEND_ORIGIN:-https://hezarfen.dizey.sh} 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 c1d2008..ffca884 100644 --- a/deploy/hezarfen_frontend.env.example +++ b/deploy/hezarfen_frontend.env.example @@ -10,11 +10,17 @@ # 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`. # -# One file, two readers: `--env-file` feeds compose's ${VAR:-default} -# interpolation in compose.yaml, and `env_file:` passes the same file into the -# container. keys compose.yaml lists under `environment:` always win over -# `env_file:` — BACKEND_ORIGIN is one of them, and it takes effect through the -# interpolation, not through the container environment. +# 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 app reads exactly two keys today (server.ts): BACKEND_ORIGIN and PORT. # Any other key here is carried into the container untouched, unread. @@ -33,8 +39,10 @@ BACKEND_ORIGIN=https://hezarfen.dizey.sh # ── 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. Leave it at 5173 unless -# something else already owns that port on the server. +# 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. PORT=5173 # ── Image tag ──────────────────────────────────────────────────────────────── diff --git a/deploy/hezarfen_frontend_compose.service b/deploy/hezarfen_frontend_compose.service index 4a854a4..24abb20 100644 --- a/deploy/hezarfen_frontend_compose.service +++ b/deploy/hezarfen_frontend_compose.service @@ -12,9 +12,11 @@ # # Keys read from hezarfen_frontend.env (full docs in # deploy/hezarfen_frontend.env.example): -# BACKEND_ORIGIN the origin /api/* is proxied to; must be -# reachable from inside the container -# PORT listen port + published port, default 5173 +# 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 # # Env-only restart (re-runs `compose up -d`, recreating the container when its # environment changed):