#!/bin/sh # Run by the nginx image's entrypoint before nginx starts. Writes the runtime # config the SPA reads as window.__APP_CONFIG__, so one image serves every # environment. To expose a new env var to the browser, add its name to VARS. # Never put secrets here: everything in config.js is public. set -eu VARS="API_BASE_URL APP_NAME" OUT=/usr/share/nginx/html/config.js : "${API_BASE_URL:=/api}" : "${APP_NAME:=app}" json_escape() { printf '%s' "$1" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' } body="" for name in $VARS; do eval "value=\${$name:-}" [ -n "$body" ] && body="$body," body="$body\"$name\":\"$(json_escape "$value")\"" done printf 'window.__APP_CONFIG__ = {%s};\n' "$body" > "$OUT" echo "10-config.sh: wrote $OUT"