todoapp-ns86xt/main.py
Automated Action 68392c171a Add complete FastAPI todo app with CRUD operations
- Implement Todo model with SQLAlchemy
- Create Pydantic schemas for request/response validation
- Add complete CRUD API endpoints (/api/v1/todos)
- Include proper error handling and HTTP status codes
- Set up proper project structure with organized modules
- All code linted and formatted with ruff
2025-06-20 23:26:54 +00:00

65 lines
1.7 KiB
Python

from fastapi import FastAPI, Depends
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.orm import Session
from app.db.session import get_db, engine
from app.db.base import Base
from app.api.api import api_router
# Create database tables
Base.metadata.create_all(bind=engine)
# Initialize FastAPI app
app = FastAPI(
title="Todo App API",
description="A simple Todo application API built with FastAPI",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc",
openapi_url="/openapi.json"
)
# CORS configuration - allow all origins
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include API router
app.include_router(api_router, prefix="/api/v1")
@app.get("/")
async def root():
"""Base URL endpoint with app information and links"""
return {
"title": "Todo App API",
"description": "A simple Todo application API built with FastAPI",
"version": "1.0.0",
"documentation": {
"swagger_ui": "/docs",
"redoc": "/redoc",
"openapi_json": "/openapi.json"
},
"health_check": "/health"
}
@app.get("/health")
async def health_check(db: Session = Depends(get_db)):
"""Health check endpoint that reports application health"""
try:
# Test database connection
db.execute("SELECT 1")
db_status = "healthy"
except Exception as e:
db_status = f"unhealthy: {str(e)}"
return {
"status": "healthy" if db_status == "healthy" else "unhealthy",
"database": db_status,
"service": "Todo App API",
"version": "1.0.0"
}