
Create a simple Todo API with FastAPI and SQLite with CRUD functionality, health check, error handling, and API documentation.
47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
import uvicorn
|
|
from fastapi import FastAPI, status
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import RedirectResponse
|
|
|
|
from app.api.v1.health import health_check
|
|
from app.api.v1.router import api_router
|
|
from app.core.config import settings
|
|
from app.core.exceptions import add_exception_handlers
|
|
|
|
app = FastAPI(
|
|
title=settings.PROJECT_NAME,
|
|
openapi_url="/openapi.json",
|
|
description="A simple Todo API built with FastAPI and SQLite",
|
|
version="0.1.0",
|
|
)
|
|
|
|
# Set up CORS middleware
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Add exception handlers
|
|
add_exception_handlers(app)
|
|
|
|
# Root endpoint - redirect to docs
|
|
@app.get("/", include_in_schema=False)
|
|
def root():
|
|
return RedirectResponse(url="/docs")
|
|
|
|
# Root health endpoint for easy access
|
|
app.get(
|
|
"/health",
|
|
summary="Check health status",
|
|
status_code=status.HTTP_200_OK,
|
|
tags=["health"],
|
|
)(health_check)
|
|
|
|
# Include API router
|
|
app.include_router(api_router, prefix=settings.API_V1_STR)
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) |