mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-02 01:27:24 -04:00
Compare commits
3 Commits
v0.3.0-alp
...
v0.3.0-alp
| Author | SHA1 | Date | |
|---|---|---|---|
| 3502a7ffa8 | |||
| 68d9f241e1 | |||
| 4fec5826bb |
34
api/main.py
34
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,38 @@ 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
|
||||
if is_dev_mode():
|
||||
# Initialize dev database (reset unless PRESERVE_DEV_DATA=true)
|
||||
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
|
||||
initialize_database(db_path)
|
||||
|
||||
yield
|
||||
|
||||
# Shutdown (if needed in future)
|
||||
pass
|
||||
|
||||
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 +518,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,9 +36,21 @@ fi
|
||||
|
||||
echo "✅ Environment variables validated"
|
||||
|
||||
# Step 1: Initialize database
|
||||
# Step 1: Initialize database (respecting dev/prod mode)
|
||||
echo "📊 Initializing database..."
|
||||
python -c "from api.database import initialize_database; initialize_database('data/jobs.db')"
|
||||
python -c "
|
||||
from tools.deployment_config import is_dev_mode, get_db_path
|
||||
from api.database import initialize_dev_database, initialize_database
|
||||
|
||||
db_path = 'data/jobs.db'
|
||||
if is_dev_mode():
|
||||
print(' 🔧 DEV mode detected - initializing dev database')
|
||||
dev_db_path = get_db_path(db_path)
|
||||
initialize_dev_database(dev_db_path)
|
||||
else:
|
||||
print(' 🏭 PROD mode - initializing production database')
|
||||
initialize_database(db_path)
|
||||
"
|
||||
echo "✅ Database initialized"
|
||||
|
||||
# Step 2: Merge and validate configuration
|
||||
|
||||
Reference in New Issue
Block a user