""" MarketScanner - Fear-to-Fortune Trading Intelligence Main FastAPI Application """ from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse import structlog from app.core.config import settings from app.core.database import init_db from app.api import router as api_router # Configure structured logging structlog.configure( processors=[ structlog.stdlib.filter_by_level, structlog.processors.TimeStamper(fmt="iso"), structlog.processors.JSONRenderer() ], wrapper_class=structlog.stdlib.BoundLogger, context_class=dict, logger_factory=structlog.stdlib.LoggerFactory(), ) logger = structlog.get_logger() @asynccontextmanager async def lifespan(app: FastAPI): """Application lifespan events.""" # Startup logger.info("Starting MarketScanner API", version=settings.VERSION) await init_db() logger.info("Database initialized") yield # Shutdown logger.info("Shutting down MarketScanner API") # Create FastAPI application app = FastAPI( title="MarketScanner API", description=""" 🚀 **MarketScanner** - Fear-to-Fortune Trading Intelligence A system that identifies buying opportunities by analyzing how stocks historically respond to panic-inducing news. ## Features * **News Monitoring** - Real-time scanning of financial news * **Sentiment Analysis** - NLP-powered sentiment scoring * **Panic Detection** - Identify market fear events * **Pattern Matching** - Historical recovery patterns * **Buy Signals** - Confidence-scored opportunities *"Buy when there's blood in the streets."* — Baron Rothschild """, version=settings.VERSION, docs_url="/docs", redoc_url="/redoc", lifespan=lifespan, ) # CORS middleware app.add_middleware( CORSMiddleware, allow_origins=settings.CORS_ORIGINS, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Include API routes app.include_router(api_router, prefix="/api/v1") @app.get("/", tags=["Root"]) async def root(): """Root endpoint with API info.""" return { "name": "MarketScanner API", "version": settings.VERSION, "description": "Fear-to-Fortune Trading Intelligence", "docs": "/docs", "health": "/health", } @app.get("/health", tags=["Health"]) async def health_check(): """Health check endpoint for Docker/Kubernetes.""" return JSONResponse( status_code=200, content={ "status": "healthy", "version": settings.VERSION, } ) if __name__ == "__main__": import uvicorn uvicorn.run( "app.main:app", host=settings.BACKEND_HOST, port=settings.BACKEND_PORT, reload=settings.DEBUG, )