Skip to Content

路由规划

💡
  • 在”/api”目录中创建”endpoint“目录,用于存放各个接口的实现文件
💡
  • 在”/api/endpoint”目录中创建一个__init__.py文件,用于初始化各个接口的实现文件
  • 在”/api/endpoint”目录中创建一个”comfy_server.py”文件,用于实现comfy_server的接口
/api/endpoint/comfy_server.py
from fastapi import APIRouter router = APIRouter() @router.get("/test", summary="测试接口", description="这是一个用于测试的接口,返回一个简单的 Hello World 消息", response_description="返回一个包含消息的字典")
💡
  • 在”/api/api.py”文件中,导入”/api/endpoint”目录中的各个接口的实现文件,并进行路由的汇总
/api/api.py
from fastapi import APIRouter from api.endpoints import comfy_server router = APIRouter() router.include_router(comfy_server.router,prefix="/comfy_server", tags=["comfy_server"])
💡

在主文件”main.py”中,导入”/api/api.py”文件,并进行路由的汇总注册

/main.py
import uvicorn from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from config import settings as cfg from api.api import router as api_router app = FastAPI( debug=cfg.APP_DEBUG, # 调试模式 title=cfg.PROJECT_NAME, # 项目名称 version=cfg.VERSION, # 项目版本 docs_url=cfg.DOCS_URL, # 文档地址 redoc_url=cfg.REDOC_URL, # 文档地址 ) app.add_middleware( CORSMiddleware, allow_origins=cfg.CORS_ORIGINS, allow_credentials=cfg.CORS_ALLOW_CREDENTIALS, allow_methods=cfg.CORS_ALLOW_METHODS, allow_headers=cfg.CORS_ALLOW_HEADERS, ) # 注册API路由 app.include_router(api_router) if __name__ == "__main__": uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)

目录结构如下:

      • __init__.py
        • comfy_server.py
      • api.py
      • __init__.py
      • __init__.py
      • __init__.py
    • main.py
    • config.py
    • pyproject.toml
    • README.md
    • .python-version
Last updated on