#!/bin/bash # Storyteller RPG Startup Script # This script starts both the backend and frontend servers echo "🎭 Starting Storyteller RPG..." echo "" # Colors for output GREEN='\033[0;32m' BLUE='\033[0;34m' YELLOW='\033[0;33m' NC='\033[0m' # No Color # Kill any existing instances echo -e "${YELLOW}Checking for existing instances...${NC}" # Kill any process using port 8000 (backend) BACKEND_PORT_PID=$(lsof -ti:8000) if [ ! -z "$BACKEND_PORT_PID" ]; then echo " Killing existing backend on port 8000 (PID: $BACKEND_PORT_PID)" kill -9 $BACKEND_PORT_PID 2>/dev/null fi # Kill any process using port 3000 (frontend) FRONTEND_PORT_PID=$(lsof -ti:3000) if [ ! -z "$FRONTEND_PORT_PID" ]; then echo " Killing existing frontend on port 3000 (PID: $FRONTEND_PORT_PID)" kill -9 $FRONTEND_PORT_PID 2>/dev/null fi # Kill any existing node/python processes from this project pkill -f "main.py" 2>/dev/null pkill -f "react-scripts start" 2>/dev/null # Wait a moment for ports to be released sleep 1 echo -e "${GREEN}✓ Ready to start fresh instances${NC}" echo "" # Check if virtual environment exists if [ ! -d ".venv" ]; then echo "Creating virtual environment..." python3 -m venv .venv fi # Activate virtual environment echo -e "${BLUE}Activating virtual environment...${NC}" source .venv/bin/activate # Install backend dependencies if needed if ! .venv/bin/python -c "import fastapi" 2>/dev/null; then echo -e "${BLUE}Installing backend dependencies...${NC}" .venv/bin/pip install -r requirements.txt fi # Check if frontend dependencies are installed if [ ! -d "frontend/node_modules" ]; then echo -e "${BLUE}Installing frontend dependencies...${NC}" cd frontend npm install cd .. fi # Function to cleanup on exit cleanup() { echo "" echo "Shutting down servers..." kill $BACKEND_PID $FRONTEND_PID 2>/dev/null exit 0 } trap cleanup SIGINT SIGTERM # Start backend echo -e "${GREEN}Starting Backend (http://localhost:8000)...${NC}" .venv/bin/python main.py & BACKEND_PID=$! # Wait a moment for backend to start sleep 2 # Start frontend echo -e "${GREEN}Starting Frontend (http://localhost:3000)...${NC}" cd frontend npm start & FRONTEND_PID=$! cd .. echo "" echo "✅ Servers are starting!" echo " Backend: http://localhost:8000" echo " Frontend: http://localhost:3000" echo "" echo "Press Ctrl+C to stop both servers" echo "" # Wait for frontend to be ready, then open browser echo "Waiting for frontend to start..." for i in {1..30}; do if curl -s http://localhost:3000 > /dev/null 2>&1; then echo -e "${GREEN}✓ Frontend ready!${NC}" sleep 1 # Open browser based on OS if command -v xdg-open > /dev/null; then echo "Opening browser..." xdg-open http://localhost:3000 > /dev/null 2>&1 & elif command -v open > /dev/null; then echo "Opening browser..." open http://localhost:3000 > /dev/null 2>&1 & elif command -v firefox > /dev/null; then echo "Opening browser..." firefox http://localhost:3000 > /dev/null 2>&1 & elif command -v google-chrome > /dev/null; then echo "Opening browser..." google-chrome http://localhost:3000 > /dev/null 2>&1 & else echo "Please open your browser to: http://localhost:3000" fi break fi sleep 1 done echo "" # Wait for both processes wait $BACKEND_PID $FRONTEND_PID