From 8d12922f6a4264d9983f92a3689191459bc98d5c Mon Sep 17 00:00:00 2001 From: Matthew Daubney Date: Fri, 25 Sep 2026 20:32:40 +0100 Subject: [PATCH] Serve fediverse paths on the UI's domain (phase 1, step 6) - The UI container forwards /.well-known/{webfinger,nodeinfo,host-meta}, /nodeinfo/, /users/, /inbox and /actor to API_INTERNAL_URL (compose: http://api:8000), passing Host and X-Forwarded-*; unset means no forwarding - The API's name is resolved per request via the container's resolver, so the UI starts before the API and survives API restarts - Compose passes the FEDERATION_* settings to api and worker; .env.example documents them, including FEDERATION_KEY_SECRET - gunicorn: disable the 26.x control socket (the container user has no home directory) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW --- .env.example | 3 +++ deploy/nginx/default.conf.template | 9 ++++++++ docker-compose.yml | 10 +++++++++ docker/ui-config.sh | 35 +++++++++++++++++++++++++++++- gunicorn.conf.py | 2 ++ 5 files changed, 58 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 149253d..0d614b5 100644 --- a/.env.example +++ b/.env.example @@ -24,6 +24,9 @@ GRAFANA_PORT=3000 GRAFANA_ADMIN_PASSWORD=admin # ActivityPub federation (in development; off by default). See src/scrobbler/federation/README.md. +# Use the web UI's host and origin: handles become @user@. FEDERATION_ENABLED=0 FEDERATION_DOMAIN= FEDERATION_BASE_URL= +# 32+ characters; encrypts actors' private keys. Keep it safe and never change it. +FEDERATION_KEY_SECRET= diff --git a/deploy/nginx/default.conf.template b/deploy/nginx/default.conf.template index 9b8ef32..b128334 100644 --- a/deploy/nginx/default.conf.template +++ b/deploy/nginx/default.conf.template @@ -1,4 +1,10 @@ # Rendered by the nginx image's envsubst step (API_BASE_URL comes from the environment). + +# Behind a TLS proxy, keep its scheme; otherwise use ours. +map $http_x_forwarded_proto $scrobbler_forwarded_proto { + default $http_x_forwarded_proto; + "" $scheme; +} server { listen 80; server_name _; @@ -17,6 +23,9 @@ server { add_header Referrer-Policy same-origin always; } + # Fediverse paths forwarded to the API (written at start-up; empty if API_INTERNAL_URL is unset). + include /etc/nginx/scrobbler/federation.conf; + location /fonts/ { add_header Cache-Control "public, max-age=2592000, immutable"; } diff --git a/docker-compose.yml b/docker-compose.yml index c9cfe75..adadcb7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -34,6 +34,10 @@ services: WEB_CONCURRENCY: ${WEB_CONCURRENCY:-2} FORWARDED_ALLOW_IPS: "*" LASTFM_API_KEY: ${LASTFM_API_KEY:-} + FEDERATION_ENABLED: ${FEDERATION_ENABLED:-0} + FEDERATION_DOMAIN: ${FEDERATION_DOMAIN:-} + FEDERATION_BASE_URL: ${FEDERATION_BASE_URL:-} + FEDERATION_KEY_SECRET: ${FEDERATION_KEY_SECRET:-} ports: - "${API_PORT:-5050}:8000" expose: @@ -53,6 +57,10 @@ services: DATABASE_URL: postgresql+psycopg://scrobbler:${POSTGRES_PASSWORD:-scrobbler}@db:5432/scrobbler SECRET_KEY: ${SECRET_KEY:-change-me} LASTFM_API_KEY: ${LASTFM_API_KEY:-} + FEDERATION_ENABLED: ${FEDERATION_ENABLED:-0} + FEDERATION_DOMAIN: ${FEDERATION_DOMAIN:-} + FEDERATION_BASE_URL: ${FEDERATION_BASE_URL:-} + FEDERATION_KEY_SECRET: ${FEDERATION_KEY_SECRET:-} RUN_MIGRATIONS: "0" # the api service applies them expose: - "9101" # metrics: internal network only @@ -69,6 +77,8 @@ services: dockerfile: docker/ui.Dockerfile environment: API_BASE_URL: ${API_BASE_URL:-http://localhost:5050} + # Forward the fediverse paths (/.well-known/webfinger, /users/, /inbox, ...) to the API. + API_INTERNAL_URL: ${API_INTERNAL_URL:-http://api:8000} ports: - "${UI_PORT:-8080}:80" diff --git a/docker/ui-config.sh b/docker/ui-config.sh index 0595b80..53184c8 100755 --- a/docker/ui-config.sh +++ b/docker/ui-config.sh @@ -1,5 +1,6 @@ #!/bin/sh -# Point the UI at the API: rewrite config.js from $API_BASE_URL at container start. +# Container start-up: point the UI at the API, and (when API_INTERNAL_URL is set) +# forward the fediverse paths on this domain to the API. set -e case "$API_BASE_URL" in http://*|https://*) ;; @@ -11,3 +12,35 @@ window.SCROBBLER_CONFIG = { apiBaseUrl: "$url", }; JS + +mkdir -p /etc/nginx/scrobbler +if [ -z "$API_INTERNAL_URL" ]; then + : > /etc/nginx/scrobbler/federation.conf + exit 0 +fi +case "$API_INTERNAL_URL" in + http://*|https://*) ;; + *) echo "API_INTERNAL_URL must look like http://api:8000" >&2; exit 1 ;; +esac +upstream=$(printf '%s' "$API_INTERNAL_URL" | sed 's#/*$##') +case "$upstream" in + *://*/*) echo "API_INTERNAL_URL must not have a path" >&2; exit 1 ;; +esac +# Resolve the API's name per request (not once at start-up), so the UI starts even if +# the API isn't up yet and keeps working when the API container is replaced. +nameserver=$(awk '/^nameserver/ { print $2; exit }' /etc/resolv.conf) +case "$nameserver" in *:*) nameserver="[$nameserver]" ;; esac +cat > /etc/nginx/scrobbler/federation.conf <