From 98622c41198bf48a80b04548ed7d9f96d7806d23 Mon Sep 17 00:00:00 2001 From: Danilo Reyes Date: Sun, 28 Dec 2025 21:20:21 -0600 Subject: [PATCH] Add host configuration for backend server and update documentation - Introduced a new `host` option in `flake.nix` to specify the server's bind address, defaulting to `0.0.0.0`. - Updated `run.sh` to use the `HOST` environment variable for server binding. - Modified `config.py` to read the `HOST` from environment variables, defaulting to `0.0.0.0`. - Enhanced `README.md` with instructions for setting up the `HOST` variable and clarified access details for development and production modes. - Adjusted `vite.config.ts` to allow frontend access from other computers by default. --- README.md | 56 ++++++++++++++++++++++++++++++-------- backend/app/core/config.py | 2 +- backend/run.sh | 3 +- flake.nix | 7 +++++ frontend/vite.config.ts | 11 ++++++-- 5 files changed, 63 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index a75ceba..5a3ab14 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ A web application that visualizes the origin countries of your media collection nix develop ``` -2. Set up environment variables (create `.env` in `backend/`): +2. Set up backend environment variables (create `.env` in `backend/`): ```bash POSTGRES_SOCKET_PATH=/run/postgresql POSTGRES_DB=jawz @@ -48,28 +48,51 @@ SONARR_API_KEY=your_sonarr_api_key RADARR_API_KEY=your_radarr_api_key LIDARR_API_KEY=your_lidarr_api_key PORT=8080 +HOST=0.0.0.0 # Use 127.0.0.1 for localhost only ``` -3. Run database migrations: +3. (Optional) Set up frontend environment variables (create `.env.local` in `frontend/`): +```bash +# Only needed if backend port/host differs from defaults +VITE_BACKEND_HOST=127.0.0.1 +VITE_BACKEND_PORT=8080 +VITE_PORT=5173 +``` + +5. Start the backend (in one terminal): ```bash cd backend -alembic upgrade head +# Uses HOST and PORT from .env file (defaults to 0.0.0.0:8080) +python -m uvicorn main:app --reload --host ${HOST:-0.0.0.0} --port ${PORT:-8080} ``` -4. Start the backend (in one terminal): -```bash -cd backend -python -m uvicorn main:app --reload --host 127.0.0.1 --port 8080 -``` - -5. Start the frontend dev server (in another terminal): +6. Start the frontend dev server (in another terminal): ```bash cd frontend npm install +# Optional: Set backend URL if different from default +# export VITE_BACKEND_HOST=127.0.0.1 +# export VITE_BACKEND_PORT=8080 npm run dev ``` -6. Open http://localhost:5173 in your browser +7. Open http://localhost:5173 in your browser (or http://:5173 from another computer) + +### Development vs Production + +**Development Mode:** +- Backend runs on port 8080 (configurable) +- Frontend dev server runs on port 5173 (configurable) +- Frontend proxies API requests to backend +- Both are accessible from other computers (bound to 0.0.0.0) + +**Production Mode (NixOS deployment):** +- Backend runs on configured port (default 8080) +- Frontend is built and served as static files by the backend +- No separate frontend server needed +- Access via backend URL only + +**Note**: The frontend dev server runs on port 5173 by default and proxies API requests to the backend. If you changed the backend port, set `VITE_BACKEND_PORT` environment variable to match. The frontend dev server binds to `0.0.0.0` by default, so it's accessible from other computers. ## Building @@ -96,6 +119,7 @@ In your `configuration.nix` or a separate module: services.moviemap = { enable = true; port = 8080; + host = "0.0.0.0"; # Bind to all interfaces (use "127.0.0.1" for localhost only) postgresSocketPath = "/run/postgresql"; # Secrets can be strings or file paths (for sops-nix integration) sonarrApiKey = "/run/secrets/sonarr-api-key"; # or "your_key_here" @@ -167,7 +191,15 @@ Or simply run migrations once manually, then enable the service. sudo nixos-rebuild switch ``` -The service will be available at `http://127.0.0.1:8080` (configure your reverse proxy to expose it). +The service will be available at `http://0.0.0.0:8080` (or your server's IP address). If you set `host = "127.0.0.1"`, it will only be accessible from localhost (use a reverse proxy in that case). + +**Note**: If you're accessing from another computer, make sure: +1. The service is bound to `0.0.0.0` (default) or your server's IP address +2. Your firewall allows incoming connections on the configured port +3. If using NixOS, you may need to open the port in your firewall configuration: + ```nix + networking.firewall.allowedTCPPorts = [ 8080 ]; + ``` ## API Endpoints diff --git a/backend/app/core/config.py b/backend/app/core/config.py index a9710f0..43a0f1f 100644 --- a/backend/app/core/config.py +++ b/backend/app/core/config.py @@ -9,7 +9,7 @@ class Settings(BaseSettings): # Server port: int = int(os.getenv("PORT", "8080")) - host: str = "127.0.0.1" + host: str = os.getenv("HOST", "0.0.0.0") # Default to all interfaces # Database postgres_socket_path: str = os.getenv("POSTGRES_SOCKET_PATH", "/run/postgresql") diff --git a/backend/run.sh b/backend/run.sh index 73aecf0..13ac134 100755 --- a/backend/run.sh +++ b/backend/run.sh @@ -5,6 +5,7 @@ cd "$(dirname "$0")" # Default values export PORT=${PORT:-8080} +export HOST=${HOST:-0.0.0.0} # Bind to all interfaces by default export POSTGRES_SOCKET_PATH=${POSTGRES_SOCKET_PATH:-/run/postgresql} export POSTGRES_DB=${POSTGRES_DB:-jawz} @@ -31,5 +32,5 @@ if [ -d "alembic/versions" ]; then fi # Start the server -exec uvicorn main:app --host 127.0.0.1 --port "$PORT" +exec uvicorn main:app --host "$HOST" --port "$PORT" diff --git a/flake.nix b/flake.nix index 10b10ad..e3a0980 100644 --- a/flake.nix +++ b/flake.nix @@ -104,6 +104,7 @@ # The run.sh script will read from files at runtime envVars = [ "PORT=${toString cfg.port}" + "HOST=${cfg.host}" "POSTGRES_SOCKET_PATH=${cfg.postgresSocketPath}" ] ++ [ # API keys - if path, pass as-is; if string, pass directly @@ -131,6 +132,12 @@ description = "Port to bind the backend server"; }; + host = mkOption { + type = types.str; + default = "0.0.0.0"; + description = "Host address to bind the server (0.0.0.0 for all interfaces, 127.0.0.1 for localhost only)"; + }; + postgresSocketPath = mkOption { type = types.str; default = "/run/postgresql"; diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 3dfd53f..8f5cd5a 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -1,19 +1,26 @@ import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' +// Get backend URL from environment variables (for dev server proxy) +const backendHost = process.env.VITE_BACKEND_HOST || '127.0.0.1' +const backendPort = process.env.VITE_BACKEND_PORT || '8080' +const backendUrl = `http://${backendHost}:${backendPort}` + export default defineConfig({ plugins: [react()], build: { outDir: 'dist', }, server: { + host: '0.0.0.0', // Allow access from other computers + port: parseInt(process.env.VITE_PORT || '5173'), proxy: { '/api': { - target: 'http://127.0.0.1:8080', + target: backendUrl, changeOrigin: true, }, '/admin': { - target: 'http://127.0.0.1:8080', + target: backendUrl, changeOrigin: true, }, },