|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +echo "🚀 Starting Memcode Docker container..." |
| 5 | + |
| 6 | +# Create empty env.js file (app will use environment variables) |
| 7 | +echo "// Environment variables are set via Docker\nexport default {};" > /app/env.js |
| 8 | + |
| 9 | +# Function to wait for PostgreSQL to be ready |
| 10 | +wait_for_postgres() { |
| 11 | + echo "⏳ Waiting for PostgreSQL to be ready..." |
| 12 | + until pg_isready -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER"; do |
| 13 | + echo "PostgreSQL is unavailable - sleeping" |
| 14 | + sleep 2 |
| 15 | + done |
| 16 | + echo "✅ PostgreSQL is ready!" |
| 17 | +} |
| 18 | + |
| 19 | +# Function to check if database exists and has tables |
| 20 | +check_database_initialized() { |
| 21 | + TABLES_COUNT=$(psql "postgresql://$DB_USER:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_NAME" -t -c "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public';" 2>/dev/null || echo "0") |
| 22 | + if [ "$TABLES_COUNT" -gt 0 ]; then |
| 23 | + echo "✅ Database already initialized with $TABLES_COUNT tables" |
| 24 | + return 0 |
| 25 | + else |
| 26 | + echo "📋 Database needs initialization" |
| 27 | + return 1 |
| 28 | + fi |
| 29 | +} |
| 30 | + |
| 31 | +# Function to build frontend |
| 32 | +build_frontend() { |
| 33 | + echo "🏗️ Building frontend..." |
| 34 | + cd /app/frontend |
| 35 | + NODE_OPTIONS="--openssl-legacy-provider" ../node_modules/.bin/webpack --config ./webpack/development.config.js |
| 36 | + echo "✅ Frontend built successfully!" |
| 37 | + cd /app |
| 38 | +} |
| 39 | + |
| 40 | +# Wait for database |
| 41 | +wait_for_postgres |
| 42 | + |
| 43 | +# Check if database is initialized, if not the schema will be loaded by PostgreSQL init scripts |
| 44 | +if ! check_database_initialized; then |
| 45 | + echo "🔄 Database will be initialized by PostgreSQL init scripts..." |
| 46 | + # Give PostgreSQL a moment to run the init scripts |
| 47 | + sleep 5 |
| 48 | +fi |
| 49 | + |
| 50 | +# Build frontend if webpacked directory doesn't exist or is empty |
| 51 | +if [ ! -d "/app/backend/webpacked" ] || [ -z "$(ls -A /app/backend/webpacked 2>/dev/null)" ]; then |
| 52 | + build_frontend |
| 53 | +else |
| 54 | + echo "✅ Frontend already built" |
| 55 | +fi |
| 56 | + |
| 57 | +echo "🎯 Starting Memcode application..." |
| 58 | + |
| 59 | +# Start the application |
| 60 | +exec node backend/index.js |
0 commit comments