PYTHON
main.py🐍python
"""
Task Management API - Main Application
A complete FastAPI application demonstrating:
- REST API design
- SQLAlchemy ORM
- JWT authentication
- Pydantic validation
- Dependency injection
"""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
from .database import engine, Base
from .routers import users, tasks
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan - create tables on startup."""
Base.metadata.create_all(bind=engine)
yield
# Cleanup on shutdown if needed
app = FastAPI(
title="Task Management API",
description="A complete REST API for managing tasks with authentication",
version="1.0.0",
lifespan=lifespan,
)
# CORS middleware for frontend integration
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Configure for production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include routers
app.include_router(users.router, prefix="/api/users", tags=["users"])
app.include_router(tasks.router, prefix="/api/tasks", tags=["tasks"])
@app.get("/")
async def root():
"""API root endpoint."""
return {
"message": "Task Management API",
"docs": "/docs",
"version": "1.0.0",
}
@app.get("/health")
async def health_check():
"""Health check endpoint for monitoring."""
return {"status": "healthy"}
# =============================================================================
# Run with: uvicorn app.main:app --reload
# API docs at: http://localhost:8000/docs
# =============================================================================