1
- import os
2
- import pathlib
3
1
import typing
4
2
5
3
from lite_bootstrap import import_checker
15
13
16
14
def enable_offline_docs (
17
15
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 ,
21
17
) -> None :
22
18
if not (app_openapi_url := app .openapi_url ):
23
19
msg = "No app.openapi_url specified"
@@ -33,29 +29,27 @@ def enable_offline_docs(
33
29
if typing .cast (Route , route ).path not in (docs_url , redoc_url , swagger_ui_oauth2_redirect_url )
34
30
]
35
31
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" )
37
33
38
- @app .get (docs_url , include_in_schema = include_docs_in_schema )
34
+ @app .get (docs_url , include_in_schema = False )
39
35
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 ("/" )
43
37
return get_swagger_ui_html (
44
- openapi_url = root_path + app_openapi_url ,
38
+ openapi_url = f" { root_path } { app_openapi_url } " ,
45
39
title = f"{ app .title } - Swagger UI" ,
46
40
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" ,
49
43
)
50
44
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 )
52
46
async def swagger_ui_redirect () -> HTMLResponse :
53
47
return get_swagger_ui_oauth2_redirect_html ()
54
48
55
- @app .get (redoc_url , include_in_schema = include_docs_in_schema )
49
+ @app .get (redoc_url , include_in_schema = False )
56
50
async def redoc_html () -> HTMLResponse :
57
51
return get_redoc_html (
58
52
openapi_url = app_openapi_url ,
59
53
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" ,
61
55
)
0 commit comments