Newer
Older
CNCTools / ReferenceSurfaceGenerator / start_local_dev.sh
#!/bin/bash

# This script automates the setup and execution of the local development environment.
# It starts the backend server in the background and the frontend server in the foreground.

# Exit immediately if a command exits with a non-zero status.
set -e

# Function to clean up background processes on exit
cleanup() {
    echo "Shutting down servers..."
    if [ -f backend/uvicorn.pid ]; then
        kill $(cat backend/uvicorn.pid)
        rm backend/uvicorn.pid
    fi
    if [ -f backend/worker.pid ]; then
        kill $(cat backend/worker.pid)
        rm backend/worker.pid
    fi
    echo "Cleanup complete."
}

# Trap the EXIT signal to run the cleanup function when the script is terminated.
trap cleanup EXIT

# --- Backend Setup ---
echo "[DEV_SCRIPT] Setting up and starting backend server..."
cd backend

# Create a virtual environment if it doesn't exist
if [ ! -d "venv" ]; then
    echo "[DEV_SCRIPT] Creating Python virtual environment..."
    python3 -m venv venv
fi

# Activate the virtual environment and install dependencies
source venv/bin/activate
pip install -r requirements.txt

# Start the FastAPI server in the background
echo "[DEV_SCRIPT] Starting backend server on http://localhost:8000..."
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload &
echo $! > uvicorn.pid

# Start the worker process in the background
echo "[DEV_SCRIPT] Starting worker process..."
python app/worker.py &
echo $! > worker.pid

cd ..

# --- Frontend Setup ---
echo ""
echo "[DEV_SCRIPT] Setting up and starting frontend server..."
cd frontend

# Install npm dependencies
npm install

# Start the React development server in the foreground
echo "[DEV_SCRIPT] Starting frontend server on http://localhost:3000..."
echo "[DEV_SCRIPT] Press Ctrl+C to stop both servers."
npm start