Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import JSONResponse | |
| from fastapi.staticfiles import StaticFiles | |
| import time | |
| import logging | |
| from app.api.routes import router | |
| from app.core.exceptions import PortfolioException | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| app = FastAPI( | |
| title="Investment Portfolio Decision Support System API", | |
| description="API for AI-powered portfolio analysis for Indian markets", | |
| version="1.0.0" | |
| ) | |
| # Add CORS middleware | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # Adjust this in production | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Add request logging middleware | |
| async def log_requests(request: Request, call_next): | |
| start_time = time.time() | |
| response = await call_next(request) | |
| process_time = time.time() - start_time | |
| logger.info(f"Request: {request.method} {request.url} - Completed in {process_time:.4f}s") | |
| return response | |
| # Exception handler | |
| async def portfolio_exception_handler(request: Request, exc: PortfolioException): | |
| return JSONResponse( | |
| status_code=exc.status_code, | |
| content={"message": exc.detail} | |
| ) | |
| # Include API routes | |
| app.include_router(router, prefix="/api/v1") | |
| # Health check endpoint | |
| async def health_check(): | |
| return {"status": "healthy", "message": "API is running"} | |
| # Root endpoint | |
| async def root(): | |
| return { | |
| "message": "Investment Portfolio Decision Support System API", | |
| "version": "1.0.0", | |
| "docs": "/docs" | |
| } | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) |