"""Swagger UI for manual experiments: http://127.0.0.1:8080/docs The page shows the course contract contracts/openapi.json as is (it is not generated from our code). Only `servers` is replaced with "/" so that the "Try it out" requests go to the same host the page was opened from. Swagger UI itself (JS/CSS) is loaded by the browser from the jsDelivr CDN. Disable with DOCS_ENABLED=false. """ from __future__ import annotations import json from functools import lru_cache from pathlib import Path from starlette.requests import Request from starlette.responses import HTMLResponse, JSONResponse SPEC_PATH = Path(__file__).resolve().parents[2] / "contracts" / "openapi.json" SWAGGER_UI = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.33.0" PAGE = f""" Messenger lab1 — Swagger UI
""" @lru_cache(maxsize=1) def _spec() -> dict: spec = json.loads(SPEC_PATH.read_text(encoding="utf-8")) spec["servers"] = [{"url": "/", "description": "этот сервер"}] return spec async def swagger_page(request: Request) -> HTMLResponse: return HTMLResponse(PAGE) async def openapi_json(request: Request) -> JSONResponse: return JSONResponse(_spec())