forge
Explore
Log in

Don't you dare go Hollow

πŸ”Œ let the env file own BACKEND_ORIGIN

Browse files at this commit

8970f614bf35e8f341b10c3b13b1bd54ec8eeb8fAhmet Kaan GümüşParent: 70570e9
  • fix
    • πŸ”Œ let the env file own BACKEND_ORIGIN
Show full message
fix: πŸ”Œ let the env file own BACKEND_ORIGIN
- drop BACKEND_ORIGIN from the service environment: β€” a listed key wins over
  env_file, so it shadowed the operator file with the compose default on any
  up that omitted --env-file, which is exactly how a server ended up proxying
  /api to an address nobody chose
- server.ts already defaults to the same public origin, so the laptop path
  (no env file, no flags) behaves as before while the file is now authoritative
- PORT stays interpolated: the published port and the container listener are
  derived from one value, so it keeps both from drifting
- document that env values are read at container start: a file edit needs a
  recreate (podman compose up -d) and not podman restart
README.mdmodified+3 -1
@@ -65,7 +65,9 @@ podman compose down # stop
65 65
66 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:<PORT>:<PORT>`, 5173 by default β€” so a server deployment terminates TLS in a reverse proxy in front of it.66 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:<PORT>:<PORT>`, 5173 by default β€” so a server deployment terminates TLS in a reverse proxy in front of it.
67 67
68-**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`).68+**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.
69+
70+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.
69 71
70 `deploy/hezarfen_frontend_compose.service` is a systemd **user** unit that brings the stack up at boot:72 `deploy/hezarfen_frontend_compose.service` is a systemd **user** unit that brings the stack up at boot:
71 73
compose.yamlmodified+16 -13
@@ -8,12 +8,18 @@
8 # from deploy/hezarfen_frontend.env.example)8 # from deploy/hezarfen_frontend.env.example)
9 #9 #
10 # Interpolation reads the --env-file arguments (hezarfen_frontend.env first,10 # Interpolation reads the --env-file arguments (hezarfen_frontend.env first,
11-# deploy-owned stack.env second). The `env_file:` on the service passes the11+# deploy-owned stack.env second); the service's `env_file:` passes that same
12-# same file into the container, so extra keys reach it without being listed in12+# file into the container. BACKEND_ORIGIN is deliberately NOT listed under
13-# `environment:`. `environment:` wins over `env_file`, which is why every knob13+# `environment:` β€” a listed key wins over `env_file:`, which would shadow the
14-# is interpolated here: one operator file then drives compose AND the14+# file with this file's default whenever compose runs without --env-file.
15-# container. PORT moves the listener, the published port and the healthcheck15+# server.ts keeps the same default built in, so nothing is lost on a laptop and
16-# together; BACKEND_ORIGIN moves the proxy target.16+# the operator's file is authoritative on a server. PORT stays listed: the
17+# publish above is interpolated from it, and one source has to move the
18+# listener and the publish together.
19+#
20+# The value is read when the container STARTS: editing the file needs a
21+# recreate (`podman compose up -d`, or `systemctl --user restart
22+# hezarfen_frontend_compose`), not a `podman restart`.
17 23
18 services:24 services:
19 frontend:25 frontend:
@@ -35,14 +41,11 @@ services:
35 required: false41 required: false
36 environment:42 environment:
37 # Listen port, read from hezarfen_frontend.env (passed as --env-file by43 # Listen port, read from hezarfen_frontend.env (passed as --env-file by
38- # the systemd unit and the deploy; see the header note). server.ts reads44+ # the systemd unit and the deploy). server.ts reads PORT, so this one
39- # PORT, so this one value moves the container, the publish above and the45+ # value moves the container, the publish above and the healthcheck
40- # healthcheck together.46+ # together β€” and keeping it here is what stops the publish and the
47+ # listener from drifting when no --env-file is given.
41 PORT: "${PORT:-5173}"48 PORT: "${PORT:-5173}"
42- # Remote backend origin; server.ts keeps browser traffic same-origin by
43- # proxying /api/* (HTTP and WebSocket) here. Must be reachable FROM
44- # INSIDE the container.
45- BACKEND_ORIGIN: ${BACKEND_ORIGIN:-https://hezarfen.dizey.sh}
46 restart: unless-stopped49 restart: unless-stopped
47 healthcheck:50 healthcheck:
48 # Bun-based: the slim runtime image ships no curl. Reads the container's51 # Bun-based: the slim runtime image ships no curl. Reads the container's
deploy/hezarfen_frontend.env.examplemodified+15 -7
@@ -10,11 +10,17 @@
10 # owns it; compose only reads it (`podman compose --env-file`, plus `env_file`10 # owns it; compose only reads it (`podman compose --env-file`, plus `env_file`
11 # into the frontend container). Plain KEY=value lines: no quotes, no `export`.11 # into the frontend container). Plain KEY=value lines: no quotes, no `export`.
12 #12 #
13-# One file, two readers: `--env-file` feeds compose's ${VAR:-default}13+# One file, two paths into the stack:
14-# interpolation in compose.yaml, and `env_file:` passes the same file into the14+# * `env_file:` passes this file into the container as-is β€” keys the compose
15-# container. keys compose.yaml lists under `environment:` always win over15+# file does not list under `environment:` (BACKEND_ORIGIN among them) apply
16-# `env_file:` β€” BACKEND_ORIGIN is one of them, and it takes effect through the16+# with or without `--env-file`, and are read when the container STARTS.
17-# interpolation, not through the container environment.17+# Change a value, then recreate: `podman compose up -d` (or
18+# `systemctl --user restart hezarfen_frontend_compose`). `podman restart`
19+# keeps the old environment.
20+# * `--env-file` additionally feeds compose's own ${VAR:-default}
21+# interpolation. Only PORT needs that (the publish and the container
22+# listener are derived from one value); compose.yaml passes no other knob
23+# through it, so nothing here is shadowed by a compose default.
18 #24 #
19 # The app reads exactly two keys today (server.ts): BACKEND_ORIGIN and PORT.25 # The app reads exactly two keys today (server.ts): BACKEND_ORIGIN and PORT.
20 # Any other key here is carried into the container untouched, unread.26 # Any other key here is carried into the container untouched, unread.
@@ -33,8 +39,10 @@ BACKEND_ORIGIN=https://hezarfen.dizey.sh
33 # ── Listen port ──────────────────────────────────────────────────────────────39 # ── Listen port ──────────────────────────────────────────────────────────────
34 # One value moves three things: what server.ts listens on, the host-loopback40 # One value moves three things: what server.ts listens on, the host-loopback
35 # publish (127.0.0.1:<PORT>:<PORT> in compose.yaml) and the deploy's health41 # publish (127.0.0.1:<PORT>:<PORT> in compose.yaml) and the deploy's health
36-# gate, which reads this line back out of this file. Leave it at 5173 unless42+# gate, which reads this line back out of this file. Unlike BACKEND_ORIGIN this
37-# something else already owns that port on the server.43+# one is interpolated by compose, so it needs `--env-file` to take effect (the
44+# systemd unit and the deploy both pass it). Leave it at 5173 unless something
45+# else already owns that port on the server.
38 PORT=517346 PORT=5173
39 47
40 # ── Image tag ────────────────────────────────────────────────────────────────48 # ── Image tag ────────────────────────────────────────────────────────────────
deploy/hezarfen_frontend_compose.servicemodified+5 -3
@@ -12,9 +12,11 @@
12 #12 #
13 # Keys read from hezarfen_frontend.env (full docs in13 # Keys read from hezarfen_frontend.env (full docs in
14 # deploy/hezarfen_frontend.env.example):14 # deploy/hezarfen_frontend.env.example):
15-# BACKEND_ORIGIN the origin /api/* is proxied to; must be15+# BACKEND_ORIGIN the origin /api/* is proxied to; reaches
16-# reachable from inside the container16+# the container through env_file, so it needs
17-# PORT listen port + published port, default 517317+# no flag β€” but only on (re)start
18+# PORT listen port + published port (interpolated,
19+# hence the --env-file below), default 5173
18 #20 #
19 # Env-only restart (re-runs `compose up -d`, recreating the container when its21 # Env-only restart (re-runs `compose up -d`, recreating the container when its
20 # environment changed):22 # environment changed):