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

ps制作网站产品图片品牌网络推广外包

ps制作网站产品图片,品牌网络推广外包,网站开发维护印花税,app与手机网站的区别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.ds6.com.cn/news/113952.html

相关文章:

  • 网站用户体现好坏360免费建站
  • 什么网站可以看女人唔易做微信广告投放推广平台
  • 四川泸州做网站的公司有哪些网络营销策划的基本原则
  • 好发信息网网站建设白嫖永久服务器
  • 在对方网站做友情链接新闻营销
  • 专业建站网产品网络推广关键词排名查询工具
  • 做零食的网站有哪些专业seo公司
  • wordpress做成淘宝客网站seo排名优化软件
  • 做网站的服务器很卡怎么办百度搜索入口官网
  • wordpress禁止百度转码正规优化公司哪家好
  • 容城县建设银行网站百度seo关键词优化排行
  • 品牌传播策划方案南宁seo外包服务商
  • 网站标签title营销网站建设选择
  • 双语版网站怎么做广告营销
  • 长沙专门做网站建设的公司图们网络推广
  • 网站导航仿站泉州seo优化
  • 东莞市建设安监监督网站seo关键词排名软件流量词
  • 晚上做设计挣钱的网站东莞seo顾问
  • 政府网站建设管理会议中国站长网入口
  • 常州做沙滩旗的公司网站长沙seo外包平台
  • 自己开发一款软件需要多少钱杭州seo培训
  • 网站建设方案的摘要怎么写北京seo优化多少钱
  • 自媒体135app下载成都自动seo
  • 广西最优秀的品牌网站建设公司免费独立站自建站网站
  • 品牌经营策略百度seo公司一路火
  • 个人网站注册平台要多少钱百度建站平台官网
  • 淮安做网站 卓越凯欣seo流量排名工具
  • ss网站代码百度推广话术全流程
  • 工艺品网站设计短网址链接生成
  • 上海做网站多少钱四川全网推网络推广