Skip to content

Commit

Permalink
chore: use fastapi-swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
dantetemplar committed Sep 19, 2024
1 parent e1c911d commit 017f777
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
18 changes: 16 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pyyaml = "^6.0.1"
uvicorn = "^0.30.0"
colorlog = "^6.8.2"
prometheus-fastapi-instrumentator = "^7.0.0"
fastapi-swagger = "^0.2.3"

[tool.poetry.group.prod]
optional = true
Expand Down
29 changes: 4 additions & 25 deletions src/api/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
__all__ = ["app"]

from fastapi import FastAPI, Request
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi import FastAPI
from fastapi_swagger import patch_fastapi
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import RedirectResponse

import src.logging_ # noqa: F401
from src.api import docs
Expand Down Expand Up @@ -35,6 +34,8 @@
redoc_url=None,
)

patch_fastapi(app)

app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_allow_origins,
Expand All @@ -59,25 +60,3 @@
from prometheus_fastapi_instrumentator import Instrumentator

Instrumentator(excluded_handlers=["/metrics"]).instrument(app).expose(app)


@app.get("/", tags=["System"], include_in_schema=False)
async def redirect_from_root(request: Request):
# Redirect to docs
return RedirectResponse(request.url_for("swagger_ui_html"), status_code=302)


@app.get("/docs", tags=["System"], include_in_schema=False)
async def swagger_ui_html(request: Request):
root_path = request.scope.get("root_path", "").rstrip("/")
openapi_url = root_path + app.openapi_url

return get_swagger_ui_html(
openapi_url=openapi_url,
title=app.title + " - Swagger UI",
swagger_js_url="https://api.innohassle.ru/swagger/swagger-ui-bundle.js",
swagger_css_url="https://api.innohassle.ru/swagger/swagger-ui.css",
swagger_favicon_url="https://api.innohassle.ru/swagger/favicon.png",
oauth2_redirect_url=None,
swagger_ui_parameters={"tryItOutEnabled": True, "persistAuthorization": True, "filter": True},
)

0 comments on commit 017f777

Please sign in to comment.