Skip to content

Commit c6aceba

Browse files
committed
use template to render swagger
Signed-off-by: Luiz Oliveira <ziuloliveira@gmail.com>
1 parent a5f44bf commit c6aceba

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

server.py

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,9 @@
1414

1515
# static files
1616
app.mount("/openapi", StaticFiles(directory="static/openapi"), name="openapi")
17-
17+
# templates
1818
templates = Jinja2Templates(directory="templates")
1919

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-
"""
5220

5321
@app.get("/proxy", include_in_schema=False)
5422
async def proxy(url: str, headers: str = None):
@@ -111,10 +79,11 @@ async def docs(request: Request):
11179
for swagger in swaggers:
11280
swagger["url"] = apply_proxy_to_openapi(swagger["url"], parse_headers(swagger["header"]))
11381

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+
}
11989
)
120-
return HTMLResponse(content=html)

templates/swagger.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<html>
2+
<head>
3+
<link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui.css">
4+
<link rel="shortcut icon" href="https://fastapi.tiangolo.com/img/favicon.png">
5+
<title>{{ title }}</title>
6+
</head>
7+
<body>
8+
<div id="swagger-ui"></div>
9+
10+
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
11+
<script src="https://unpkg.com/swagger-ui-dist/swagger-ui-standalone-preset.js"></script>
12+
13+
<script>
14+
const ui = SwaggerUIBundle({
15+
urls: {{ urls | safe }},
16+
dom_id: "#swagger-ui",
17+
layout: "StandaloneLayout",
18+
deepLinking: true,
19+
showExtensions: true,
20+
showCommonExtensions: true,
21+
oauth2RedirectUrl: window.location.origin + '/oauth2-redirect',
22+
presets: [
23+
SwaggerUIBundle.presets.apis,
24+
SwaggerUIStandalonePreset
25+
],
26+
})
27+
</script>
28+
29+
</body>
30+
</html>

0 commit comments

Comments
 (0)