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

ps制作网站产品图片seo关键词推广多少钱

ps制作网站产品图片,seo关键词推广多少钱,手机网站开发标准,设计公司起名字1. 项目结构规范 myproject/ ├── app/ │ ├── core/ # 核心配置 │ │ ├── config.py # 环境配置 │ │ └── security.py # 安全配置 │ ├── routers/ # 路由模块 │ │ └── users.py # 用户路由 │ ├…

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.yidumall.com/news/2737.html

相关文章:

  • wordpress 语言包修改石家庄seo外包的公司
  • 婚纱摄影网站设计免费生成短链接
  • 设置网站开场动画站长工具爱站
  • 网站分类目录大全常德seo公司
  • 西安 网站建设 费用软文广告范例大全
  • 线上营销渠道苏州首页排名关键词优化
  • 做国学类网站合法吗上海网站营销推广
  • 定制开发软件税率抖音seo排名
  • 增城专业建站公司如何优化关键词排名快速首页
  • 梅兰商贸网站开发设计简介福清市百度seo
  • 怎样制作wordpress主题汉化包seo成功的案例和分析
  • 国家重点高新技术企业名单seo在线教程
  • 北京国家建设部网站首页建立网站要多少钱一年
  • 日本男女做受网站深圳发布最新通告
  • 门户网站的区别营销型网站方案
  • 聚美优品网站建设方案seo如何优化的
  • 如何做自媒体和网站签约赚点击seo的定义
  • seo体系网站的建设及优化上海百度推广代理商
  • wordpress w3 total cache上海seo优化外包公司
  • 重庆网站建设首选卓光seo竞价培训
  • 建设网站是做什么常德网站seo
  • 自制头像生成器网站一个新产品策划方案
  • 做衣服招临工在什么网站找百度搜索高级搜索技巧
  • 大连 网站制作国外搜索引擎大全
  • 仪征市企业网站建设公司重庆seo排名软件
  • 如何进行网站建设云服务器免费
  • app开发做网站故事式的软文广告例子
  • 网站建设属什么资产google adwords关键词工具
  • 苹果手机做任务网站媒体推广
  • 公众号怎么赚钱seo综合查询怎么关闭