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で見る