代码优化
This commit is contained in:
parent
203b4ff697
commit
fbf7ef1301
|
|
@ -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
|
||||||
3
main.py
3
main.py
|
|
@ -6,11 +6,12 @@ import uvicorn
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict, Any, Optional, List
|
from typing import Dict, Any, Optional, List
|
||||||
from fastapi import FastAPI, HTTPException, Depends
|
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 public_function.public_func import read_config, create_logger
|
||||||
from model.model import GoodsInfo, DataReceive, AccountCreate, AccountUpdate, DeviceResetData
|
from model.model import GoodsInfo, DataReceive, AccountCreate, AccountUpdate, DeviceResetData
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
app.middleware("http")(verify_tk_token)
|
||||||
logger = create_logger(file_name="crawler_main")
|
logger = create_logger(file_name="crawler_main")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,24 +5,24 @@ from typing import Optional, Dict, Any
|
||||||
|
|
||||||
# 定义数据模型
|
# 定义数据模型
|
||||||
class AccountCreate(BaseModel):
|
class AccountCreate(BaseModel):
|
||||||
account_id: str = Field(..., min_length=1, max_length=128, description="账号ID")
|
account_id: str = Field(..., description="账号ID")
|
||||||
password: str = Field(..., min_length=1, max_length=128, description="密码")
|
password: str = Field(..., description="密码")
|
||||||
country: str = Field(..., min_length=1, max_length=128, description="账号所在国家")
|
country: str = Field(..., description="账号所在国家")
|
||||||
app_name: str = Field(..., min_length=1, max_length=128, description="应用名称")
|
app_name: str = Field(..., description="应用名称")
|
||||||
|
|
||||||
|
|
||||||
class AccountUpdate(BaseModel):
|
class AccountUpdate(BaseModel):
|
||||||
account_id: str = Field(..., description="账号ID")
|
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-账号已无法使用")
|
status: int = Field(..., ge=1, le=10, description="状态:1-空闲,2-使用中,3-暂停使用(后续还能使用),4-账号已无法使用")
|
||||||
|
|
||||||
|
|
||||||
class GoodsInfo(BaseModel):
|
class GoodsInfo(BaseModel):
|
||||||
country: str = Field(..., min_length=1, max_length=128, description="账号所在国家")
|
country: str = Field(..., description="账号所在国家")
|
||||||
app_name: str = Field(..., min_length=1, max_length=128, description="应用名称")
|
app_name: str = Field(..., description="应用名称")
|
||||||
goods_id: str = Field(..., min_length=1, max_length=128, description="商品ID")
|
goods_id: str = Field(..., description="商品ID")
|
||||||
store_id: str = Field(..., min_length=1, max_length=128, description="店铺ID")
|
store_id: str = Field(..., description="店铺ID")
|
||||||
is_re_crawl: str = Field(..., min_length=0, max_length=128, description="是否重新抓取")
|
is_re_crawl: bool = Field(..., description="是否重新抓取")
|
||||||
|
|
||||||
|
|
||||||
class DataReceive(BaseModel):
|
class DataReceive(BaseModel):
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
Loading…
Reference in New Issue