1- import os
2- import pathlib
31import typing
42
53from lite_bootstrap import import_checker
1513
1614def enable_offline_docs (
1715 app : "FastAPI" ,
18- static_files_handler : str = "/static" ,
19- static_dir_path : os .PathLike [str ] = pathlib .Path (__file__ ).parent / "static" ,
20- include_docs_in_schema : bool = False ,
16+ static_path : str ,
2117) -> None :
2218 if not (app_openapi_url := app .openapi_url ):
2319 msg = "No app.openapi_url specified"
@@ -33,29 +29,27 @@ def enable_offline_docs(
3329 if typing .cast (Route , route ).path not in (docs_url , redoc_url , swagger_ui_oauth2_redirect_url )
3430 ]
3531
36- app .mount (static_files_handler , StaticFiles (directory = static_dir_path ), name = static_files_handler )
32+ app .mount (static_path , StaticFiles (directory = "lite_bootstrap/static/fastapi_docs" ), name = "static" )
3733
38- @app .get (docs_url , include_in_schema = include_docs_in_schema )
34+ @app .get (docs_url , include_in_schema = False )
3935 async def custom_swagger_ui_html (request : Request ) -> HTMLResponse :
40- root_path = typing .cast (str , request .scope .get ("root_path" , "" ).rstrip ("/" ))
41- swagger_js_url = f"{ root_path } { static_files_handler } /swagger-ui-bundle.js"
42- swagger_css_url = f"{ root_path } { static_files_handler } /swagger-ui.css"
36+ root_path = request .scope .get ("root_path" , "" ).rstrip ("/" )
4337 return get_swagger_ui_html (
44- openapi_url = root_path + app_openapi_url ,
38+ openapi_url = f" { root_path } { app_openapi_url } " ,
4539 title = f"{ app .title } - Swagger UI" ,
4640 oauth2_redirect_url = app .swagger_ui_oauth2_redirect_url ,
47- swagger_js_url = swagger_js_url ,
48- swagger_css_url = swagger_css_url ,
41+ swagger_js_url = f" { root_path } { static_path } /swagger-ui-bundle.js" ,
42+ swagger_css_url = f" { root_path } { static_path } /swagger-ui.css" ,
4943 )
5044
51- @app .get (swagger_ui_oauth2_redirect_url , include_in_schema = include_docs_in_schema )
45+ @app .get (swagger_ui_oauth2_redirect_url , include_in_schema = False )
5246 async def swagger_ui_redirect () -> HTMLResponse :
5347 return get_swagger_ui_oauth2_redirect_html ()
5448
55- @app .get (redoc_url , include_in_schema = include_docs_in_schema )
49+ @app .get (redoc_url , include_in_schema = False )
5650 async def redoc_html () -> HTMLResponse :
5751 return get_redoc_html (
5852 openapi_url = app_openapi_url ,
5953 title = f"{ app .title } - ReDoc" ,
60- redoc_js_url = f"{ static_files_handler } /redoc.standalone.js" ,
54+ redoc_js_url = f"{ static_path } /redoc.standalone.js" ,
6155 )
0 commit comments