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

Need to Explicitly set Swagger specs_url Scheme When Deployed on Behind AWS Elastic Load Balancer. #237

Closed
DustinMoriarty opened this issue Oct 9, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@DustinMoriarty
Copy link
Contributor

We have found that when we deploy to AWS ECS behind an elastic load balancer, we need to subclass the API class in order to explicitly set the scheme for the URL such that it sets an external URL instead of a relative URL. It would be preferable if this option was built into flask-restx itself.

I plan to submit a PR for this issue.

@rdagumampan
Copy link

I hope your PR solves this with ease... Sharing here my solution.

I solved this for AKS with these hacks. This requires that I explicitly set a APP_BASE_PATH settings variable. This ensures that if theres value, then its running behind an ingress controller with custom host path as base URL in SwaggerUI.

class ReverseProxiedApi(Api):
    # handles dynamic root path when running local and on aks cluster
    @property
    def specs_url(self):
        return url_for(self.endpoint('specs'))

    # handles dynamic root path when running local and on aks cluster
    @property
    def base_path(self):
        base_path = get_value("APP_BASE_PATH") or None
        if base_path:
            return base_path
        else:
            return url_for(self.endpoint("root"), _external=False)

api = ReverseProxiedApi(
    version="1.0",
}

# handles dynamic root path when running local and on aks cluster
@apidoc.apidoc.add_app_template_global
def swagger_static(filename):
    base_path = get_value("APP_BASE_PATH") or None
    if base_path:
        return "{0}/swaggerui/{1}".format(base_path, filename)
    else:
        return url_for("restx_doc.static", filename=filename)


# handles dynamic root path when running local and on aks cluster
@api.documentation
def custom_ui():
    base_path = get_value("APP_BASE_PATH") or None
    if base_path:
        return render_template("swagger-ui.html", title=api.title, specs_url="{}/swagger.json".format(base_path))
    else:
        return render_template("swagger-ui.html", title=api.title, specs_url=api.specs_url)

@DustinMoriarty
Copy link
Contributor Author

DustinMoriarty commented Oct 12, 2020

This is pr #238. I don't think I am able to add this as a linked issue.

@rdagumampan : Let me know if this achieves what you are looking for.

@DustinMoriarty
Copy link
Contributor Author

@rdagumampan : I see your point of providing a more general solution using the base url. I will revise accordingly, though I think I may expose the functionality a little differently to make it more integrated.

@rdagumampan
Copy link

@DustinMoriarty looking forward to it... I just got my hands on Python in last two weeks and lost few hairline on this so If I dont have to hack this way, that would be lovely :D

@j5awry
Copy link
Contributor

j5awry commented Apr 27, 2021

closing with merge of #238

@j5awry j5awry closed this as completed Apr 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants