Monday, 20 June 2022

Can't serve swagger on remotely hosted server with different url

I'm trying to serve some simple service using flask and flask_restx (a forked project of flask-restplus, that would be eventually served on AWS.

When it is served, I want to generate swagger page for others to test it easily.

from flask import Flask
from flask_restx import Api
from my_service import service_namespace

app = Flask(__name__)
api = Api(app, version='1.0')
api.add_namespace(service_namespace)

if __name__ == '__main__':

    app.run(debug=True)

When I test it locally (e.g. localhost:5000), it works just fine. Problem is, when it is hosted on AWS, because it has a specific domain (gets redirected?) (e.g. my-company.com/chris-service to a container), the document page is unable to find its required files like css and so:

enter image description here

What I've looked and tried

Python (Flask + Swagger) Flasgger throwing 404 error

flask python creating swagger document error

404 error in Flask

Also tried adding Blueprint (albeit without knowing exactly what it does):

app = Flask(__name__)
blueprint = Blueprint("api", __name__, 
                      root_path="/chris-service",
                      # url_prefix="/chris-service", # doesn't work
)
api = Api(blueprint)
app.register_blueprint(blueprint)
...

And still no luck.

Update

So here's more information as per the comments (pseudo, but technically identical)

  1. Access point for the swagger is my-company.com/chris (with or without http:// or https:// doesn't make difference)
  2. When connecting to the above address, the request URL for the assets are my-company.com/swaggerui/swagger-ui.css
  3. You can access the asset in my-company.com/chris/swaggerui/swagger-ui.css
  4. So I my resolution (which didn't work) was to somehow change the root_path (not even sure if it's the correct wording), as shown in What I've looked and tried.

I've spent about a week to solve this but can't find a way.

Any help will be greatful :) Thanks



from Can't serve swagger on remotely hosted server with different url

No comments:

Post a Comment