mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-01 17:17:24 -04:00
Move database initialization logic from shell script to Python application lifespan, following separation of concerns and improving maintainability. Benefits: - Single source of truth for database initialization (api/main.py lifespan) - Better testability - Python code vs shell scripts - Clearer logging with structured messages - Easier to debug and maintain - Infrastructure (entrypoint.sh) focuses on service orchestration - Application (api/main.py) owns its data layer Changes: - Removed database init from entrypoint.sh - Enhanced lifespan function with detailed logging - Simplified entrypoint script (now 4 steps instead of 5) - All tests pass (28/28 API endpoint tests)
76 lines
2.1 KiB
Bash
Executable File
76 lines
2.1 KiB
Bash
Executable File
#!/bin/bash
|
|
set -e # Exit on any error
|
|
|
|
echo "🚀 Starting AI-Trader-Server API..."
|
|
|
|
# Validate required environment variables
|
|
echo "🔍 Validating environment variables..."
|
|
MISSING_VARS=()
|
|
|
|
if [ -z "$OPENAI_API_KEY" ]; then
|
|
MISSING_VARS+=("OPENAI_API_KEY")
|
|
fi
|
|
|
|
if [ -z "$ALPHAADVANTAGE_API_KEY" ]; then
|
|
MISSING_VARS+=("ALPHAADVANTAGE_API_KEY")
|
|
fi
|
|
|
|
if [ -z "$JINA_API_KEY" ]; then
|
|
MISSING_VARS+=("JINA_API_KEY")
|
|
fi
|
|
|
|
if [ ${#MISSING_VARS[@]} -gt 0 ]; then
|
|
echo ""
|
|
echo "❌ ERROR: Missing required environment variables:"
|
|
for var in "${MISSING_VARS[@]}"; do
|
|
echo " - $var"
|
|
done
|
|
echo ""
|
|
echo "Please set these variables in your .env file:"
|
|
echo " 1. Copy .env.example to .env"
|
|
echo " 2. Edit .env and add your API keys"
|
|
echo " 3. Restart the container"
|
|
echo ""
|
|
exit 1
|
|
fi
|
|
|
|
echo "✅ Environment variables validated"
|
|
|
|
# Step 1: Merge and validate configuration
|
|
echo "🔧 Merging and validating configuration..."
|
|
python -c "from tools.config_merger import merge_and_validate; merge_and_validate()" || {
|
|
echo "❌ Configuration validation failed"
|
|
exit 1
|
|
}
|
|
export CONFIG_PATH=/tmp/runtime_config.json
|
|
echo "✅ Configuration validated and merged"
|
|
|
|
# Step 2: Start MCP services in background
|
|
echo "🔧 Starting MCP services..."
|
|
cd /app
|
|
python agent_tools/start_mcp_services.py &
|
|
MCP_PID=$!
|
|
|
|
# Setup cleanup trap before starting uvicorn
|
|
trap "echo '🛑 Stopping services...'; kill $MCP_PID 2>/dev/null; exit 0" EXIT SIGTERM SIGINT
|
|
|
|
# Step 3: Wait for services to initialize
|
|
echo "⏳ Waiting for MCP services to start..."
|
|
sleep 3
|
|
|
|
# Step 4: Start FastAPI server with uvicorn (this blocks)
|
|
# Note: Container always uses port 8080 internally
|
|
# The API_PORT env var only affects the host port mapping in docker-compose.yml
|
|
echo "🌐 Starting FastAPI server on port 8080..."
|
|
echo "🔍 Checking if FastAPI app can be imported..."
|
|
python -c "from api.main import app; print('✓ App imported successfully')" || {
|
|
echo "❌ Failed to import FastAPI app"
|
|
exit 1
|
|
}
|
|
|
|
exec uvicorn api.main:app \
|
|
--host 0.0.0.0 \
|
|
--port 8080 \
|
|
--log-level info \
|
|
--access-log
|