#!/bin/bash set -e echo "=== Starting Stillwave Container on Hugging Face Spaces ===" # Check for Hugging Face Persistent Storage at /data if [ -d "/data" ] && [ -w "/data" ]; then export DATA_DIR="/data" echo "Persistent storage detected at /data" else export DATA_DIR="/app/backend/data" echo "Using container storage at /app/backend/data" fi mkdir -p "$DATA_DIR" export PORT=5000 export HOST="127.0.0.1" export NODE_ENV=production export API_PROXY_TARGET="http://127.0.0.1:5000" echo "Starting NestJS backend on http://127.0.0.1:5000..." cd /app/backend node dist/main.js & BACKEND_PID=$! echo "Waiting for backend API to be ready..." READY=0 for i in $(seq 1 30); do if curl -s http://127.0.0.1:5000/api/v1/health | grep -q '"status":"ok"'; then READY=1 echo "Backend is ready! (health check passed)" break fi sleep 1 done if [ $READY -eq 0 ]; then echo "Backend health check timed out. Proceeding with frontend launch..." fi echo "Starting Next.js frontend on http://0.0.0.0:7860..." cd /app/frontend cleanup() { echo "Shutting down Stillwave..." kill -TERM "$BACKEND_PID" 2>/dev/null || true kill -TERM "$FRONTEND_PID" 2>/dev/null || true wait "$BACKEND_PID" 2>/dev/null || true wait "$FRONTEND_PID" 2>/dev/null || true exit 0 } trap cleanup SIGTERM SIGINT npx next start -p 7860 -H 0.0.0.0 & FRONTEND_PID=$! # Wait for either process to terminate wait -n "$BACKEND_PID" "$FRONTEND_PID"