File size: 2,865 Bytes
4343907
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
# SAAP Backend Startup Script
# Starts the FastAPI backend server with Uvicorn

set -e  # Exit on error

echo "πŸš€ SAAP Backend Startup"
echo "======================="

# Navigate to script directory
cd "$(dirname "$0")"

# Check for virtual environment and activate
if [ -d ".venv" ]; then
    echo "βœ… Activating virtual environment (.venv)..."
    source .venv/bin/activate
elif [ -d "venv" ]; then
    echo "βœ… Activating virtual environment (venv)..."
    source venv/bin/activate
else
    echo "⚠️  No virtual environment found. Creating .venv..."
    python3 -m venv .venv
    source .venv/bin/activate
    echo "πŸ“¦ Installing dependencies from requirements.txt..."
    pip install --upgrade pip
    pip install -r requirements.txt
fi

# Check if dependencies need updating (greenlet, aiosqlite for async SQLAlchemy)
echo "πŸ” Checking critical dependencies..."
if ! python -c "import greenlet" 2>/dev/null; then
    echo "πŸ“¦ Installing missing greenlet library..."
    pip install greenlet aiosqlite
fi

# Check if uvicorn is installed (verify dependencies)
if ! python -c "import uvicorn" 2>/dev/null; then
    echo "⚠️  Dependencies not found in virtual environment"
    echo "πŸ“¦ Installing dependencies from requirements.txt..."
    pip install --upgrade pip
    pip install -r requirements.txt
fi

# Check for .env file
if [ -f "backend/.env" ]; then
    echo "βœ… Loading environment variables from backend/.env"
    set -a  # Automatically export all variables
    source backend/.env
    set +a  # Disable auto-export
else
    echo "⚠️  No backend/.env file found!"
    if [ -f "backend/.env.example" ]; then
        echo "πŸ“ Creating backend/.env from template..."
        cp backend/.env.example backend/.env
        echo ""
        echo "⚠️  IMPORTANT: Please configure backend/.env with your settings!"
        echo "   Required: DATABASE_URL, OPENROUTER_API_KEY, COLOSSUS_API_KEY"
        echo ""
        read -p "Press Enter to continue with default values (not recommended) or Ctrl+C to exit and configure .env..."
    else
        echo "❌ No .env.example template found. Please create backend/.env manually."
        exit 1
    fi
fi

# Navigate to backend directory
cd backend

# Check if main.py exists
if [ ! -f "main.py" ]; then
    echo "❌ Error: backend/main.py not found!"
    exit 1
fi

echo ""
echo "🌟 Starting SAAP Backend Server..."
echo "   - FastAPI + Uvicorn"
echo "   - Host: 0.0.0.0"
echo "   - Port: 8000"
echo "   - Auto-reload: enabled"
echo ""
echo "πŸ“‘ API will be available at: http://localhost:8000"
echo "πŸ“š API Docs (Swagger): http://localhost:8000/docs"
echo "πŸ“– Alternative Docs (ReDoc): http://localhost:8000/redoc"
echo ""
echo "Press Ctrl+C to stop the server"
echo ""

# Start the backend server
python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000