
- Set up FastAPI application with CORS support - Created SQLite database configuration with absolute paths - Implemented Todo model with SQLAlchemy - Added full CRUD operations for todos - Created API endpoints with proper error handling - Set up Alembic for database migrations - Added health check and base URL endpoints - Updated README with comprehensive documentation - Configured project structure following best practices Features: - Complete Todo CRUD API - SQLite database with proper path configuration - Database migrations with Alembic - API documentation at /docs and /redoc - Health check endpoint at /health - CORS enabled for all origins - Proper error handling and validation
51 lines
1.2 KiB
Python
51 lines
1.2 KiB
Python
from datetime import datetime
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from app.api.todos import router as todos_router
|
|
from app.models.schemas import HealthResponse, BaseResponse
|
|
from app.db.base import Base
|
|
from app.db.session import engine
|
|
|
|
# Create database tables
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
app = FastAPI(
|
|
title="Todo API",
|
|
description="A simple Todo API built with FastAPI and SQLite",
|
|
version="1.0.0",
|
|
docs_url="/docs",
|
|
redoc_url="/redoc",
|
|
openapi_url="/openapi.json"
|
|
)
|
|
|
|
# Configure CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Include routers
|
|
app.include_router(todos_router, prefix="/api/v1", tags=["todos"])
|
|
|
|
|
|
@app.get("/", response_model=BaseResponse)
|
|
def read_root():
|
|
"""Base URL endpoint with project information"""
|
|
return BaseResponse(
|
|
title="Todo API",
|
|
documentation="/docs",
|
|
health_endpoint="/health"
|
|
)
|
|
|
|
|
|
@app.get("/health", response_model=HealthResponse)
|
|
def health_check():
|
|
"""Health check endpoint"""
|
|
return HealthResponse(
|
|
status="healthy",
|
|
timestamp=datetime.utcnow()
|
|
) |