From 5f63fb5ccee64c836df3b616c93ae23d922dc477 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 15 Sep 2026 19:32:19 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=94=8C=20default=20the=20backend?= =?UTF-8?q?=20origin=20to=20the=20co-located=20loopback=20-=20server.ts=20?= =?UTF-8?q?and=20the=20vite=20dev=20proxy=20now=20default=20BACKEND=5FORIG?= =?UTF-8?q?IN=20to=20=20=20http://127.0.0.1:7656=20instead=20of=20the=20pu?= =?UTF-8?q?blic=20origin=20=E2=80=94=20that=20is=20where=20the=20=20=20com?= =?UTF-8?q?pose=20stack=20publishes=20the=20backend=20on=20this=20machine,?= =?UTF-8?q?=20and=20what=20the=20frontend=20=20=20container=20reaches=20un?= =?UTF-8?q?der=20host=20networking=20-=20nothing=20in=20a=20browser=20bund?= =?UTF-8?q?le=20changes:=20the=20SPA=20still=20calls=20its=20own=20origin?= =?UTF-8?q?=20and=20=20=20the=20server=20owns=20the=20backend=20address=20?= =?UTF-8?q?-=20README:=20proxy=20note=20and=20the=20defaults=20list=20foll?= =?UTF-8?q?ow=20the=20new=20fallback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- server.ts | 7 +++++-- vite.config.ts | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 941772c..c849ab2 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ bun run build Open [http://localhost:5173](http://localhost:5173) in your browser. -> **Proxy Note**: Vite server and the production Bun server proxy `/api/*` and WebSocket connections to `BACKEND_ORIGIN` (default: `https://hezarfen.dizey.sh`), preserving HttpOnly session cookies across same-origin calls. Override it when needed: `BACKEND_ORIGIN=http://127.0.0.1:7656 bun run dev` against a local backend, or `BACKEND_ORIGIN` in `hezarfen_frontend.env` for the compose stack (see "Run in a container" below). +> **Proxy Note**: Vite server and the production Bun server proxy `/api/*` and WebSocket connections to `BACKEND_ORIGIN` (default: `http://127.0.0.1:7656`, the co-located backend's loopback port), preserving HttpOnly session cookies across same-origin calls. Point it elsewhere with `BACKEND_ORIGIN=… bun run dev`, or with `BACKEND_ORIGIN` in `hezarfen_frontend.env` for the compose stack (see "Run in a container" below). --- @@ -65,7 +65,7 @@ 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. 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.** 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`. +**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=http://127.0.0.1:7656`, `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/server.ts b/server.ts index 102cfd7..3e3f277 100644 --- a/server.ts +++ b/server.ts @@ -11,8 +11,11 @@ const PORT = Number(Bun.env.PORT ?? 5173); // 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"; +// Where the backend lives. The default is the co-located backend a server +// publishes on its own loopback (the compose stack maps 127.0.0.1:7656), which +// is what the frontend container reaches under host networking. Override with +// BACKEND_ORIGIN for anything else. +const BACKEND_ORIGIN = Bun.env.BACKEND_ORIGIN ?? "http://127.0.0.1:7656"; const BACKEND_HTTP = BACKEND_ORIGIN.replace(/\/+$/, ""); const BACKEND_WS = BACKEND_HTTP.replace(/^http/, "ws"); const BACKEND_HOST = new URL(BACKEND_HTTP).host; diff --git a/vite.config.ts b/vite.config.ts index 92c4a9e..12c3160 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -2,7 +2,7 @@ import path from "node:path"; import { defineConfig } from "vite"; import solid from "vite-plugin-solid"; -const target = process.env.BACKEND_ORIGIN ?? "https://hezarfen.dizey.sh"; +const target = process.env.BACKEND_ORIGIN ?? "http://127.0.0.1:7656"; export default defineConfig({ plugins: [solid()],