|
14 | 14 |
|
15 | 15 | # static files |
16 | 16 | app.mount("/openapi", StaticFiles(directory="static/openapi"), name="openapi") |
17 | | - |
| 17 | +# templates |
18 | 18 | templates = Jinja2Templates(directory="templates") |
19 | 19 |
|
20 | | -fastapi_ui_html = """ |
21 | | -<html> |
22 | | -<head> |
23 | | - <link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui.css"> |
24 | | - <link rel="shortcut icon" href="https://fastapi.tiangolo.com/img/favicon.png"> |
25 | | - <title>{{ title }}</title> |
26 | | -</head> |
27 | | -<body> |
28 | | - <div id="swagger-ui"></div> |
29 | | -
|
30 | | - <script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.js"></script> |
31 | | - <script src="https://unpkg.com/swagger-ui-dist/swagger-ui-standalone-preset.js"></script> |
32 | | -
|
33 | | - <script> |
34 | | - const ui = SwaggerUIBundle({ |
35 | | - urls: {{ urls | safe }}, |
36 | | - dom_id: "#swagger-ui", |
37 | | - layout: "StandaloneLayout", |
38 | | - deepLinking: true, |
39 | | - showExtensions: true, |
40 | | - showCommonExtensions: true, |
41 | | - oauth2RedirectUrl: window.location.origin + '/oauth2-redirect', |
42 | | - presets: [ |
43 | | - SwaggerUIBundle.presets.apis, |
44 | | - SwaggerUIStandalonePreset |
45 | | - ], |
46 | | - }) |
47 | | - </script> |
48 | | -
|
49 | | -</body> |
50 | | -</html> |
51 | | -""" |
52 | 20 |
|
53 | 21 | @app.get("/proxy", include_in_schema=False) |
54 | 22 | async def proxy(url: str, headers: str = None): |
@@ -111,10 +79,11 @@ async def docs(request: Request): |
111 | 79 | for swagger in swaggers: |
112 | 80 | swagger["url"] = apply_proxy_to_openapi(swagger["url"], parse_headers(swagger["header"])) |
113 | 81 |
|
114 | | - # Renderiza o template HTML diretamente |
115 | | - html = fastapi_ui_html.replace( |
116 | | - "{{ urls | safe }}", json.dumps(swaggers) |
117 | | - ).replace( |
118 | | - "{{ title }}", os.environ.get("TITLE", "API Documentation") |
| 82 | + return templates.TemplateResponse( |
| 83 | + "swagger.html", |
| 84 | + { |
| 85 | + "request": request, |
| 86 | + "urls": swaggers, |
| 87 | + "title": os.environ.get("TITLE", "API Documentation"), |
| 88 | + } |
119 | 89 | ) |
120 | | - return HTMLResponse(content=html) |
0 commit comments