Skip to content

Commit

Permalink
Merge pull request #27 from community-of-python/vrslev-patch-8
Browse files Browse the repository at this point in the history
Fix offline Swagger UI for Litestar
  • Loading branch information
vrslev authored Oct 10, 2024
2 parents b969235 + 3d59676 commit 0a5693a
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions microbootstrap/bootstrappers/litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from litestar.config.cors import CORSConfig as LitestarCorsConfig
from litestar.contrib.opentelemetry.config import OpenTelemetryConfig as LitestarOpentelemetryConfig
from litestar.contrib.prometheus import PrometheusConfig, PrometheusController
from litestar.openapi.plugins import SwaggerRenderPlugin
from litestar_offline_docs import generate_static_files_config
from sentry_sdk.integrations.litestar import LitestarIntegration

Expand Down Expand Up @@ -51,24 +52,30 @@ def bootstrap(self) -> None:
@LitestarBootstrapper.use_instrument()
class LitestarSwaggerInstrument(SwaggerInstrument):
def bootstrap_before(self) -> dict[str, typing.Any]:
class LitestarOpenAPIController(openapi.OpenAPIController):
path = self.instrument_config.swagger_path
if self.instrument_config.swagger_offline_docs:
swagger_ui_standalone_preset_js_url = (
f"{self.instrument_config.service_static_path}/swagger-ui-standalone-preset.js"
)
swagger_bundle_path: str = f"{self.instrument_config.service_static_path}/swagger-ui-bundle.js"
swagger_css_url: str = f"{self.instrument_config.service_static_path}/swagger-ui.css"
render_plugins: typing.Final = (
(
SwaggerRenderPlugin(
js_url=f"{self.instrument_config.service_static_path}/swagger-ui-bundle.js",
css_url=f"{self.instrument_config.service_static_path}/swagger-ui.css",
standalone_preset_js_url=(
f"{self.instrument_config.service_static_path}/swagger-ui-standalone-preset.js"
),
),
)
if self.instrument_config.swagger_offline_docs
else ()
)

openapi_config: typing.Final = openapi.OpenAPIConfig(
path=self.instrument_config.swagger_path,
title=self.instrument_config.service_name,
version=self.instrument_config.service_version,
description=self.instrument_config.service_description,
openapi_controller=LitestarOpenAPIController,
render_plugins=render_plugins,
**self.instrument_config.swagger_extra_params,
)

bootstrap_result = {}
bootstrap_result: typing.Final = {}
if self.instrument_config.swagger_offline_docs:
bootstrap_result["static_files_config"] = [
generate_static_files_config(static_files_handler_path=self.instrument_config.service_static_path),
Expand Down

0 comments on commit 0a5693a

Please sign in to comment.