From e375b0a0204814aadfef1dfc3918d2388cfda977 Mon Sep 17 00:00:00 2001 From: whoswhip Date: Sat, 26 Sep 2026 19:40:36 -0400 Subject: [PATCH] refactor: simplify configuration to a single env file --- .dockerignore | 4 +++- backend.env.example => .env.example | 20 ++++++++--------- .gitignore | 4 +++- README.md | 26 ++++++++++++++++------ SharpbinV3.Client/README.md | 10 +++++---- SharpbinV3.Server/README.md | 6 +++-- SharpbinV3.Server/SharpbinV3.Server.csproj | 1 + docker-compose.yml | 16 ++++++------- frontend.env.example | 10 --------- 9 files changed, 54 insertions(+), 43 deletions(-) rename backend.env.example => .env.example (60%) delete mode 100644 frontend.env.example diff --git a/.dockerignore b/.dockerignore index fe1152b..e1aa717 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,8 @@ **/.classpath **/.dockerignore **/.env +**/backend.env +**/frontend.env **/.git **/.gitignore **/.project @@ -27,4 +29,4 @@ README.md !.git/HEAD !.git/config !.git/packed-refs -!.git/refs/heads/** \ No newline at end of file +!.git/refs/heads/** diff --git a/backend.env.example b/.env.example similarity index 60% rename from backend.env.example rename to .env.example index ba236a0..7d091b3 100644 --- a/backend.env.example +++ b/.env.example @@ -1,13 +1,14 @@ -# Replace every replace_with_ value before running. See README.md for secret generation. -# Use a different random secret for each setting, except the shared frontend/backend key. -# Required Misc. Variables +# Copy to .env in the repository root. Replace every replace_with_ value before running. +# Generate a different random value for each secret. See README.md for instructions. + +# Site Domain=example.com Https=true AllowedCorsOrigins__0=https://example.com -JwtSettings__Secret=replace_with_a_unique_random_jwt_secret_before_running -ConnectionStrings__DefaultConnection=Data Source=/app/data/sharpbin.db +ORIGIN=https://example.com -# Auth Settings +# Authentication +JwtSettings__Secret=replace_with_a_unique_random_jwt_secret_before_running # These Turnstile keys are for testing. Use your own site and secret keys in production. AuthSettings__CF_Turnstile_SecretKey=1x0000000000000000000000000000000AA AuthSettings__CF_Turnstile_SiteKey=3x00000000000000000000FF @@ -15,12 +16,11 @@ AuthSettings__Registration_Enabled=true AuthSettings__Admins_Require_2FA=true AuthSettings__API_Key_HMAC_Secret=replace_with_a_unique_random_api_key_hmac_secret_before_running -# Paste Settings +# Pastes PasteSettings__MaxTitleLength=500 PasteSettings__MaxPasteSizeInBytes=1048576 PasteSettings__EnablePasteCompression=true PasteSettings__RequiresVerification=true PasteSettings__View_HMAC_Secret=replace_with_a_unique_random_view_hmac_secret_before_running -# Used so that views are only counted from the frontend -# Must be the same on both the frontend and backend environment -PasteSettings__View_Internal_API_Key=replace_with_the_same_random_internal_api_key_in_both_files +# Compose passes this value to both services. No separate frontend key is needed. +PasteSettings__View_Internal_API_Key=replace_with_a_unique_random_internal_api_key_before_running diff --git a/.gitignore b/.gitignore index 2c392c4..e4330fc 100644 --- a/.gitignore +++ b/.gitignore @@ -325,4 +325,6 @@ Data !SharpbinV3.Server/Data .idea -**/.env \ No newline at end of file +**/.env +backend.env +frontend.env diff --git a/README.md b/README.md index 7307c1d..a73776a 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ The easiest way to run SharpbinV3 is using Docker Compose. git clone https://github.com/whoswhip/Sharpbin.git ``` -3. In the repository root, copy `backend.env.example` to `backend.env` and `frontend.env.example` to `frontend.env`. +3. In the repository root, copy `.env.example` to `.env`. 4. Replace the secret placeholders and configure your domain and Turnstile keys. Set `ORIGIN` to the public frontend URL, including the scheme. For local Docker testing, use `ORIGIN=http://localhost:5173`, `Domain=localhost:5173`, `Https=false`, and `AllowedCorsOrigins__0=http://localhost:5173`. 5. Run the following command in the root directory: @@ -36,9 +36,11 @@ docker compose up -d --build ### Configuration -Configure the backend in `backend.env` and the frontend in `frontend.env` when using Docker Compose. The backend also loads `appsettings.json`, with environment variables taking precedence. +Configure Sharpbin in the root `.env` file when using Docker Compose. The backend loads this file as environment variables. Compose passes only the backend URL, public origin, and shared view-recording key to the frontend. -Compose explicitly sets `ConnectionStrings__DefaultConnection`, `DATA_PROTECTION_KEY_PATH`, and the frontend's `VITE_API_URL`. These override values from the environment files; change `docker-compose.yml` to override them. +The backend image includes `appsettings.json` with application defaults. Environment variables have priority, so you do not need to edit or mount that file for normal setup. Additional backend settings, including `EmailSettings__Host` and other SMTP settings, can be added to `.env` using their existing names. + +Compose defaults the database to `/app/data/sharpbin.db`, Data Protection keys to `/app/data/keys`, and `VITE_API_URL` to `http://backend:8080`. To override them, add `ConnectionStrings__DefaultConnection`, `DATA_PROTECTION_KEY_PATH`, or `VITE_API_URL` to `.env`. Keep database and key paths under `/app/data` to use the existing persistent volume. The example secrets are placeholders, not secure defaults. Generate a separate secret for JWT signing, API-key hashing, and view hashing, plus one shared internal API key. This command generates a 64-character random hex value; run it once for each secret: @@ -52,7 +54,17 @@ or use openSSL: openssl rand -hex 32 ``` -Set the same shared value in `PasteSettings__View_Internal_API_Key` in `backend.env` and `VIEW_INTERNAL_API_KEY` in `frontend.env`. Keep generated secrets across restarts and updates. Changing the API-key HMAC secret invalidates existing API keys. +Set the shared key once as `PasteSettings__View_Internal_API_Key`. Compose supplies it to the frontend as `VIEW_INTERNAL_API_KEY`. Keep generated secrets across restarts and updates. Changing the API-key HMAC secret invalidates existing API keys. + +#### Migrating an Existing Installation + +1. Copy the values from `backend.env` into the root `.env` file. If `.env` already exists, merge the settings without replacing existing values blindly. +2. Copy `ORIGIN` from `frontend.env`. If it was unset, use your public frontend URL, including the scheme. +3. Keep `PasteSettings__View_Internal_API_Key` set to the existing shared key. The separate `VIEW_INTERNAL_API_KEY` entry is no longer needed for Compose. +4. Move any custom `appsettings.json` values into `.env` using `__` for nested settings. For array settings, use numbered entries such as `AllowedCorsOrigins__0`. If you need to retain a custom JSON file, add a read-only mount for it in `compose.override.yaml`. +5. Run `docker compose up -d --build`. The old `backend.env` and `frontend.env` files are no longer read by Compose. + +Keep the existing database volume, Data Protection keys, and secret values. Do not regenerate secrets during this migration. > When configuring via `appsettings.json` instead of for example `ConnectionStrings__DefaultConnection` it would be: @@ -80,14 +92,14 @@ Set the same shared value in `PasteSettings__View_Internal_API_Key` in `backend. - `PasteSettings__EnablePasteCompression`:**true**: Enables/Disables server-side paste compression. - `PasteSettings__RequiresVerification`:**true**: Enables/Disables CAPTCHA verification when creating pastes. - `PasteSettings__View_HMAC_Secret`: A required random secret of 32–128 characters used to hash viewer identifiers. -- `PasteSettings__View_Internal_API_Key`: A shared key of 32–128 characters used to authorize view recording. Match the frontend's `VIEW_INTERNAL_API_KEY`. +- `PasteSettings__View_Internal_API_Key`: A shared key of 32–128 characters used to authorize view recording. Compose passes it to both services. #### Frontend (SharpbinV3.Client) -- `VITE_API_URL`: The backend URL reachable from the frontend server. Defaults to `http://localhost:5050`; Compose overrides it with `http://backend:8080`. Browser requests use the frontend's `/api` proxy. +- `VITE_API_URL`: The backend URL reachable from the frontend server. Defaults to `http://localhost:5050` when running directly and `http://backend:8080` in Compose. Browser requests use the frontend's `/api` proxy. - `ORIGIN`: The public frontend URL, including its scheme (e.g., `https://example.com`). Used by the production Node server; see [SvelteKit's Node adapter documentation](https://svelte.dev/docs/kit/adapter-node#ORIGIN-PROTOCOL_HEADER-HOST_HEADER-and-PORT_HEADER). - `VITE_ALLOWED_HOSTS`: A comma-separated list used only by Vite dev/preview (e.g., `localhost,sharpbin.cc`). Export it in the shell before starting Vite; the config reads `process.env` directly. It does not configure production host restrictions. -- `VIEW_INTERNAL_API_KEY`: The production frontend's shared view-recording key. Falls back to `View_HMAC_Internal_API_Key` if unset. The frontend does not read `PasteSettings__View_Internal_API_Key`. +- `VIEW_INTERNAL_API_KEY`: The production frontend's shared view-recording key. Compose sets it from `PasteSettings__View_Internal_API_Key`; do not duplicate it in the root `.env`. When running the frontend directly, set it yourself or use the existing `View_HMAC_Internal_API_Key` fallback. When `NODE_ENV=development`, the frontend uses a fixed development-only internal key matching the backend's checked-in development configuration and ignores both environment key names. See the [client README](SharpbinV3.Client/README.md) for development and standalone production setup. diff --git a/SharpbinV3.Client/README.md b/SharpbinV3.Client/README.md index 314e3f0..89dc7c0 100644 --- a/SharpbinV3.Client/README.md +++ b/SharpbinV3.Client/README.md @@ -38,13 +38,15 @@ The frontend will be available at http://localhost:5173. ### Running in Production -Docker Compose loads `frontend.env` and runs the built Node server on port 5173. Set these runtime variables: +Docker Compose reads the root `.env` file and runs the built Node server on port 5173. Configure these values there: -- `VITE_API_URL`: Backend URL. Compose sets `http://backend:8080` directly, overriding `frontend.env`. +- `VITE_API_URL`: Backend URL. Defaults to `http://backend:8080` in Compose. - `ORIGIN`: Public frontend URL, such as `https://example.com` or `http://localhost:5173` for local Docker testing. -- `VIEW_INTERNAL_API_KEY`: Same generated value as the backend's `PasteSettings__View_Internal_API_Key`. If unset, the frontend reads `View_HMAC_Internal_API_Key`; it does not read the backend setting name. +- `PasteSettings__View_Internal_API_Key`: Shared view-recording key. Compose passes it to the frontend as `VIEW_INTERNAL_API_KEY`, so you only set it once. -For a standalone Node deployment, `.env` is not loaded automatically. Build the frontend, then load your environment file explicitly: +For a standalone Node deployment, set `VITE_API_URL`, `ORIGIN`, and `VIEW_INTERNAL_API_KEY` in the client's own `.env` file. The key must match the backend's `PasteSettings__View_Internal_API_Key`. The frontend still supports `View_HMAC_Internal_API_Key` as a fallback; it does not read the backend setting name directly. + +The standalone server does not load `.env` automatically. Build the frontend, then load your environment file explicitly: ```bash npm run build diff --git a/SharpbinV3.Server/README.md b/SharpbinV3.Server/README.md index 257d5e8..092577e 100644 --- a/SharpbinV3.Server/README.md +++ b/SharpbinV3.Server/README.md @@ -13,10 +13,12 @@ This is the backend API for SharpbinV3, built with ASP.NET Core and Entity Frame The server configuration is managed in `appsettings.json`. For development, ensure you have set the following in `appsettings.Development.json` or as environment variables: -- `ConnectionStrings__DefaultConnection`: SQLite connection string (e.g., `Data Source=sharpbin.db` locally). Compose sets `Data Source=/app/data/sharpbin.db` directly. +- `ConnectionStrings__DefaultConnection`: SQLite connection string (e.g., `Data Source=sharpbin.db` locally). Compose defaults to `Data Source=/app/data/sharpbin.db`. - `JwtSettings__Secret`: A random secret of 32–128 characters used for signing JWT tokens. - `PasteSettings__View_HMAC_Secret`: A required random secret of 32–128 characters used to hash viewer identifiers. -- `PasteSettings__View_Internal_API_Key`: A shared key of 32–128 characters used to authorize view recording. Match the production frontend's `VIEW_INTERNAL_API_KEY`. +- `PasteSettings__View_Internal_API_Key`: A shared key of 32–128 characters used to authorize view recording. Compose passes it to both services. For standalone deployments, match the frontend's `VIEW_INTERNAL_API_KEY`. + +For Docker Compose, configure the backend in the root `.env` file. The image includes `appsettings.json` defaults, and environment variables override them. No host configuration mount is required. The development frontend uses a fixed internal key matching the checked-in `appsettings.Development.json`. Changing only the backend key will break development view recording. Use generated secrets for deployment; see the [root configuration guide](../README.md#configuration). diff --git a/SharpbinV3.Server/SharpbinV3.Server.csproj b/SharpbinV3.Server/SharpbinV3.Server.csproj index 5a86b0a..1902ece 100644 --- a/SharpbinV3.Server/SharpbinV3.Server.csproj +++ b/SharpbinV3.Server/SharpbinV3.Server.csproj @@ -38,6 +38,7 @@ + diff --git a/docker-compose.yml b/docker-compose.yml index 57d878c..c5a9467 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,13 +6,13 @@ services: image: sharpbin-backend container_name: sharpbin-backend env_file: - - "backend.env" + - ".env" environment: - DATA_PROTECTION_KEY_PATH: /app/data/keys - ConnectionStrings__DefaultConnection: "Data Source=/app/data/sharpbin.db" + DATA_PROTECTION_KEY_PATH: ${DATA_PROTECTION_KEY_PATH:-/app/data/keys} + ConnectionStrings__DefaultConnection: ${ConnectionStrings__DefaultConnection:-Data Source=/app/data/sharpbin.db} + PasteSettings__View_Internal_API_Key: ${PasteSettings__View_Internal_API_Key:?Set PasteSettings__View_Internal_API_Key in .env} volumes: - sharpbin-data:/app/data - - ./SharpbinV3.Server/appsettings.json:/app/appsettings.json networks: - sharpbin-net healthcheck: @@ -28,13 +28,13 @@ services: context: ./SharpbinV3.Client dockerfile: Dockerfile args: - GIT_COMMIT_HASH: ${GIT_COMMIT_HASH} + GIT_COMMIT_HASH: ${GIT_COMMIT_HASH:-unknown} image: sharpbin-frontend container_name: sharpbin-frontend - env_file: - - "frontend.env" environment: - VITE_API_URL: http://backend:8080 + VITE_API_URL: ${VITE_API_URL:-http://backend:8080} + ORIGIN: ${ORIGIN:?Set ORIGIN in .env} + VIEW_INTERNAL_API_KEY: ${PasteSettings__View_Internal_API_Key:?Set PasteSettings__View_Internal_API_Key in .env} ports: - "5173:5173" diff --git a/frontend.env.example b/frontend.env.example deleted file mode 100644 index 572b961..0000000 --- a/frontend.env.example +++ /dev/null @@ -1,10 +0,0 @@ -# Docker Compose sets this URL directly; change docker-compose.yml to override it. -VITE_API_URL=http://backend:8080 -# Public frontend URL, including the scheme. Use http://localhost:5173 for local Docker testing. -ORIGIN=https://example.com -# VITE_ALLOWED_HOSTS only affects Vite dev/preview, not the production Node server. -# Used so that views are only counted from the frontend -# Must be the same on both the frontend and backend environment -# Production fallback: View_HMAC_Internal_API_Key (not PasteSettings__View_Internal_API_Key). -# Replace this placeholder with the same generated value as the backend setting. -VIEW_INTERNAL_API_KEY=replace_with_the_same_random_internal_api_key_in_both_files