#!/bin/bash # Data Entry App Starter Script # Starts backend and frontend services for the data entry application set -e # Exit on any error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Function to print colored messages print_message() { echo -e "${BLUE}[DATA-ENTRY]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Function to check if port is in use check_port() { local port=$1 if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1; then return 0 else return 1 fi } # Function to cleanup processes on exit cleanup() { print_message "Stopping services..." # Kill background processes if [[ -n $BACKEND_PID ]]; then kill $BACKEND_PID 2>/dev/null || true fi if [[ -n $FRONTEND_PID ]]; then kill $FRONTEND_PID 2>/dev/null || true fi print_success "All services stopped." exit 0 } # Function to stop all services stop_services() { print_message "Stopping all Data Entry services..." # Stop backend on port 8003 print_message "Checking for backend processes on port 8003..." if check_port 8003; then BACKEND_PIDS=$(lsof -ti:8003) if [[ -n $BACKEND_PIDS ]]; then echo $BACKEND_PIDS | xargs kill -TERM 2>/dev/null || true sleep 2 echo $BACKEND_PIDS | xargs kill -KILL 2>/dev/null || true print_success "Backend processes stopped" fi else print_message "No backend processes found on port 8003" fi # Stop frontend on port 3010 print_message "Checking for frontend processes on port 3010..." if check_port 3010; then FRONTEND_PIDS=$(lsof -ti:3010) if [[ -n $FRONTEND_PIDS ]]; then echo $FRONTEND_PIDS | xargs kill -TERM 2>/dev/null || true sleep 2 echo $FRONTEND_PIDS | xargs kill -KILL 2>/dev/null || true print_success "Frontend processes stopped" fi else print_message "No frontend processes found on port 3010" fi # Kill any remaining processes related to data-entry pkill -f "uvicorn.*data-entry" 2>/dev/null || true pkill -f "node.*data-entry" 2>/dev/null || true pkill -f "vite.*3010" 2>/dev/null || true print_success "✅ All Data Entry services have been stopped!" exit 0 } # Function to stop individual service stop_service() { local service=$1 case $service in backend) print_message "Stopping backend..." if check_port 8003; then BACKEND_PIDS=$(lsof -ti:8003) if [[ -n $BACKEND_PIDS ]]; then echo $BACKEND_PIDS | xargs kill -TERM 2>/dev/null || true sleep 2 echo $BACKEND_PIDS | xargs kill -KILL 2>/dev/null || true print_success "Backend stopped" else print_message "Backend not running" fi else print_message "Backend not running on port 8003" fi ;; frontend) print_message "Stopping frontend..." if check_port 3010; then FRONTEND_PIDS=$(lsof -ti:3010) if [[ -n $FRONTEND_PIDS ]]; then echo $FRONTEND_PIDS | xargs kill -TERM 2>/dev/null || true sleep 2 echo $FRONTEND_PIDS | xargs kill -KILL 2>/dev/null || true print_success "Frontend stopped" fi else print_message "Frontend not running on port 3010" fi ;; *) print_error "Unknown service: $service" print_message "Valid services: backend, frontend" exit 1 ;; esac } # Function to start individual service start_service() { local service=$1 case $service in backend) print_message "Starting backend..." if check_port 8003; then print_warning "Port 8003 already in use. Backend might be running." return 1 fi cd data-entry-app/backend/ # Check if virtual environment exists if [ ! -d "venv" ]; then print_message "Creating Python virtual environment..." python3 -m venv venv fi # Activate virtual environment source venv/bin/activate # Check if dependencies are installed if ! python -c "import fastapi, uvicorn, sqlmodel" 2>/dev/null; then print_message "Installing backend dependencies..." pip install --upgrade pip > /dev/null 2>&1 pip install -r requirements.txt fi # Run Alembic migrations print_message "Running database migrations..." alembic upgrade head 2>/dev/null || print_warning "Migration may have already been applied" print_message "Starting uvicorn server..." nohup uvicorn app.main:app --host 0.0.0.0 --port 8003 --reload > /tmp/data_entry_backend.log 2>&1 & sleep 2 for i in {1..10}; do if check_port 8003; then print_success "Backend started on http://localhost:8003" cd - > /dev/null return 0 fi sleep 1 done print_error "Backend failed to start" cat /tmp/data_entry_backend.log cd - > /dev/null exit 1 ;; frontend) print_message "Starting frontend..." if check_port 3010; then print_warning "Port 3010 already in use. Frontend might be running." return 1 fi cd data-entry-app/frontend/ # Check node_modules if [ ! -d "node_modules" ] || [ ! -f "node_modules/.bin/vite" ]; then print_message "Installing frontend dependencies..." npm install fi print_message "Starting Vite development server..." nohup npm run dev -- --port 3010 > /tmp/data_entry_frontend.log 2>&1 & sleep 3 for i in {1..10}; do if check_port 3010; then print_success "Frontend started on http://localhost:3010" cd - > /dev/null return 0 fi sleep 1 done print_error "Frontend failed to start" cat /tmp/data_entry_frontend.log cd - > /dev/null exit 1 ;; *) print_error "Unknown service: $service" print_message "Valid services: backend, frontend" exit 1 ;; esac } # Function to restart individual service restart_service() { local service=$1 print_message "Restarting $service..." stop_service $service sleep 2 start_service $service print_success "$service restarted successfully" } # Function to show service status show_status() { echo -e "${BLUE}Data Entry Services Status${NC}" echo # Check backend if check_port 8003; then echo -e " Backend: ${GREEN}✓ Running${NC} (http://localhost:8003)" else echo -e " Backend: ${RED}✗ Stopped${NC}" fi # Check frontend if check_port 3010; then echo -e " Frontend: ${GREEN}✓ Running${NC} (http://localhost:3010)" else echo -e " Frontend: ${RED}✗ Stopped${NC}" fi echo } # Function to show usage show_usage() { echo -e "${BLUE}Data Entry App Starter Script${NC}" echo echo "Usage:" echo " ./start-data-entry.sh Start all services" echo " ./start-data-entry.sh start Start all services" echo " ./start-data-entry.sh stop Stop all services" echo " ./start-data-entry.sh status Show services status" echo echo " ./start-data-entry.sh restart Restart specific service" echo " ./start-data-entry.sh start Start specific service" echo " ./start-data-entry.sh stop Stop specific service" echo echo "Services:" echo " backend - FastAPI (port 8003)" echo " frontend - Vue.js/Vite (port 3010)" echo echo "Examples:" echo " ./start-data-entry.sh restart backend Restart only backend" echo " ./start-data-entry.sh stop frontend Stop only frontend" echo } # Check command line arguments case $1 in stop) if [[ -n $2 ]]; then stop_service $2 exit 0 else stop_services fi ;; start) if [[ -n $2 ]]; then start_service $2 exit 0 else true # Continue with normal start process fi ;; restart) if [[ -z $2 ]]; then print_error "Please specify which service to restart" echo show_usage exit 1 fi restart_service $2 exit 0 ;; status) show_status exit 0 ;; help|--help|-h) show_usage exit 0 ;; "") # No parameter - start all services true ;; *) print_error "Unknown parameter: $1" echo show_usage exit 1 ;; esac # Set up signal handlers trap cleanup SIGINT SIGTERM print_message "Starting Data Entry Development Environment..." echo # Step 1: Start Frontend FIRST (for fast UI availability) print_message "1. Starting Frontend (Vue.js)..." cd data-entry-app/frontend/ # Check if node_modules exists if [ ! -d "node_modules" ]; then print_message "Installing frontend dependencies..." npm install fi # Check for WSL compatibility if [ -f "node_modules/.bin/vite.cmd" ] && [ ! -f "node_modules/.bin/vite" ]; then print_warning "Windows node_modules detected, reinstalling for WSL..." rm -rf node_modules package-lock.json npm install fi # Start frontend in background print_message "Starting Vite development server..." npm run dev -- --port 3010 > /tmp/data_entry_frontend.log 2>&1 & FRONTEND_PID=$! # Wait for frontend to start sleep 3 for i in {1..10}; do if check_port 3010; then print_success "Frontend started on http://localhost:3010" break fi if [ $i -eq 10 ]; then print_error "Frontend failed to start after 10 attempts" print_message "Check log at /tmp/data_entry_frontend.log" cat /tmp/data_entry_frontend.log cleanup fi sleep 1 done # Step 2: Start Backend (with OCR loading in background) print_message "2. Starting Backend (FastAPI + OCR)..." # Check if backend port is already in use if check_port 8003; then print_warning "Port 8003 is already in use. Backend might already be running." read -p "Continue anyway? (y/n): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then cleanup fi fi cd ../backend/ # Check if .env file exists if [ ! -f ".env" ]; then if [ -f ".env.example" ]; then print_message "Creating .env from .env.example..." cp .env.example .env else print_error ".env file not found!" exit 1 fi fi # Check if virtual environment exists if [ ! -d "venv" ]; then print_message "Creating Python virtual environment..." python3 -m venv venv fi # Activate virtual environment source venv/bin/activate # Check if dependencies are installed if ! python -c "import fastapi, uvicorn, sqlmodel" 2>/dev/null; then print_message "Installing backend dependencies..." pip install --upgrade pip > /dev/null 2>&1 pip install -r requirements.txt fi # Ensure data directories exist mkdir -p data/uploads # Run Alembic migrations print_message "Running database migrations..." alembic upgrade head 2>/dev/null || print_warning "Migration may have already been applied or first run" # Start backend in background (OCR loads asynchronously) print_message "Starting uvicorn server (OCR loads in background)..." uvicorn app.main:app --host 0.0.0.0 --port 8003 --reload > /tmp/data_entry_backend.log 2>&1 & BACKEND_PID=$! # Wait for backend to start (uvicorn --reload takes longer to bind) sleep 3 for i in {1..20}; do if check_port 8003; then print_success "Backend started on http://localhost:8003" print_message "Note: OCR engine loading in background (first OCR request may be slow)" break fi if [ $i -eq 20 ]; then print_error "Backend failed to start after 20 attempts" cat /tmp/data_entry_backend.log cleanup fi sleep 1 done # Summary echo print_success "🚀 Data Entry Development Environment is now running!" echo echo -e "${BLUE}Services:${NC}" echo " • Backend: http://localhost:8003" echo " • Frontend: http://localhost:3010" echo " • API Docs: http://localhost:8003/docs" echo echo -e "${BLUE}Quick Links:${NC}" echo " • Lista Bonuri: http://localhost:3010/" echo " • Bon Nou: http://localhost:3010/create" echo " • Aprobare: http://localhost:3010/approval" echo echo -e "${YELLOW}Press Ctrl+C to stop all services${NC}" echo # Keep script running and wait for user interrupt wait