Search code examples
pythonswagger-uiflask-restplus

How to set base url for swagger with flask_restplus?


I'm serving a flask app with flask_restplus from an apache server. I'm using ProxyPass to forward all traffic to the app at some url extension like so in the apache .conf file:

ProxyPass /some-extension http://127.0.0.1:3000
ProxyPassReverse /some-extension http://127.0.0.1:3000

The flask_restplus api is set up as follows:

from flask_restplus import Api
api = Api(
    title='App Title',
    doc='/docs'
) 

The app works ok except that when I go to the swagger route /some-extension/docs the flask server starts looking for swagger dependencies at the url base /swaggerui/ (rather than the required /some-extension/swaggerui), and so the swagger UI fails to load.

Is there a way to configure flask_restplus (or otherwise) so that swagger gets served /some-extension/swaggerui/ (rather than the root url)?


Solution

  • OK, got there after some fiddling. You need to proxy traffic on to the extension and set up a flask with a blueprint so the whole app also runs from that extension. So something like this in your apache config:

    ProxyPass /some-extension http://127.0.0.1:3000/some-extension
    ProxyPassReverse /some-extension http://127.0.0.1:3000/some-extension
    

    ...and you have to ProxyPass the extension /swaggerui like so in your apache configuration:

    ProxyPass /swaggerui http://127.0.0.1:3000/swaggerui
    ProxyPassReverse /swaggerui http://127.0.0.1:3000/swaggerui
    

    ...and then use this pattern in your flask_restplus set up (from official guide):

    from flask import Flask, Blueprint
    from flask_restplus import Api
    
    app = Flask(__name__)
    blueprint = Blueprint('api', __name__, url_prefix='/some-extension')
    api = Api(blueprint, doc='/docs/')
    app.register_blueprint(blueprint)