#!/bin/bash # Backend Data Entry Service Control Script for ROA2WEB Unified App # Manages the FastAPI Data Entry backend on port 8003 # Script directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT_DIR="$SCRIPT_DIR" # Source helper functions source "$SCRIPT_DIR/scripts/service-helpers.sh" # Service configuration SERVICE_NAME="Data Entry Backend" PORT=8003 LOG_FILE="/tmp/data-entry-backend.log" BACKEND_DIR="$ROOT_DIR/data-entry-app/backend" VENV_DIR="$BACKEND_DIR/venv" ENV_FILE="$BACKEND_DIR/.env" # Function to start backend start_backend() { print_header "Starting $SERVICE_NAME" # Check if port is already in use if ! check_port_available $PORT "$SERVICE_NAME"; then print_warning "$SERVICE_NAME may already be running" print_info "Use './backend-data-entry.sh status' to check or './backend-data-entry.sh stop' to stop it" return 1 fi # Check backend directory if [ ! -d "$BACKEND_DIR" ]; then print_error "Backend directory not found: $BACKEND_DIR" return 1 fi # Check virtual environment if [ ! -d "$VENV_DIR" ]; then print_error "Virtual environment not found: $VENV_DIR" print_info "Please run setup first: cd data-entry-app/backend && python3 -m venv venv && source venv/bin/activate && pip install -r requirements.txt" return 1 fi # Check .env file if [ ! -f "$ENV_FILE" ]; then print_warning ".env file not found: $ENV_FILE" print_info "Database connection may fail" fi # Start backend print_info "Starting FastAPI server..." cd "$BACKEND_DIR" # Activate venv and start uvicorn in background ( source venv/bin/activate nohup uvicorn app.main:app --host 0.0.0.0 --port $PORT > "$LOG_FILE" 2>&1 & echo $! > /tmp/data-entry-backend.pid ) local pid=$(cat /tmp/data-entry-backend.pid 2>/dev/null) print_info "Started with PID: $pid" print_info "Log file: $LOG_FILE" # Wait for port to be ready (Data Entry takes longer due to SQLite migrations) if wait_for_port $PORT "$SERVICE_NAME" 35; then # Check health endpoint print_info "Checking health endpoint..." sleep 2 local health_status=$(curl -s http://localhost:$PORT/health 2>&1 | head -1) if echo "$health_status" | grep -q "ok"; then print_success "$SERVICE_NAME started successfully and is healthy!" else print_success "$SERVICE_NAME started (health check inconclusive)" fi echo "" print_info "🌐 API URLs:" echo " • API Docs: http://localhost:$PORT/docs" echo " • Health: http://localhost:$PORT/health" echo "" print_info "📄 View logs: tail -f $LOG_FILE" return 0 else print_error "Failed to start $SERVICE_NAME (timeout waiting for port $PORT)" print_info "Check logs: tail -20 $LOG_FILE" rm -f /tmp/data-entry-backend.pid return 1 fi } # Function to stop backend stop_backend() { print_header "Stopping $SERVICE_NAME" kill_port $PORT "$SERVICE_NAME" # Clean up PID file if [ -f "/tmp/data-entry-backend.pid" ]; then rm /tmp/data-entry-backend.pid fi return 0 } # Function to show backend status status_backend() { print_header "$SERVICE_NAME Status" if check_service_status $PORT "$SERVICE_NAME"; then echo "" # Check health endpoint print_info "Health check:" local health=$(curl -s http://localhost:$PORT/health 2>&1) if echo "$health" | grep -q "ok"; then print_success "Health endpoint: OK" else print_warning "Health endpoint: Not responding" fi # Show recent logs if [ -f "$LOG_FILE" ]; then echo "" tail_logs "$LOG_FILE" 10 fi return 0 else echo "" print_warning "Service is not running" # Show last logs if available if [ -f "$LOG_FILE" ]; then echo "" print_info "Last logs before shutdown:" tail_logs "$LOG_FILE" 10 fi return 1 fi } # Main script logic case "${1:-}" in start) start_backend ;; stop) stop_backend ;; status) status_backend ;; *) echo "Usage: $0 {start|stop|status}" echo "" echo "Commands:" echo " start - Start the Data Entry backend API server" echo " stop - Stop the Data Entry backend API server" echo " status - Show backend status and health check" echo "" echo "Examples:" echo " ./backend-data-entry.sh start # Start backend" echo " ./backend-data-entry.sh status # Check if running" echo " ./backend-data-entry.sh stop # Stop backend" exit 1 ;; esac