Intel_classification / backend /api /entrypoint.sh
danielle2035's picture
Fix: Collect static files during Docker build to prevent 500 errors
8813834
Raw
History Blame Contribute Delete
2.66 kB
#!/bin/bash
set -e
echo "==================== Intel Image Classifier Startup ===================="
echo "Starting at $(date)"
# Navigate to backend directory
cd /app/backend/api
# Step 1: Run database migrations
echo "[1/5] Running database migrations..."
python manage.py migrate --noinput || {
echo "[WARNING] Migration failed (non-critical in development)"
}
# Step 2: Collect static files (skip if already done in build)
echo "[2/5] Collecting static files from Django apps..."
if [ ! -d "static" ] || [ -z "$(ls -A static 2>/dev/null)" ]; then
python manage.py collectstatic --noinput --clear 2>/dev/null || {
echo "[WARNING] Static files collection had issues (non-critical)"
}
else
echo "[OK] Static files already collected during build"
fi
# Step 3: Copy frontend build to Django static directory (skip if already done in build)
echo "[3/5] Copying React frontend build to Django static files..."
if [ ! -d "static/frontend" ] || [ -z "$(ls -A static/frontend 2>/dev/null)" ]; then
if [ -d "/app/frontend/build" ]; then
mkdir -p static/frontend
cp -r /app/frontend/build/* static/frontend/ 2>/dev/null || true
echo "[OK] Frontend assets copied"
else
echo "[WARNING] Frontend build directory not found"
fi
else
echo "[OK] Frontend assets already copied during build"
fi
# Step 4: Create superuser for admin panel (development only)
echo "[4/5] Setting up admin access..."
python -c "
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')
django.setup()
from django.contrib.auth import get_user_model
User = get_user_model()
if not User.objects.filter(username='admin').exists():
User.objects.create_superuser('admin', 'admin@localhost', 'admin')
print('[OK] Admin user created - Username: admin, Password: admin')
else:
print('[OK] Admin user already exists')
" 2>/dev/null || echo "[INFO] Admin setup skipped"
# Step 5: Start the Django application
echo "[5/5] Starting Django application..."
echo "=================================================="
echo "URL: http://0.0.0.0:7860"
echo "API Docs: http://0.0.0.0:7860/swagger/"
echo "Admin: http://0.0.0.0:7860/admin/"
echo "=================================================="
# Use gunicorn in production, runserver in development
if [ "$DEBUG" = "False" ] || [ "$DEBUG" = "false" ]; then
echo "Running in PRODUCTION mode with Gunicorn"
gunicorn api.wsgi:application --bind 0.0.0.0:7860 --workers 3 --timeout 120 --access-logfile - --error-logfile -
else
echo "Running in DEVELOPMENT mode with Django runserver"
python manage.py runserver 0.0.0.0:7860
fi