#!/bin/sh set -e # ============================================================================= # Docker Entrypoint Script # ============================================================================= # Handles runtime setup before starting the application: # - Waits for database and cache dependencies # - Decodes SF_PRIVATE_KEY_BASE64 to file if provided # - Runs Prisma migrations if RUN_MIGRATIONS=true # ============================================================================= echo "🚀 Starting Customer Portal Backend..." # Handle Salesforce private key from base64 environment variable if [ -n "$SF_PRIVATE_KEY_BASE64" ]; then echo "📝 Decoding Salesforce private key..." mkdir -p /app/secrets echo "$SF_PRIVATE_KEY_BASE64" | base64 -d > /app/secrets/sf-private.key chmod 600 /app/secrets/sf-private.key export SF_PRIVATE_KEY_PATH=/app/secrets/sf-private.key echo "✅ Salesforce private key configured" fi # Wait for database if DATABASE_URL is set # Extract host:port from postgresql://user:pass@host:port/db if [ -n "$DATABASE_URL" ]; then DB_HOST=$(echo "$DATABASE_URL" | sed -E 's|.*@([^:/]+):([0-9]+)/.*|\1|') DB_PORT=$(echo "$DATABASE_URL" | sed -E 's|.*@([^:/]+):([0-9]+)/.*|\2|') if [ -n "$DB_HOST" ] && [ -n "$DB_PORT" ]; then echo "⏳ Waiting for database ($DB_HOST:$DB_PORT)..." until nc -z "$DB_HOST" "$DB_PORT" 2>/dev/null; do sleep 2 done echo "✅ Database is ready" fi fi # Wait for Redis if REDIS_URL is set # Extract host:port from redis://host:port/db if [ -n "$REDIS_URL" ]; then REDIS_HOST=$(echo "$REDIS_URL" | sed -E 's|redis://([^:/]+):([0-9]+).*|\1|') REDIS_PORT=$(echo "$REDIS_URL" | sed -E 's|redis://([^:/]+):([0-9]+).*|\2|') if [ -n "$REDIS_HOST" ] && [ -n "$REDIS_PORT" ]; then echo "⏳ Waiting for cache ($REDIS_HOST:$REDIS_PORT)..." until nc -z "$REDIS_HOST" "$REDIS_PORT" 2>/dev/null; do sleep 2 done echo "✅ Cache is ready" fi fi # Run database migrations if enabled if [ "$RUN_MIGRATIONS" = "true" ] && [ -n "$DATABASE_URL" ]; then echo "🗄️ Running database migrations..." npx prisma@6.14.0 migrate deploy --schema=/app/apps/bff/prisma/schema.prisma echo "✅ Migrations complete" fi echo "🌐 Starting server on port ${PORT:-4000}..." # Execute the main command (node dist/main.js) exec "$@"