
Features: - Project structure with FastAPI framework - SQLAlchemy models with SQLite database - Alembic migrations system - CRUD operations for items - API routers with endpoints for items - Health endpoint for monitoring - Error handling and validation - Comprehensive documentation
54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
import uvicorn
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.responses import JSONResponse
|
|
from pydantic import ValidationError
|
|
|
|
from app.api.errors import (
|
|
ItemNotFoundException,
|
|
validation_exception_handler,
|
|
item_not_found_handler,
|
|
)
|
|
from app.api.router import api_router
|
|
from app.core.config import settings
|
|
|
|
app = FastAPI(
|
|
title=settings.PROJECT_NAME,
|
|
description=settings.PROJECT_DESCRIPTION,
|
|
version=settings.VERSION,
|
|
openapi_url=f"{settings.API_V1_STR}/openapi.json",
|
|
docs_url="/docs",
|
|
redoc_url="/redoc",
|
|
)
|
|
|
|
# Exception handlers
|
|
app.add_exception_handler(ValidationError, validation_exception_handler)
|
|
app.add_exception_handler(ItemNotFoundException, item_not_found_handler)
|
|
|
|
|
|
@app.exception_handler(Exception)
|
|
async def generic_exception_handler(request: Request, exc: Exception):
|
|
return JSONResponse(
|
|
status_code=500,
|
|
content={"detail": "Internal server error"},
|
|
)
|
|
|
|
|
|
app.include_router(api_router, prefix=settings.API_V1_STR)
|
|
|
|
|
|
@app.get("/")
|
|
def root():
|
|
"""
|
|
Root endpoint with API information.
|
|
"""
|
|
return {
|
|
"name": settings.PROJECT_NAME,
|
|
"version": settings.VERSION,
|
|
"docs": "/docs",
|
|
"redoc": "/redoc",
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|