Files
moyosapp_beta.0.0.3/docker/entrypoint.sh
2026-01-15 17:06:42 +02:00

108 lines
4.1 KiB
Bash
Executable File

#!/bin/sh
# Entrypoint script for Next.js application
# Validates environment variables at runtime (not during build)
set -e
echo "🚀 Starting application... $(date '+%Y-%m-%d %H:%M:%S')"
# Validate critical environment variables at runtime
if [ "$NODE_ENV" = "production" ]; then
echo "🔍 Validating production environment... $(date '+%Y-%m-%d %H:%M:%S')"
# Required vars (expand as needed)
# Check each variable individually for better portability
if [ -z "$DATABASE_URL" ]; then
echo "❌ ERROR: DATABASE_URL is required in production $(date '+%Y-%m-%d %H:%M:%S')"
exit 1
fi
if [ -z "$SESSION_SECRET" ]; then
echo "❌ ERROR: SESSION_SECRET is required in production $(date '+%Y-%m-%d %H:%M:%S')"
exit 1
fi
if [ -z "$ADMIN_PASSWORD" ]; then
echo "❌ ERROR: ADMIN_PASSWORD is required in production $(date '+%Y-%m-%d %H:%M:%S')"
exit 1
fi
if [ -z "$CSRF_SECRET" ]; then
echo "❌ ERROR: CSRF_SECRET is required in production $(date '+%Y-%m-%d %H:%M:%S')"
exit 1
fi
if [ -z "$SUPABASE_SERVICE_ROLE_KEY" ]; then
echo "❌ ERROR: SUPABASE_SERVICE_ROLE_KEY is required in production $(date '+%Y-%m-%d %H:%M:%S')"
exit 1
fi
# Optional: Check URL formats (basic regex)
if ! echo "$DATABASE_URL" | grep -qE '^postgres(ql)?://'; then
echo "❌ ERROR: DATABASE_URL must be a valid PostgreSQL connection string $(date '+%Y-%m-%d %H:%M:%S')"
exit 1
fi
echo "✅ Environment validation passed $(date '+%Y-%m-%d %H:%M:%S')"
fi
# Allow skipping health check database validation for debugging
if [ "$SKIP_HEALTH_DB_CHECK" = "1" ]; then
echo "⚠️ Skipping database check in health endpoint (SKIP_HEALTH_DB_CHECK=1) $(date '+%Y-%m-%d %H:%M:%S')"
fi
# Run database migrations if DATABASE_URL is set and not skipped
if [ -n "$DATABASE_URL" ] && [ "$SKIP_MIGRATIONS" != "1" ]; then
echo "🔄 Testing database connectivity... $(date '+%Y-%m-%d %H:%M:%S')"
# Test basic database connectivity
echo "SELECT 1;" > /tmp/test.sql
if npx prisma db execute --file /tmp/test.sql >/dev/null 2>&1; then
echo "✅ Database connection successful $(date '+%Y-%m-%d %H:%M:%S')"
rm -f /tmp/test.sql
else
echo "❌ ERROR: Cannot connect to database $(date '+%Y-%m-%d %H:%M:%S')"
echo "Database URL: $(echo "$DATABASE_URL" | sed 's|//.*@|//***:***@|')" # Hide credentials in logs
echo "⚠️ Continuing without migrations..."
rm -f /tmp/test.sql
fi
echo "🔄 Running database migrations... $(date '+%Y-%m-%d %H:%M:%S')"
# Run migrations with timeout to prevent hanging
timeout 120 sh -c '
echo "Running: npx prisma migrate deploy"
npx prisma migrate deploy 2>&1
exit_code=$?
echo "Migration command completed with exit code: $exit_code"
exit $exit_code
' || {
migration_status=$?
echo "❌ ERROR: Prisma migrations failed or timed out (status: $migration_status) $(date '+%Y-%m-%d %H:%M:%S')"
if [ $migration_status -eq 124 ]; then
echo "⚠️ Migration command timed out after 120 seconds"
fi
# Continue anyway - don't fail the startup for migration issues in production
echo "⚠️ Continuing startup despite migration issues..."
}
echo "✅ Migrations step complete $(date '+%Y-%m-%d %H:%M:%S')"
elif [ "$SKIP_MIGRATIONS" = "1" ]; then
echo "⏭️ Skipping database migrations (SKIP_MIGRATIONS=1) $(date '+%Y-%m-%d %H:%M:%S')"
fi
# Optional: Run seeding if RUN_SEED=1 (set in env vars)
if [ -n "$RUN_SEED" ]; then
echo "🌱 Running database seeding... $(date '+%Y-%m-%d %H:%M:%S')"
npm run db:seed || { echo "❌ ERROR: Seeding failed $(date '+%Y-%m-%d %H:%M:%S')"; exit 1; }
echo "✅ Seeding complete $(date '+%Y-%m-%d %H:%M:%S')"
fi
# Start the application
echo "🎯 Starting Next.js server... $(date '+%Y-%m-%d %H:%M:%S')"
# Check if server.js exists (Next.js standalone output)
if [ -f "server.js" ]; then
echo "✅ Found server.js, starting with standalone build... $(date '+%Y-%m-%d %H:%M:%S')"
exec node server.js
else
echo "❌ ERROR: server.js not found. This might indicate a build issue. $(date '+%Y-%m-%d %H:%M:%S')"
echo "Contents of /app directory:"
ls -la
exit 1
fi