aiohttp-apispec icon indicating copy to clipboard operation
aiohttp-apispec copied to clipboard

Failed to access static files with subapps.

Open momocow opened this issue 4 years ago • 0 comments

Hello, I have set up the apispec and visited the web page with browser and I found that all swagger static files are 404 not found.

For example, I have set up the apispec like the following snippet, where app is a subapp which is registered to the main app with main_app.add_subapp("/v2/", app).

setup_aiohttp_apispec(app=app,  # app is an instance of subapp
                      title="My Documentation",
                      version="v2",
                      url="/docs/swagger.json",
                      static_path="/static/swagger",
                      swagger_path="/docs",)

Since the subapp is registered under /v2/, the web page can be visited at http://example.com/v2/docs with swagger_path set to /docs.

But all urls in static files are rendered with absolute path, /static/swagger, which caused 404 since all static files are registered to the subapp, app, under /v2/static/swagger.

https://github.com/maximdanilchenko/aiohttp-apispec/blob/cf8a9210760e93b867b6c5e66effb4ab074d21d7/aiohttp_apispec/aiohttp_apispec.py#L96

where app is served as the subapp, /v2/.

Maybe it's better to separate the static path used in the template from the one registered to the app?

That is,

setup_aiohttp_apispec(app=app,  # app is an instance of subapp
                      title="My Documentation",
                      version="v2",
                      url="/docs/swagger.json",
                      static_base_url="/v2/static/swagger",  # used in the template
                      static_path="/static/swagger",  # used to register the static resource
                      swagger_path="/docs",)

momocow avatar Mar 06 '20 12:03 momocow