refactor: 移除冗余代码,重构API路由,整合工作流相关功能,提升代码结构和可维护性
This commit is contained in:
98
workflow_service/routes/workflow.py
Normal file
98
workflow_service/routes/workflow.py
Normal file
@@ -0,0 +1,98 @@
|
||||
import json
|
||||
from typing import Optional, List
|
||||
|
||||
from fastapi import APIRouter, Request, HTTPException, Path
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from workflow_service import comfyui_client
|
||||
from workflow_service import database
|
||||
|
||||
|
||||
workflow_router = APIRouter(
|
||||
prefix="/api/workflow",
|
||||
tags=["Workflow"],
|
||||
)
|
||||
|
||||
|
||||
@workflow_router.post("", status_code=201)
|
||||
async def publish_workflow_endpoint(request: Request):
|
||||
"""
|
||||
发布工作流
|
||||
"""
|
||||
try:
|
||||
data = await request.json()
|
||||
name, wf_json = data.get("name"), data.get("workflow")
|
||||
if not name or not wf_json:
|
||||
raise HTTPException(
|
||||
status_code=400, detail="`name` and `workflow` fields are required."
|
||||
)
|
||||
await database.save_workflow(name, json.dumps(wf_json))
|
||||
print(f"Workflow '{name}' published.")
|
||||
return JSONResponse(
|
||||
content={"status": "success", "message": f"Workflow '{name}' published."},
|
||||
status_code=201,
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Failed to save workflow: {e}")
|
||||
|
||||
|
||||
@workflow_router.get("", response_model=List[dict])
|
||||
async def get_all_workflows_endpoint():
|
||||
"""
|
||||
获取所有工作流
|
||||
"""
|
||||
try:
|
||||
return await database.get_all_workflows()
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Failed to get workflows: {e}")
|
||||
|
||||
|
||||
@workflow_router.delete("/{workflow_name:path}")
|
||||
async def delete_workflow_endpoint(
|
||||
workflow_name: str = Path(
|
||||
...,
|
||||
description="The full, unique name of the workflow to delete, e.g., 'my_workflow [20250101120000]'",
|
||||
)
|
||||
):
|
||||
"""
|
||||
删除工作流
|
||||
"""
|
||||
try:
|
||||
success = await database.delete_workflow(workflow_name)
|
||||
if success:
|
||||
return {"status": "deleted", "name": workflow_name}
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=404, detail=f"Workflow '{workflow_name}' not found"
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Failed to delete workflow: {e}")
|
||||
|
||||
|
||||
@workflow_router.get("/{base_name:path}")
|
||||
async def get_one_workflow_endpoint(base_name: str, version: Optional[str] = None):
|
||||
"""
|
||||
获取工作流规范
|
||||
"""
|
||||
workflow_data = await database.get_workflow(base_name, version)
|
||||
|
||||
if not workflow_data:
|
||||
detail = (
|
||||
f"Workflow '{base_name}'"
|
||||
+ (f" with version '{version}'" if version else " (latest)")
|
||||
+ " not found."
|
||||
)
|
||||
raise HTTPException(status_code=404, detail=detail)
|
||||
|
||||
try:
|
||||
workflow = json.loads(workflow_data["workflow_json"])
|
||||
return {
|
||||
"workflow": workflow,
|
||||
"api_spec": comfyui_client.parse_api_spec(workflow),
|
||||
}
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Failed to parse workflow specification: {e}"
|
||||
)
|
||||
Reference in New Issue
Block a user