Skip to main content

fastapi-production-patterns

Build production-ready FastAPI applications with async patterns, Pydantic validation, dependency injection, middleware, and database integration. Use for FastAPI endpoints, async workflows, or API design. Use when this capability is needed.

설치로 이동

소스 정보

저장소
tomevault-io/skills-registry
최근 소스 활동
2026년 4월 28일 22:53
감지된 SKILL.md 언어
영어
스타
1
포크
0

설치 방법

기본적으로 소스를 먼저 확인하는 Prompt가 선택됩니다. 직접 명령으로 전환하거나 로컬 사본을 다운로드할 수도 있습니다.

소스 파일 검토

설치 여부를 결정하기 전에 SKILL.md와 SkillsMP에 표시된 보조 파일을 읽어 보세요.

파일 탐색기
2 개 파일

SKILL.md 표시 중

SKILL.md
소스 지침 · 읽기 전용 미리보기
name
fastapi-production-patterns
description
Build production-ready FastAPI applications with async patterns, Pydantic validation, dependency injection, middleware, and database integration. Use for FastAPI endpoints, async workflows, or API design. Use when this capability is needed.
metadata
{"author":"ak-eyther"}
# FastAPI Production Patterns ## App Structure with Lifespan ```python from fastapi import FastAPI from contextlib import asynccontextmanager @asynccontextmanager async def lifespan(app: FastAPI): # Startup await database.connect() yield # Shutdown await database.disconnect() app = FastAPI(lifespan=lifespan) ``` ## Async Endpoints ```python from fastapi import FastAPI, Depends from sqlalchemy.ext.asyncio import AsyncSession @app.get("/users/{user_id}") async def get_user( user_id: int, db: AsyncSession = Depends(get_db) ): user = await db.get(User, user_id) if not user: raise HTTPException(status_code=404, detail="User not found") return user ``` ## Pydantic Models with Validation ```python from pydantic import BaseModel, validator, Field class UserCreate(BaseModel): email: str = Field(..., example="user@example.com") age: int = Field(..., ge=0, le=150) @validator('email') def validate_email(cls, v): if '@' not in v or '.' not in v.split('@')[1]: raise ValueError('Invalid email format') return v.lower() class Config: orm_mode = True ``` ## Dependency Injection ```python from sqlalchemy.ext.asyncio import AsyncSession async def get_db() -> AsyncSession: async with AsyncSessionLocal() as session: yield session async def get_current_user( token: str = Depends(oauth2_scheme), db: AsyncSession = Depends(get_db) ): user = await verify_token(token, db) return user @app.get("/me") async def read_users_me( current_user: User = Depends(get_current_user) ): return current_user ``` ## CORS Middleware ```python from fastapi.middleware.cors import CORSMiddleware app.add_middleware( CORSMiddleware, allow_origins=["https://yourdomain.com"], allow_credentials=True, allow_methods=["GET", "POST", "PUT", "DELETE"], allow_headers=["*"], ) ``` ## Error Handling ```python from fastapi import Request from fastapi.responses import JSONResponse @app.exception_handler(ValueError) async def value_error_handler(request: Request, exc: ValueError): return JSONResponse( status_code=400, content={"detail": str(exc)} ) class CustomException(Exception): def __init__(self, name: str): self.name = name @app.exception_handler(CustomException) async def custom_exception_handler(request: Request, exc: CustomException): return JSONResponse( status_code=418, content={"message": f"Oops! {exc.name}"} ) ``` ## Background Tasks ```python from fastapi import BackgroundTasks def send_email(email: str, message: str): # Send email logic pass @app.post("/send-notification/") async def send_notification( email: str, background_tasks: BackgroundTasks ): background_tasks.add_task(send_email, email, "notification") return {"message": "Notification sent in background"} ``` ## File Upload ```python from fastapi import File, UploadFile @app.post("/upload/") async def upload_file(file: UploadFile = File(...)): contents = await file.read() # Process file return {"filename": file.filename, "size": len(contents)} ``` ## Scripts - `scripts/skill_info.py`: Print skill name and description. --- > Converted and distributed by [TomeVault](https://tomevault.io/claim/ak-eyther) — claim your Tome and manage your conversions. <!-- tomevault:4.0:skill_md:2026-04-14 -->
GitHub에서 보기