Two-stage build: node builds the static bundle, nginx serves it. The bundle is built against a placeholder base token that the entrypoint rewrites to $BASE_URL at start, so one image serves from any path without a rebuild. nginx config caches hashed assets forever, never caches index.html, and applies security headers (CSP, no framing, no referrer) suited to an app that handles cloud credentials client-side. .gitattributes pins LF on the container-consumed files, since core.autocrlf would otherwise give the entrypoint a CRLF shebang. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
35 lines
1.2 KiB
Bash
35 lines
1.2 KiB
Bash
#!/bin/sh
|
|
# Applies $BASE_URL to the built bundle and the nginx config at container start.
|
|
#
|
|
# The bundle is built with Vite's base set to the literal token below, so every asset
|
|
# URL in the emitted HTML/JS/CSS carries it. Rewriting that token here is what lets one
|
|
# image serve from any path without a rebuild.
|
|
set -eu
|
|
|
|
TOKEN='/__R2B_BASE__/'
|
|
SRC='/opt/ready2blob/html'
|
|
DEST='/usr/share/nginx/html'
|
|
|
|
# Normalize to a leading and trailing slash: "app" and "/app" both mean "/app/".
|
|
base="${BASE_URL:-/}"
|
|
case "$base" in /*) ;; *) base="/$base" ;; esac
|
|
case "$base" in */) ;; *) base="$base/" ;; esac
|
|
|
|
echo "ready2blob: serving from base $base"
|
|
|
|
# Rebuild the web root from the pristine copy on every start, so both restarts and
|
|
# BASE_URL changes are idempotent.
|
|
rm -rf "${DEST:?}"/*
|
|
mkdir -p "$DEST$base"
|
|
cp -R "$SRC/." "$DEST$base"
|
|
|
|
# '|' as the sed delimiter, since both needle and replacement contain '/'.
|
|
find "$DEST" -type f \( -name '*.html' -o -name '*.js' -o -name '*.css' \) \
|
|
-exec sed -i "s|$TOKEN|$base|g" {} +
|
|
|
|
# Restrict envsubst to BASE_URL so nginx's own $uri / $1 survive untouched.
|
|
export BASE_URL="$base"
|
|
envsubst '${BASE_URL}' \
|
|
< /etc/nginx/ready2blob.conf.template \
|
|
> /etc/nginx/conf.d/default.conf
|