develop-fastapi-app
使用最佳实践构建生产就绪的FastAPI应用程序,包括项目结构、异步模式、Pydantic模型、依赖注入、数据库操作和测试。用于开发FastAPI后端、REST API或Python异步Web服务。
📖 中文导读:使用最佳实践构建生产就绪的FastAPI应用程序,包括项目结构、异步模式、Pydantic模型、依赖注入、数据库操作和测试。用于开发FastAPI后端、REST API或Python异步Web服务。 以下正文为该技能的原始 SKILL.md 内容(英文),上方安装命令可直接使用。
name: develop-fastapi-app description: Build production-ready FastAPI applications with best practices for project structure, async patterns, Pydantic models, dependency injection, database operations, and testing. Use when developing FastAPI backends, REST APIs, or Python async web services. license: Apache-2.0 metadata: author: zhanymkanov, xiaoland version: "1.0"
Developing a FastAPI Application
When to use this skill
Use this skill when:
- Starting a new FastAPI project
- Refactoring existing FastAPI applications
- Implementing REST APIs with Python
- Working with async/await patterns in web services
- Setting up database models and migrations
- Writing tests for FastAPI endpoints
- Configuring dependency injection patterns
- Optimizing API performance and code quality
Project Structure
Organize projects by domain rather than file type for better scalability:
fastapi-project/
├── app/
│ ├── schemas/ # data structure and constraints
│ ├── models/ # schema with behavior
│ ├── managers/ # domain orchestrator
│ ├── exceptions/ # business exception
│ ├── routes/ # domain orchestrator
│ ├── settings.py # application global settings
│ ├── engine.py # database engine
├── utils/
├── libs/
├── tests/
│ ├── conftest.py
│ ├── <by_domain>/test_<xxx>.py
├── migrations/
├── scripts/
├── docs/
├── .env
├── .env.example
├── logging.ini
├── pyproject.toml
└── run.py # FastAPI app
File Templates:
- route.py - Router module with endpoints
- schemas.py - Pydantic request/response schemas
- models.py - SQLAlchemy database models
- dependencies.py - Validation dependencies
- manager.py - Domain orchestrator/service
- exceptions.py - Module-specific exceptions
- run.py - Main application file
- settings.py - Global configuration
- engine.py - Database engine setup
Notes:
- Store all domain modules inside
app/folder - Each subfolder (
schemas/,models/,managers/,routes/) is organized by domain- e.g.,
app/schemas/posts.py,app/managers/posts_manager.py,app/routes/posts.py
- e.g.,
- Import from other packages with explicit module names
References
Read revelant references before you really start your task:
- API Best Practices
- Async Patterns
- Pydantic Best Practices
- Dependencies Guide
- Database Operations
- Testing
General Best Practices
- Use Ruff for fast linting and formatting:
ruff check --fix src
ruff format src