todoapp-ns86xt/main.py
Automated Action 16000f8745 Set up database layer for todo app
- Created app/db/base.py with SQLAlchemy Base to avoid circular imports
- Created app/db/session.py with SQLite database connection using /app/storage/db path
- Created app/models/todo.py with Todo model including all required fields
- Created app/schemas/todo.py with Pydantic schemas for request/response
- Added requirements.txt with FastAPI, SQLAlchemy, and other dependencies
- Created proper package structure with __init__.py files
2025-06-20 23:17:54 +00:00

62 lines
1.6 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
import os
# 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=["*"],
)
@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"
}