todoapp-ns86xt/main.py
Automated Action 01586fdd64 Set up basic FastAPI project structure for todo app
- Created main.py with FastAPI app, CORS configuration, and basic endpoints
- Added requirements.txt with necessary dependencies
- Set up app directory structure with proper organization
- Implemented base URL endpoint with app info and documentation links
- Added health check endpoint that reports application and database status
- Configured CORS to allow all origins
- Set up Alembic for database migrations with proper SQLite configuration
- Updated README with comprehensive project documentation
2025-06-20 23:19:40 +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 routes
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"
}