diff --git a/gunicorn_conf.py b/gunicorn_conf.py new file mode 100644 index 0000000..d561529 --- /dev/null +++ b/gunicorn_conf.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +import multiprocessing +import os + +# 服务器socket +bind = "0.0.0.0:8000" + +# 工作进程数 +workers = multiprocessing.cpu_count() * 2 + 1 + +# 工作进程类型 +worker_class = "uvicorn.workers.UvicornWorker" + +# 工作进程超时时间 +timeout = 120 + +# 保持连接 +keepalive = 5 + +# 最大请求数 +max_requests = 1000 +max_requests_jitter = 100 + +# 日志配置 +accesslog = "-" +errorlog = "-" +loglevel = "info" + +# 进程名称 +proc_name = "fastapi_app" + +# 工作模式 +worker_tmp_dir = "/dev/shm" + +# 环境变量 +raw_env = ["PYTHONPATH=/main", "PYTHONUNBUFFERED=1"] + +# 预加载应用 +preload_app = True + +# 优雅退出时间 +graceful_timeout = 30 diff --git a/main.py b/main.py index 4439ada..19688c8 100644 --- a/main.py +++ b/main.py @@ -6,11 +6,12 @@ import uvicorn from pathlib import Path from typing import Dict, Any, Optional, List from fastapi import FastAPI, HTTPException, Depends - +from public_function.auth import verify_tk_token from public_function.public_func import read_config, create_logger from model.model import GoodsInfo, DataReceive, AccountCreate, AccountUpdate, DeviceResetData app = FastAPI() +app.middleware("http")(verify_tk_token) logger = create_logger(file_name="crawler_main") diff --git a/model/model.py b/model/model.py index 965e744..d4e3bde 100644 --- a/model/model.py +++ b/model/model.py @@ -5,24 +5,24 @@ from typing import Optional, Dict, Any # 定义数据模型 class AccountCreate(BaseModel): - account_id: str = Field(..., min_length=1, max_length=128, description="账号ID") - password: str = Field(..., min_length=1, max_length=128, description="密码") - country: str = Field(..., min_length=1, max_length=128, description="账号所在国家") - app_name: str = Field(..., min_length=1, max_length=128, description="应用名称") + account_id: str = Field(..., description="账号ID") + password: str = Field(..., description="密码") + country: str = Field(..., description="账号所在国家") + app_name: str = Field(..., description="应用名称") class AccountUpdate(BaseModel): account_id: str = Field(..., description="账号ID") - app_name: str = Field(..., min_length=1, max_length=128, description="应用名称") + app_name: str = Field(..., description="应用名称") status: int = Field(..., ge=1, le=10, description="状态:1-空闲,2-使用中,3-暂停使用(后续还能使用),4-账号已无法使用") class GoodsInfo(BaseModel): - country: str = Field(..., min_length=1, max_length=128, description="账号所在国家") - app_name: str = Field(..., min_length=1, max_length=128, description="应用名称") - goods_id: str = Field(..., min_length=1, max_length=128, description="商品ID") - store_id: str = Field(..., min_length=1, max_length=128, description="店铺ID") - is_re_crawl: str = Field(..., min_length=0, max_length=128, description="是否重新抓取") + country: str = Field(..., description="账号所在国家") + app_name: str = Field(..., description="应用名称") + goods_id: str = Field(..., description="商品ID") + store_id: str = Field(..., description="店铺ID") + is_re_crawl: bool = Field(..., description="是否重新抓取") class DataReceive(BaseModel): diff --git a/public_function/auth.py b/public_function/auth.py new file mode 100644 index 0000000..900d482 --- /dev/null +++ b/public_function/auth.py @@ -0,0 +1,14 @@ +from fastapi import Request +from fastapi.responses import JSONResponse + +TOKEN = "opB4ztbdw45xFoJbXti20520bsEq3UDKKAtiDWHnGjjhP6v0KNFjqBM7bfzto6GLdUPviYnVdCgdCJYqe42nPoy6mvW59F3TPQZu" + + +async def verify_tk_token(request: Request, call_next): + """鉴权中间件""" + + token = request.headers.get("token") + if token != TOKEN: + return JSONResponse(status_code=401, content={"detail": "Unauthorized"}) + + return await call_next(request)