123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import sys,os
- current_path = os.getcwd()
- sys.path.append(current_path+"\\web")
- sys.path.append(current_path+"\\agent")
- import json
- from fastapi import FastAPI
- from fastapi.middleware.cors import CORSMiddleware
- from fastapi.staticfiles import StaticFiles
- from fastapi.openapi.docs import (
- get_redoc_html,
- get_swagger_ui_html,
- get_swagger_ui_oauth2_redirect_html,
- )
- def save_api_spec(app: FastAPI):
- """
- 保存 FastAPI 应用的 OpenAPI 规范到文件中。
- """
- from fastapi.openapi.utils import get_openapi
- import yaml
- openapi_schema = get_openapi(
- title="FastAPI - Swagger UI",
- version="1.0.0",
- routes=app.routes
- )
- print(app.routes)
- # 将 JSON 转换为 YAML
- yaml_data = yaml.dump(openapi_schema, sort_keys=False)
- # 保存为 YAML 文件
- with open("openapi.yaml", "w") as f:
- f.write(yaml_data)
- print("OpenAPI YAML 文件已生成:openapi.yaml")
-
- import logging
- logging.basicConfig(level=logging.INFO)
- handler = logging.FileHandler('/app/logs/api-server.log', mode='w',encoding="utf-8")
- handler.setLevel(logging.INFO)
- formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
- handler.setFormatter(formatter)
- logging.getLogger().addHandler(handler)
- app = FastAPI(title="知识图谱自构建平台系统",
- description="知识图谱自构建平台系统API",
- version="1.0.0",
- root_path="/open-platform",
- docs_url=None,
- redoc_url=None)
- # 允许所有来源的跨域请求
- app.add_middleware(
- CORSMiddleware,
- allow_origins=["*"],
- allow_credentials=True,
- allow_methods=["*"],
- allow_headers=["*"],
- )
- from router.task_router import task_router
- app.include_router(task_router)
- from router.file_router import file_router
- app.include_router(file_router)
- from router.user_router import user_router
- app.include_router(user_router)
- # from router.graph_mgr_router import graph_mgr_router
- # app.include_router(graph_mgr_router)
- from router.kb_router import kb_router
- app.include_router(kb_router)
- #from router.graph_router import graph_router
- #app.include_router(graph_router)
- #app.include_router(dify_kb_router)
- from router.knowledge_base_router import knowledge_base_router
- app.include_router(knowledge_base_router)
- save_api_spec(app)
- app.mount("/static", StaticFiles(directory="static"), name="static")
- @app.get("/docs", include_in_schema=False)
- async def custom_swagger_ui_html():
- return get_swagger_ui_html(
- openapi_url=app.openapi_url,
- title=app.title + " - Swagger UI",
- oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
- swagger_js_url="/open-platform/static/swagger-ui-bundle.js",
- swagger_css_url="/open-platform/static/swagger-ui.css",
- )
- @app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False)
- async def swagger_ui_redirect():
- return get_swagger_ui_oauth2_redirect_html()
- @app.get("/redoc", include_in_schema=False)
- async def redoc_html():
- return get_redoc_html(
- openapi_url=app.openapi_url,
- title=app.title + " - ReDoc",
- redoc_js_url="/open-platform/static/redoc.standalone.js",
- )
|