代码优化

This commit is contained in:
zhangtao
2025-02-16 04:25:08 +08:00
parent aae8aa91fd
commit 1662d3f1d8
5 changed files with 228 additions and 170 deletions
@@ -5,8 +5,6 @@ from fastapi import APIRouter, Depends, Request, Query, BackgroundTasks, WebSock
from fastapi.responses import JSONResponse
from sqlalchemy.ext.asyncio import AsyncSession
from app.config.setting import settings
from app.common.response import ErrorResponse, SuccessResponse, StreamResponse
from app.api.v1.services.system.auth_service import (
@@ -26,7 +24,6 @@ from app.core.dependencies import (
from app.core.router_class import OperationLogRoute
from app.core.security import CustomOAuth2PasswordRequestForm
from app.core.logger import logger
from app.core.tasks import celery_app, background_task
router = APIRouter(route_class=OperationLogRoute)
@@ -79,40 +76,3 @@ async def logout(
logger.info('退出成功')
return SuccessResponse(msg='退出成功')
return ErrorResponse(msg='退出失败')
# ws://127.0.0.1:8000/api/v1/system/auth/ws
@router.websocket("/ws", name="websocket")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(f"Message text was: {data}")
@router.post("/celery-task", summary="模拟celery进行后台任务")
async def process_data(data: str):
# 调用 Celery 任务
result = background_task.delay(data)
return SuccessResponse(msg=f"任务已提交到 Celery: {result}")
@router.get("/celery-task/{task_id}", summary="获取celery任务状态")
async def get_task_status(task_id: str):
task = celery_app.AsyncResult(task_id)
return {
"task_id": task_id,
"status": task.status,
}
# 模拟流响应
@router.get("/bg-tsak-stream", summary="模拟fastapi自带后台任务-模拟流式响应")
async def stream_response(data: str,background_tasks: BackgroundTasks):
def log_task(message):
logger.info(message)
background_tasks.add_task(log_task, "Streaming started")
return StreamResponse(
data = background_task(data=data),
headers={"X-Custom-Header": "Streaming-Response"},
media_type="text/plain",
)