Skip to content

Commit

Permalink
add aiohttp trailing-slash-redirect middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
dtkav committed Jan 24, 2019
1 parent 2873605 commit 43c2888
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions connexion/apis/aiohttp_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import aiohttp_jinja2
from aiohttp import web
from aiohttp.web_exceptions import HTTPNotFound
from aiohttp.web_middlewares import normalize_path_middleware
from connexion.apis.abstract import AbstractAPI
from connexion.exceptions import OAuthProblem, OAuthScopeProblem
from connexion.handlers import AuthErrorHandler
Expand Down Expand Up @@ -43,7 +44,10 @@ class AioHttpApi(AbstractAPI):
def __init__(self, *args, **kwargs):
self.subapp = web.Application(
debug=kwargs.get('debug', False),
middlewares=[oauth_problem_middleware]
middlewares=[
oauth_problem_middleware,
normalize_path_middleware
]
)
AbstractAPI.__init__(self, *args, **kwargs)

Expand Down Expand Up @@ -129,21 +133,30 @@ def add_swagger_ui(self):
self._get_swagger_ui_home
)

# this route will match and get a permission error when trying to
# serve index.html, so we add the redirect below.
self.subapp.router.add_static(
console_ui_path,
path=str(self.options.openapi_console_ui_from_dir),
name='swagger_ui_static'
)

# we have to add an explicit redirect instead of relying on the
# normalize_path_middleware because we also serve static files
# from this dir (above)

@asyncio.coroutine
def redirect(request):
raise web.HTTPMovedPermanently(location=self.base_path + console_ui_path + '/')
raise web.HTTPMovedPermanently(
location=self.base_path + console_ui_path + '/'
)

self.subapp.router.add_route(
'GET',
console_ui_path,
redirect
)

self.subapp.router.add_static(
console_ui_path + '/',
path=str(self.options.openapi_console_ui_from_dir),
name='swagger_ui_static'
)

@aiohttp_jinja2.template('index.j2')
@asyncio.coroutine
def _get_swagger_ui_home(self, req):
Expand Down

0 comments on commit 43c2888

Please sign in to comment.