72 lines
1.9 KiB
Python
72 lines
1.9 KiB
Python
"""
|
|
Task Manager API - Main application entry point
|
|
"""
|
|
from app.api.v1.api import api_router
|
|
from app.core.config import settings
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.openapi.utils import get_openapi
|
|
from fastapi.responses import JSONResponse
|
|
|
|
# Set up FastAPI application
|
|
app = FastAPI(
|
|
title="Task Manager API",
|
|
description="API for managing tasks and users",
|
|
version="0.1.0",
|
|
docs_url="/docs",
|
|
redoc_url="/redoc",
|
|
openapi_url="/openapi.json",
|
|
)
|
|
|
|
# Add CORS middleware
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=[origin for origin in settings.BACKEND_CORS_ORIGINS],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Include API router
|
|
app.include_router(api_router, prefix=settings.API_V1_STR)
|
|
|
|
# Add health check endpoint
|
|
@app.get("/health", tags=["Health"])
|
|
async def health_check():
|
|
"""
|
|
Health check endpoint to verify API is running properly
|
|
"""
|
|
return {"status": "healthy"}
|
|
|
|
# Custom OpenAPI schema
|
|
def custom_openapi():
|
|
if app.openapi_schema:
|
|
return app.openapi_schema
|
|
|
|
openapi_schema = get_openapi(
|
|
title=app.title,
|
|
version=app.version,
|
|
description=app.description,
|
|
routes=app.routes,
|
|
)
|
|
|
|
# Custom modifications to OpenAPI schema can be added here
|
|
|
|
app.openapi_schema = openapi_schema
|
|
return app.openapi_schema
|
|
|
|
app.openapi = custom_openapi
|
|
|
|
# Global exception handler
|
|
@app.exception_handler(Exception)
|
|
async def global_exception_handler(request: Request, exc: Exception):
|
|
"""Global exception handler for unhandled exceptions"""
|
|
return JSONResponse(
|
|
status_code=500,
|
|
content={"detail": "Internal server error", "type": "internal_error"}
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
# Run the application with uvicorn when script is executed directly
|
|
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) |