v0.21.1-fastapi
This commit is contained in:
@@ -52,6 +52,40 @@ def create_app() -> FastAPI:
|
||||
openapi_url="/apispec.json"
|
||||
)
|
||||
|
||||
# 自定义 OpenAPI schema 以支持 Bearer Token 认证
|
||||
def custom_openapi():
|
||||
if app.openapi_schema:
|
||||
return app.openapi_schema
|
||||
from fastapi.openapi.utils import get_openapi
|
||||
openapi_schema = get_openapi(
|
||||
title=app.title,
|
||||
version=app.version,
|
||||
description=app.description,
|
||||
routes=app.routes,
|
||||
)
|
||||
# 添加安全方案定义(HTTPBearer 会自动注册为 "HTTPBearer")
|
||||
# 如果 components 不存在,先创建它
|
||||
if "components" not in openapi_schema:
|
||||
openapi_schema["components"] = {}
|
||||
if "securitySchemes" not in openapi_schema["components"]:
|
||||
openapi_schema["components"]["securitySchemes"] = {}
|
||||
|
||||
# 移除 CustomHTTPBearer(如果存在),只保留 HTTPBearer
|
||||
if "CustomHTTPBearer" in openapi_schema["components"]["securitySchemes"]:
|
||||
del openapi_schema["components"]["securitySchemes"]["CustomHTTPBearer"]
|
||||
|
||||
# 添加/更新 HTTPBearer 安全方案(FastAPI 默认名称)
|
||||
openapi_schema["components"]["securitySchemes"]["HTTPBearer"] = {
|
||||
"type": "http",
|
||||
"scheme": "bearer",
|
||||
"bearerFormat": "JWT",
|
||||
"description": "输入 token,可以不带 'Bearer ' 前缀,系统会自动添加"
|
||||
}
|
||||
app.openapi_schema = openapi_schema
|
||||
return app.openapi_schema
|
||||
|
||||
app.openapi = custom_openapi
|
||||
|
||||
# 添加CORS中间件
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
@@ -124,22 +158,20 @@ def setup_routes(app: FastAPI):
|
||||
from api.apps.user_app_fastapi import router as user_router
|
||||
from api.apps.kb_app import router as kb_router
|
||||
from api.apps.document_app import router as document_router
|
||||
from api.apps.file_app import router as file_router
|
||||
from api.apps.file2document_app import router as file2document_router
|
||||
from api.apps.mcp_server_app import router as mcp_router
|
||||
from api.apps.tenant_app import router as tenant_router
|
||||
from api.apps.llm_app import router as llm_router
|
||||
from api.apps.chunk_app import router as chunk_router
|
||||
from api.apps.mcp_server_app import router as mcp_router
|
||||
from api.apps.canvas_app import router as canvas_router
|
||||
|
||||
app.include_router(user_router, prefix=f"/{API_VERSION}/user", tags=["User"])
|
||||
app.include_router(kb_router, prefix=f"/{API_VERSION}/kb", tags=["KB"])
|
||||
app.include_router(kb_router, prefix=f"/{API_VERSION}/kb", tags=["KnowledgeBase"])
|
||||
app.include_router(document_router, prefix=f"/{API_VERSION}/document", tags=["Document"])
|
||||
app.include_router(file_router, prefix=f"/{API_VERSION}/file", tags=["File"])
|
||||
app.include_router(file2document_router, prefix=f"/{API_VERSION}/file2document", tags=["File2Document"])
|
||||
app.include_router(mcp_router, prefix=f"/{API_VERSION}/mcp", tags=["MCP"])
|
||||
app.include_router(tenant_router, prefix=f"/{API_VERSION}/tenant", tags=["Tenant"])
|
||||
app.include_router(llm_router, prefix=f"/{API_VERSION}/llm", tags=["LLM"])
|
||||
app.include_router(chunk_router, prefix=f"/{API_VERSION}/chunk", tags=["Chunk"])
|
||||
app.include_router(mcp_router, prefix=f"/{API_VERSION}/mcp", tags=["MCP"])
|
||||
app.include_router(canvas_router, prefix=f"/{API_VERSION}/canvas", tags=["Canvas"])
|
||||
|
||||
|
||||
|
||||
def get_current_user_from_token(authorization: str):
|
||||
"""从token获取当前用户"""
|
||||
|
||||
Reference in New Issue
Block a user