Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup swagger UI within main app, but consider indexing sub app as well #42

Open
webknjaz opened this issue Jan 12, 2018 · 0 comments
Open

Comments

@webknjaz
Copy link

This flow is terribly broken currently. It's partially affected by aio-libs/aiohttp#2656.
But also it becomes messy in terms of setup sequence.

For now I won't suggest a long-term solution (till upstream issue fixed), but here's what I had to hack to get this working:

"""The app configuration module."""
import logging

from aiohttp.web import Application
from aiohttp_swagger import setup_swagger

from .views import APIView

_logger = logging.getLogger(__name__)


async def build_application():
    """Create and pre-populate an aiohttp.web.Application instance."""
    app = Application()
    _logger.info('App initialized.')

    api_app = Application()
    _logger.info('API App initialized.')

    api_app.router.add_route('POST', r'/ping-cad/', APIView)
    _logger.info('Routes configured.')

    # Achtung! Keep add_subapp after routes declaration and
    # before swagger setup. Otherwise things will get fragile.
    # Affected by https://github.com/aio-libs/aiohttp/issues/2656
    app.add_subapp('/api/v1', api_app)
    _logger.info('API App mounted within main App.')

    # Accepts also `swagger_url='/api/v1/doc'`, defaults to '/api/doc':
    # we set api_base_url='/' because setting up within main App,
    # but referring API App, which after .add_subapp() has routes prepended
    # with /api/v1
    # We have to mount it before configuring swagger to make it recognise API.
    setup_swagger(app, api_base_url='/', swagger_url='/api/doc')
    _logger.info('Swagger Web UI configured.')

    return app

Note api_base_url reset to /, setup_swagger called with app, add_subapp() done before swagger setup, routes done before add_subapp()

Why I needed to setup it on the main app?

It's just that I've got a middleware processing error responses and converting them into JSON. And I didn't want that middleware to affect queries to swagger app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant