react-spa-template/docker-entrypoint.sh
2025-04-19 19:26:03 +02:00

130 lines
4.4 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
#
# docker-entrypoint.sh
# ────────────────────
# Creates /usr/share/nginx/html/app-config.js from CLIENT_* envvars
# and then starts Nginx.
#
set -e
echo "Starting docker-entrypoint.sh script..."
echo "Current directory: $(pwd)"
echo "Preparing runtime environment..."
TARGET_DIR="/usr/share/nginx/html"
echo "Checking target directory: $TARGET_DIR"
if [ -d "$TARGET_DIR" ]; then
echo "Target directory exists"
ls -la "$TARGET_DIR"
else
echo "ERROR: Target directory does not exist: $TARGET_DIR"
exit 1
fi
echo "Creating runtime environment variables script..."
if [ -d "$TARGET_DIR" ]; then
TEMP_DIR="/tmp/app-config"
mkdir -p "$TEMP_DIR"
rm -rf "$TEMP_DIR"/*
#
# ────────────────────────────────────────────────────────────────
# 1. Robust camelcase conversion ← *changed*
# ────────────────────────────────────────────────────────────────
#
to_camel_case() {
# Converts FOO_BAR_BAZ → fooBarBaz (POSIXsh + awk)
printf '%s\n' "$1" | awk -F'_' '{
for (i = 1; i <= NF; i++) {
if (i == 1) {
# first chunk stays lowercase
printf tolower($i)
} else {
# capitalise 1st, lowercase rest
printf toupper(substr($i,1,1)) tolower(substr($i,2))
}
}
}'
}
echo "Processing CLIENT_ environment variables..."
env | grep '^CLIENT_' | while IFS='=' read -r key value; do
key_without_prefix="${key#CLIENT_}"
camel_key="$(to_camel_case "$key_without_prefix")"
echo "$value" > "$TEMP_DIR/$camel_key"
echo " Processed $key -> $camel_key"
done
#
# ────────────────────────────────────────────────────────────────
# 2. Add defaults only if the key is still missing ← *changed*
# ────────────────────────────────────────────────────────────────
#
[ -f "$TEMP_DIR/apiUrl" ] || echo "https://localhost:7205" >"$TEMP_DIR/apiUrl"
[ -f "$TEMP_DIR/basePath" ] || echo "/" >"$TEMP_DIR/basePath"
[ -f "$TEMP_DIR/appName" ] || echo "App" >"$TEMP_DIR/appName"
[ -f "$TEMP_DIR/debug" ] || echo "false" >"$TEMP_DIR/debug"
echo "Generating app-config.js..."
{
echo "// Runtime environment variables - Generated by docker-entrypoint.sh"
echo "window.appConfig = {"
for file in "$TEMP_DIR"/*; do
key="$(basename "$file")"
value="$(cat "$file")"
if echo "$value" | grep -Eq '^(true|false|null|[0-9]+)$'; then
echo " $key: $value,"
else
# escape double quotes just in case
esc_value=$(printf '%s' "$value" | sed 's/"/\\"/g')
echo " $key: \"$esc_value\","
fi
done
echo "};"
} > "$TARGET_DIR/app-config.js"
rm -rf "$TEMP_DIR"
chmod 644 "$TARGET_DIR/app-config.js"
if [ -f "$TARGET_DIR/index.html" ]; then
if ! grep -q "app-config.js" "$TARGET_DIR/index.html"; then
sed -i 's|<head>|<head>\n <script src="/app-config.js"></script>|' "$TARGET_DIR/index.html"
echo "Added app-config.js script to index.html"
else
echo "app-config.js script already exists in index.html"
fi
else
echo "ERROR: index.html not found in $TARGET_DIR"
fi
echo "Runtime environment variables script created successfully."
else
echo "ERROR: Target directory does not exist: $TARGET_DIR"
fi
echo "Environment variables extracted. Starting Nginx server..."
echo "Checking Nginx configuration..."
if [ -f "/etc/nginx/conf.d/default.conf" ]; then
echo "Nginx configuration found at: /etc/nginx/conf.d/default.conf"
cat /etc/nginx/conf.d/default.conf
else
echo "ERROR: Nginx configuration not found at /etc/nginx/conf.d/default.conf"
ls -la /etc/nginx/conf.d/
fi
if command -v nginx >/dev/null 2>&1; then
echo "Nginx found at: $(which nginx)"
nginx -v 2>&1
echo "Testing Nginx configuration..."
nginx -t
echo "Starting Nginx with: nginx -g 'daemon off;'"
exec nginx -g 'daemon off;'
else
echo "ERROR: Nginx not found or not executable"
exit 1
fi