| import logging |
| from contextlib import asynccontextmanager |
|
|
| from fastapi import FastAPI |
| from fastapi.middleware.cors import CORSMiddleware |
|
|
| from app.api.platform_routes import router as platform_router |
| from app.api.research_routes import router as research_router |
| from app.api.routes import router |
| from app.api.websocket import ws_router |
| from app.core.config import settings |
| from app.core.contracts import CONTRACTS |
| from app.models.database import init_db |
| from app.services.market_data import market_data_service |
|
|
| logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(name)s: %(message)s") |
| logger = logging.getLogger(__name__) |
|
|
|
|
| @asynccontextmanager |
| async def lifespan(app: FastAPI): |
| logger.info("Starting %s v%s", settings.APP_NAME, settings.APP_VERSION) |
| await init_db() |
| await market_data_service.start() |
| logger.info("Market data service started for %d contracts", len(CONTRACTS)) |
| yield |
| await market_data_service.stop() |
| logger.info("Shutdown complete") |
|
|
|
|
| app = FastAPI( |
| title=settings.APP_NAME, |
| version=settings.APP_VERSION, |
| description="Personal Trading System V1 - Futures Trading with Quantitative Strategies", |
| lifespan=lifespan, |
| ) |
|
|
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=["*"], |
| allow_credentials=True, |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
|
|
| app.include_router(router, prefix="/api") |
| app.include_router(platform_router, prefix="/api") |
| app.include_router(research_router, prefix="/api") |
| app.include_router(ws_router) |
|
|
|
|
| @app.get("/") |
| async def root(): |
| return { |
| "name": settings.APP_NAME, |
| "version": settings.APP_VERSION, |
| "description": "Personal Trading System V1 API", |
| "docs": "/docs", |
| } |
|
|