#!/bin/bash # Define colors for better readability GREEN='\033[0;32m' RED='\033[0;31m' YELLOW='\033[0;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color echo -e "${BLUE}=== Semblance Synthetic Society Startup Script ===${NC}" # Check if MongoDB is installed if ! command -v mongod &> /dev/null; then echo -e "${RED}Error: MongoDB is not installed or not in PATH.${NC}" echo -e "Please install MongoDB using your package manager:" echo -e " - macOS: brew install mongodb-community" echo -e " - Ubuntu: sudo apt install -y mongodb" echo -e " - Windows: Download from https://www.mongodb.com/try/download/community" exit 1 fi # Check if MongoDB is running if ! pgrep -x "mongod" > /dev/null; then echo -e "${YELLOW}MongoDB is not running. Attempting to start MongoDB...${NC}" if [[ "$OSTYPE" == "darwin"* ]]; then # macOS brew services start mongodb-community || mongod --config /usr/local/etc/mongod.conf --fork else # Linux sudo systemctl start mongod || sudo service mongod start fi # Wait for MongoDB to start sleep 3 # Check again if ! pgrep -x "mongod" > /dev/null; then echo -e "${RED}Failed to start MongoDB. Please start it manually before running this script.${NC}" exit 1 else echo -e "${GREEN}MongoDB started successfully.${NC}" fi else echo -e "${GREEN}MongoDB is already running.${NC}" fi # Ask for MongoDB credentials if needed if [[ -z "${MONGO_USER}" || -z "${MONGO_PASS}" ]]; then echo -e "${YELLOW}Do you need to authenticate with MongoDB? (y/N)${NC}" read -r need_auth if [[ "$need_auth" =~ ^[Yy]$ ]]; then echo -n "MongoDB username: " read mongo_user echo -n "MongoDB password: " read -s mongo_pass echo export MONGO_USER="$mongo_user" export MONGO_PASS="$mongo_pass" echo -e "${GREEN}MongoDB credentials set for this session.${NC}" else echo -e "${GREEN}Assuming no authentication is required for MongoDB.${NC}" fi fi # Check if Python is installed if ! command -v python3 &> /dev/null; then echo -e "${RED}Error: Python 3 is not installed or not in PATH.${NC}" exit 1 else echo -e "${GREEN}Found Python $(python3 --version 2>&1 | cut -d' ' -f2)${NC}" fi # Check if Node.js is installed if ! command -v node &> /dev/null; then echo -e "${RED}Error: Node.js is not installed or not in PATH.${NC}" exit 1 else echo -e "${GREEN}Found Node.js $(node --version)${NC}" fi # Create Python virtual environment if it doesn't exist if [ ! -d "backend/venv" ]; then echo -e "${YELLOW}Creating Python virtual environment...${NC}" cd backend python3 -m venv venv cd .. echo -e "${GREEN}Virtual environment created.${NC}" else echo -e "${GREEN}Using existing Python virtual environment.${NC}" fi # Activate virtual environment and install dependencies echo -e "${YELLOW}Installing Python dependencies...${NC}" cd backend source venv/bin/activate pip install -r requirements.txt # Populate the database with sample data echo -e "${YELLOW}Populating the database with sample data...${NC}" python scripts/populate_db_direct.py # Start the backend server in the background echo -e "${YELLOW}Starting backend server with extended timeout for AI operations...${NC}" # Use FLASK_DEBUG instead of FLASK_ENV which is deprecated export FLASK_DEBUG=1 # Check if gunicorn is installed, use it for better timeout handling if command -v gunicorn &> /dev/null; then echo -e "${GREEN}Using Gunicorn with 5 minute timeout for AI operations${NC}" cd backend gunicorn --config gunicorn_config.py "app:create_app()" --bind 0.0.0.0:5137 & cd .. else # Fallback to regular Flask development server python backend/run.py & fi BACKEND_PID=$! cd .. # Install frontend dependencies echo -e "${YELLOW}Installing frontend dependencies...${NC}" npm install # Start the frontend server echo -e "${YELLOW}Starting frontend development server...${NC}" echo -e "${GREEN}======================================================${NC}" echo -e "${GREEN}The application is starting up!${NC}" echo -e "${GREEN}- Backend is running at http://localhost:5137/api${NC}" echo -e "${GREEN}- Frontend will be available at http://localhost:5173${NC}" echo -e "${GREEN}======================================================${NC}" echo -e "Press Ctrl+C to stop both servers" # Start the frontend server and wait for it to finish npm run dev # When the frontend server stops, kill the backend server echo -e "${YELLOW}Shutting down the backend server...${NC}" kill $BACKEND_PID echo -e "${GREEN}All services stopped. Goodbye!${NC}"