server.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import sys,os
  2. current_path = os.getcwd()
  3. sys.path.append(current_path+"\\web")
  4. sys.path.append(current_path+"\\agent")
  5. import json
  6. from fastapi import FastAPI
  7. from fastapi.middleware.cors import CORSMiddleware
  8. from fastapi.staticfiles import StaticFiles
  9. from fastapi.openapi.docs import (
  10. get_redoc_html,
  11. get_swagger_ui_html,
  12. get_swagger_ui_oauth2_redirect_html,
  13. )
  14. def save_api_spec(app: FastAPI):
  15. """
  16. 保存 FastAPI 应用的 OpenAPI 规范到文件中。
  17. """
  18. from fastapi.openapi.utils import get_openapi
  19. import yaml
  20. openapi_schema = get_openapi(
  21. title="FastAPI - Swagger UI",
  22. version="1.0.0",
  23. routes=app.routes
  24. )
  25. print(app.routes)
  26. # 将 JSON 转换为 YAML
  27. yaml_data = yaml.dump(openapi_schema, sort_keys=False)
  28. # 保存为 YAML 文件
  29. with open("openapi.yaml", "w") as f:
  30. f.write(yaml_data)
  31. print("OpenAPI YAML 文件已生成:openapi.yaml")
  32. import logging
  33. logging.basicConfig(level=logging.INFO)
  34. handler = logging.FileHandler('/app/logs/api-server.log', mode='w',encoding="utf-8")
  35. handler.setLevel(logging.INFO)
  36. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  37. handler.setFormatter(formatter)
  38. logging.getLogger().addHandler(handler)
  39. app = FastAPI(title="知识图谱自构建平台系统",
  40. description="知识图谱自构建平台系统API",
  41. version="1.0.0",
  42. root_path="/open-platform",
  43. docs_url=None,
  44. redoc_url=None)
  45. # 允许所有来源的跨域请求
  46. app.add_middleware(
  47. CORSMiddleware,
  48. allow_origins=["*"],
  49. allow_credentials=True,
  50. allow_methods=["*"],
  51. allow_headers=["*"],
  52. )
  53. from router.task_router import task_router
  54. app.include_router(task_router)
  55. from router.file_router import file_router
  56. app.include_router(file_router)
  57. from router.user_router import user_router
  58. app.include_router(user_router)
  59. # from router.graph_mgr_router import graph_mgr_router
  60. # app.include_router(graph_mgr_router)
  61. from router.kb_router import kb_router
  62. app.include_router(kb_router)
  63. #from router.graph_router import graph_router
  64. #app.include_router(graph_router)
  65. #app.include_router(dify_kb_router)
  66. from router.knowledge_base_router import knowledge_base_router
  67. app.include_router(knowledge_base_router)
  68. save_api_spec(app)
  69. app.mount("/static", StaticFiles(directory="static"), name="static")
  70. @app.get("/docs", include_in_schema=False)
  71. async def custom_swagger_ui_html():
  72. return get_swagger_ui_html(
  73. openapi_url=app.openapi_url,
  74. title=app.title + " - Swagger UI",
  75. oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
  76. swagger_js_url="/open-platform/static/swagger-ui-bundle.js",
  77. swagger_css_url="/open-platform/static/swagger-ui.css",
  78. )
  79. @app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False)
  80. async def swagger_ui_redirect():
  81. return get_swagger_ui_oauth2_redirect_html()
  82. @app.get("/redoc", include_in_schema=False)
  83. async def redoc_html():
  84. return get_redoc_html(
  85. openapi_url=app.openapi_url,
  86. title=app.title + " - ReDoc",
  87. redoc_js_url="/open-platform/static/redoc.standalone.js",
  88. )