from fastapi import FastAPI, Request, status from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse from fastapi.exceptions import RequestValidationError from slowapi import Limiter, _rate_limit_exceeded_handler from slowapi.util import get_remote_address from slowapi.errors import RateLimitExceeded import os from dotenv import load_dotenv import time load_dotenv() # Initialize rate limiter limiter = Limiter(key_func=get_remote_address) app = FastAPI( title="Todo API", description="API for managing user tasks in the Todo application", version="1.0.0" ) # Add rate limiter to app state app.state.limiter = limiter app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) # Add CORS middleware app.add_middleware( CORSMiddleware, allow_origins=os.getenv("ALLOWED_ORIGINS", "*").split(","), allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Import logging utilities # from src.utils.logger import logger, log_error from src.utils.logger import logger, log_error from src.utils.security import generate_secure_headers # Global exception handler @app.exception_handler(Exception) async def global_exception_handler(request: Request, exc: Exception): log_error(exc, f"Unhandled exception in {request.method} {request.url.path}") return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={ "detail": "An internal server error occurred", "type": "internal_error" } ) # Validation error handler @app.exception_handler(RequestValidationError) async def validation_exception_handler(request: Request, exc: RequestValidationError): logger.warning(f"Validation error in {request.method} {request.url.path}: {exc.errors()}") return JSONResponse( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, content={ "detail": exc.errors(), "type": "validation_error" } ) # Security headers middleware @app.middleware("http") async def add_security_headers(request: Request, call_next): response = await call_next(request) # Add security headers secure_headers = generate_secure_headers() for header, value in secure_headers.items(): response.headers[header] = value return response # Request logging middleware @app.middleware("http") async def log_requests(request: Request, call_next): start_time = time.time() # Log incoming request logger.info(f"Incoming request: {request.method} {request.url.path}") response = await call_next(request) # Log response time process_time = time.time() - start_time logger.info(f"Request completed: {request.method} {request.url.path} - Status: {response.status_code} - Time: {process_time:.3f}s") return response # Import and include routers from src.api import auth_router, task_router, chat_router app.include_router(auth_router.router, prefix="/api/v1/auth", tags=["authentication"]) app.include_router(task_router.router, prefix="/api/v1/tasks", tags=["tasks"]) app.include_router(chat_router.router, prefix="/api/v1", tags=["chat"]) @app.get("/") @limiter.limit("100/minute") def read_root(request: Request): logger.info("Health check endpoint accessed") return {"message": "Todo API is running", "status": "healthy", "version": "2.0"} if __name__ == "__main__": import uvicorn uvicorn.run( "src.main:app", host="0.0.0.0", port=int(os.getenv("PORT", 8000)), reload=True )