generated from nholtware/service-template-react
34 lines
1.1 KiB
Bash
Executable file
34 lines
1.1 KiB
Bash
Executable file
#!/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"
|
|
|
|
# nginx variables for the JSON access log (nginx/default.conf). Restricted
|
|
# charset: these land inside nginx string literals.
|
|
safe() { printf '%s' "$1" | tr -cd 'A-Za-z0-9._-'; }
|
|
cat > /etc/nginx/conf.d/service-vars.conf <<NGINX
|
|
map \$host \$app_name { default "$(safe "$APP_NAME")"; }
|
|
map \$host \$git_sha { default "$(safe "${GIT_SHA:-dev}")"; }
|
|
NGINX
|
|
echo "10-config.sh: wrote $OUT and service-vars.conf"
|