当前位置: 首页 > news >正文

Python创建FastApi项目模板

1. 项目结构规范

myproject/
├── app/
│   ├── core/              # 核心配置
│   │   ├── config.py     # 环境配置
│   │   └── security.py   # 安全配置
│   ├── routers/          # 路由模块
│   │   └── users.py      # 用户路由
│   ├── models/           # 数据库模型
│   │   └── user.py       # 用户模型
│   ├── schemas/          # Pydantic模型
│   │   └── user.py       # 用户Schema
│   ├── services/         # 业务逻辑
│   │   └── user.py       # 用户服务
│   ├── dependencies.py   # 依赖注入
│   └── db/               # 数据库配置
├── tests/                # 测试用例
│   └── test_users.py     # 用户测试
├── requirements.txt      # 依赖文件
└── main.py               # 入口文件

2. 核心代码示例

配置管理 (app/core/config.py)
from pydantic import BaseSettingsclass Settings(BaseSettings):API_V1_STR: str = "/api/v1"DB_URL: str = "postgresql+asyncpg://user:pass@localhost:5432/db"JWT_SECRET: str = "secret-key"class Config:env_file = ".env"settings = Settings()
路由示例 (app/routers/users.py)
from fastapi import APIRouter, Depends, HTTPException
from app.services.user import UserService
from app.schemas.user import UserCreate, UserResponse
from app.dependencies import get_dbrouter = APIRouter(prefix="/users", tags=["users"])@router.post("/", response_model=UserResponse)
async def create_user(user: UserCreate,service: UserService = Depends(UserService)
):try:return await service.create_user(user)except Exception as e:raise HTTPException(400, str(e))
服务层 (app/services/user.py)
from app.models.user import User
from app.schemas.user import UserCreate, UserResponse
from app.core.security import get_password_hashclass UserService:def __init__(self, db):self.db = dbasync def create_user(self, user_create: UserCreate):hashed_password = get_password_hash(user_create.password)user = User(email=user_create.email,hashed_password=hashed_password)self.db.add(user)await self.db.commit()return UserResponse(**user.dict())
数据库模型 (app/models/user.py)
from sqlalchemy import Column, Integer, String
from app.db.base import Baseclass User(Base):__tablename__ = "users"id = Column(Integer, primary_key=True)email = Column(String, unique=True, index=True)hashed_password = Column(String)
Schema验证 (app/schemas/user.py)
from pydantic import BaseModel, EmailStrclass UserBase(BaseModel):email: EmailStrclass UserCreate(UserBase):password: strclass UserResponse(UserBase):id: intclass Config:orm_mode = True

3. 关键规范说明

  1. 分层架构

    • 路由层:只处理HTTP协议和参数验证
    • 服务层:处理业务逻辑
    • 数据访问层:处理数据库操作
  2. 依赖注入

# app/dependencies.py
async def get_db():async_session = sessionmaker(expire_on_commit=False,class_=AsyncSession)async with async_session() as session:yield session
  1. 安全规范
# app/core/security.py
from passlib.context import CryptContextpwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")def verify_password(plain_password, hashed_password):return pwd_context.verify(plain_password, hashed_password)def get_password_hash(password):return pwd_context.hash(password)
  1. 异常处理
# app/main.py
from fastapi import FastAPI
from fastapi.exceptions import RequestValidationErrorapp = FastAPI()@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):return JSONResponse(status_code=400,content={"detail": exc.errors(), "body": exc.body},)
  1. 测试规范
# tests/test_users.py
from fastapi.testclient import TestClientdef test_create_user():client = TestClient(app)response = client.post("/users/", json={"email": "test@example.com","password": "string"})assert response.status_code == 200assert "id" in response.json()

4. 最佳实践建议

  1. 使用异步数据库驱动(如asyncpg)
  2. 为每个路由添加OpenAPI标签
  3. 强制类型注解(mypy兼容)
  4. 使用alembic进行数据库迁移
  5. 统一响应格式:
class BaseResponse(BaseModel):code: int = 200message: str = "success"data: Any = None
  1. 接口版本管理:
router = APIRouter(prefix="/v1/users")
  1. 日志记录规范:
import logging
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",level=logging.INFO
)

该方案结合了FastAPI官方推荐实践和企业级开发经验,在保持灵活性的同时确保代码质量。建议配合以下工具链:

  • 代码格式化:black + isort
  • 静态检查:mypy + pylint
  • 测试覆盖:pytest + coverage
  • 文档生成:swagger UI + redoc
  • CI/CD:GitHub Actions/GitLab CI

可根据具体业务需求扩展中间件、缓存、任务队列等模块,但需保持核心架构的稳定性。

http://www.lryc.cn/news/536862.html

相关文章:

  • TCNE 网络安全
  • 车规MCU处理器选择Cortex-M7还是Cortex-R52?
  • 什么是计算机中的 “终端”?
  • LeetCode刷题---字符串---819
  • SSH IBM AIX服务器相关指标解读
  • Wireshark TS | 再谈虚假的 TCP Spurious Retransmission
  • 基于kafka、celery的日志收集报警项目
  • QML使用ChartView绘制饼状图
  • 头歌实验--面向对象程序设计
  • DeepSeek-R1 蒸馏 Qwen 和 Llama 架构 企业级RAG知识库
  • App UI自动化--Appium学习--第二篇
  • 【SpringBoot实现全局API限频】 最佳实践
  • Day1 25/2/14 FRI
  • 开发板适配之I2C-RTC
  • vuedraggable固定某一item的记录
  • 我的新书《青少年Python趣学编程(微课视频版)》出版了!
  • 前端开发入门一
  • Linux(Centos 7.6)命令详解:head
  • HTTP请求X-Forwarded-For注入
  • 《生息之地》入围柏林主竞赛,总制片人蒋浩助力青年导演走向国际
  • 实践记录--电脑故障的问题定位和处理回顾--磁盘故障已解决
  • uni-app 学习(一)
  • Ubuntu 22.04 Desktop企业级基础配置操作指南
  • QILSTE H4-105LB/5M高亮蓝光LED灯珠 发光二极管LED
  • 【Elasticsearch】Elasticsearch检索方式全解析:从基础到实战(一)
  • 封装neo4j的持久层和服务层
  • 基于Spring Boot的宠物爱心组织管理系统的设计与实现(LW+源码+讲解)
  • error: conflicting types for ‘SSL_SESSION_get_master_key’
  • 测试狗参加国家超级计算成都中心2024年度用户大会
  • 从2025年起:数字化建站PHP 8.1应成为建站开发的基准线