forked from mtusi2026/mtusi2026lab2
reworked
This commit is contained in:
+52
@@ -0,0 +1,52 @@
|
||||
import json
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import HTMLResponse, JSONResponse, Response
|
||||
|
||||
_SPEC_CANDIDATES = (
|
||||
Path(__file__).resolve().parents[1] / "contracts" / "openapi.json",
|
||||
Path("/app/contracts/openapi.json"),
|
||||
)
|
||||
|
||||
_SWAGGER_HTML = """<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>MTUSI Messenger — Swagger (lab2)</title>
|
||||
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui.css"/>
|
||||
</head>
|
||||
<body>
|
||||
<div id="swagger-ui"></div>
|
||||
<script src="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui-bundle.js"></script>
|
||||
<script>
|
||||
window.onload = function () {
|
||||
SwaggerUIBundle({
|
||||
url: "/openapi.json",
|
||||
dom_id: "#swagger-ui",
|
||||
deepLinking: true,
|
||||
persistAuthorization: true,
|
||||
tryItOutEnabled: true
|
||||
});
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def _load_spec() -> dict:
|
||||
for path in _SPEC_CANDIDATES:
|
||||
if path.is_file():
|
||||
return json.loads(path.read_text(encoding="utf-8"))
|
||||
return {"openapi": "3.1.0", "info": {"title": "MTUSI Messenger", "version": "1.2.0"}, "paths": {}}
|
||||
|
||||
|
||||
async def openapi_json(_: Request) -> Response:
|
||||
return JSONResponse(_load_spec())
|
||||
|
||||
|
||||
async def swagger_ui(_: Request) -> Response:
|
||||
return HTMLResponse(_SWAGGER_HTML)
|
||||
Reference in New Issue
Block a user