mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-02 01:27:24 -04:00
Compare commits
4 Commits
v0.3.0-alp
...
v0.3.0-alp
| Author | SHA1 | Date | |
|---|---|---|---|
| bdb3f6a6a2 | |||
| 3502a7ffa8 | |||
| 68d9f241e1 | |||
| 4fec5826bb |
42
api/main.py
42
api/main.py
@@ -17,6 +17,7 @@ from pathlib import Path
|
||||
from fastapi import FastAPI, HTTPException, Query
|
||||
from fastapi.responses import JSONResponse
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from api.job_manager import JobManager
|
||||
from api.simulation_worker import SimulationWorker
|
||||
@@ -127,21 +128,46 @@ def create_app(
|
||||
Returns:
|
||||
Configured FastAPI app
|
||||
"""
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Initialize database on startup, cleanup on shutdown if needed"""
|
||||
from tools.deployment_config import is_dev_mode, get_db_path
|
||||
from api.database import initialize_dev_database, initialize_database
|
||||
|
||||
# Startup - use closure to access db_path from create_app scope
|
||||
logger.info("🚀 FastAPI application starting...")
|
||||
logger.info("📊 Initializing database...")
|
||||
|
||||
if is_dev_mode():
|
||||
# Initialize dev database (reset unless PRESERVE_DEV_DATA=true)
|
||||
logger.info(" 🔧 DEV mode detected - initializing dev database")
|
||||
dev_db_path = get_db_path(db_path)
|
||||
initialize_dev_database(dev_db_path)
|
||||
log_dev_mode_startup_warning()
|
||||
else:
|
||||
# Ensure production database schema exists
|
||||
logger.info(" 🏭 PROD mode - ensuring database schema exists")
|
||||
initialize_database(db_path)
|
||||
|
||||
logger.info("✅ Database initialized")
|
||||
logger.info("🌐 API server ready to accept requests")
|
||||
|
||||
yield
|
||||
|
||||
# Shutdown (if needed in future)
|
||||
logger.info("🛑 FastAPI application shutting down...")
|
||||
|
||||
app = FastAPI(
|
||||
title="AI-Trader Simulation API",
|
||||
description="REST API for triggering and monitoring AI trading simulations",
|
||||
version="1.0.0"
|
||||
version="1.0.0",
|
||||
lifespan=lifespan
|
||||
)
|
||||
|
||||
# Store paths in app state
|
||||
app.state.db_path = db_path
|
||||
app.state.config_path = config_path
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
"""Display DEV mode warning on startup if applicable"""
|
||||
log_dev_mode_startup_warning()
|
||||
|
||||
@app.post("/simulate/trigger", response_model=SimulateTriggerResponse, status_code=200)
|
||||
async def trigger_simulation(request: SimulateTriggerRequest):
|
||||
"""
|
||||
@@ -500,7 +526,7 @@ app = create_app()
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
# Display DEV mode warning if applicable
|
||||
log_dev_mode_startup_warning()
|
||||
# Note: Database initialization happens in startup_event()
|
||||
# DEV mode warning will be displayed there as well
|
||||
|
||||
uvicorn.run(app, host="0.0.0.0", port=8080)
|
||||
|
||||
@@ -36,12 +36,7 @@ fi
|
||||
|
||||
echo "✅ Environment variables validated"
|
||||
|
||||
# Step 1: Initialize database
|
||||
echo "📊 Initializing database..."
|
||||
python -c "from api.database import initialize_database; initialize_database('data/jobs.db')"
|
||||
echo "✅ Database initialized"
|
||||
|
||||
# Step 2: Merge and validate configuration
|
||||
# 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"
|
||||
@@ -50,7 +45,7 @@ python -c "from tools.config_merger import merge_and_validate; merge_and_validat
|
||||
export CONFIG_PATH=/tmp/runtime_config.json
|
||||
echo "✅ Configuration validated and merged"
|
||||
|
||||
# Step 3: Start MCP services in background
|
||||
# Step 2: Start MCP services in background
|
||||
echo "🔧 Starting MCP services..."
|
||||
cd /app
|
||||
python agent_tools/start_mcp_services.py &
|
||||
@@ -59,11 +54,11 @@ MCP_PID=$!
|
||||
# Setup cleanup trap before starting uvicorn
|
||||
trap "echo '🛑 Stopping services...'; kill $MCP_PID 2>/dev/null; exit 0" EXIT SIGTERM SIGINT
|
||||
|
||||
# Step 4: Wait for services to initialize
|
||||
# Step 3: Wait for services to initialize
|
||||
echo "⏳ Waiting for MCP services to start..."
|
||||
sleep 3
|
||||
|
||||
# Step 5: Start FastAPI server with uvicorn (this blocks)
|
||||
# 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..."
|
||||
|
||||
Reference in New Issue
Block a user