可以在带有不同URL的远程托管服务器上发挥作用

发布于 2025-02-06 16:05:46 字数 1992 浏览 1 评论 0 原文

我正在尝试使用 flask flask_restx flask-restplus 的分叉项目,最终将在AWS上使用, 它

当它提供时,我想在当地测试其他人(例如 localhost:5000 )时,就可以轻松地测试

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)

。在AWS上,因为它具有特定的域(重定向?)(例如,到一个容器,例如 my-company.com/chris-service ),文档页面无法找到所需的文件,例如 css 等等:

”在此处输入映像说明“

我看的东西

python(烧瓶 + Swagger)Flasgger投掷404错误

404烧瓶中的错误

也尝试添加 BluePrint (尽管不知道它做什么):

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)
...

仍然没有运气。

更新

因此,以下是根据评论(伪造但在技术上相同)

  1. 的招标点的更多信息, my-company.com/chris (有或没有http:// http:// http:// http:// https:// https:// 't有所不同)
  2. 连接到上述地址时,资产的请求URL为 my-company.com/swaggerui/swaggerui/swagger-ui.css
  3. can can can 访问 my-company.com/chris/swaggerui/swagger-ui.css 中的资产,
  4. 所以我的解决方案(无效)是某种程度上更改 root_path (不是即使是正确的措辞),如我所示和尝试的所示。

我花了大约一个星期的时间来解决这个问题,但找不到方法。

任何帮助都会很奇怪:)谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

披肩女神 2025-02-13 16:05:46

apidoc.py 文件。默认 apidoc 在此文件中也创建了对象。因此,如果要自定义它,则在 app api 初始化之前将其更改。

在您的情况下,应该更改 url_prefix (我建议使用环境变量能够设置 url_prefix 灵活地):

$ export URL_PREFIX='/chris'
from os import environ
from flask import Flask
from flask_restx import Api, apidoc

if (url_prefix := environ.get('URL_PREFIX', None)) is not None:
    apidoc.apidoc.url_prefix = url_prefix

app = Flask(__name__)
api = Api(app)

...

if __name__ == '__main__':
    app.run()

Swagger parameters defined at apidoc.py file. Default apidoc object also created in this file. So if you want to customize it you have change it before app and api initialization.

In your case url_prefix should be changed (I recommend to use environment variables to be able set url_prefix flexibly):

$ export URL_PREFIX='/chris'
from os import environ
from flask import Flask
from flask_restx import Api, apidoc

if (url_prefix := environ.get('URL_PREFIX', None)) is not None:
    apidoc.apidoc.url_prefix = url_prefix

app = Flask(__name__)
api = Api(app)

...

if __name__ == '__main__':
    app.run()
平定天下 2025-02-13 16:05:46

当东西在本地工作时,总是非常令人沮丧,但在部署到AWS时不会很沮丧。读取 this github问题,这些404个关于摇摇欲坠资产的错误可能是造成的。作者:

  1. 缺少JavaScript Swagger软件包

,因为Flask-Restx为您做到这一点。在这种情况下,本地运行它也不应工作。

  1. 缺少枪支设置

确保您也可以正确设置枪支
- 前向允许-IPS如果与之部署(应该是)。如果您在Kubernetes群集中,则可以将其设置为 *

https://docs.gunicorn.org/en.org/en /stable/settings.html#forwarded-allow-ips

  1. 根据 this 帖子,您还必须明确设置
    settings.flask_server_name to http://ec2-10-221-200-56.us-west-2.compute.amazonaws.com:5000

  2. 如果不起作用,请尝试部署flask-restx 示例 ,这应该肯定有效。这排除了您的尽头。

Always very frustrating when stuff is working locally but not when deployed to AWS. Reading this github issue, these 404 errors on swagger assets are probably caused by:

  1. Missing javascript swagger packages

Probably not the case, since flask-restx does this for you. And running it locally should also not work in this case.

  1. Missing gunicorn settings

Make sure that you are also setting gunicorn up correctly as well with
--forwarded-allow-ips if deploying with it (you should be). If you are in a kubernetes cluster you can set this to *

https://docs.gunicorn.org/en/stable/settings.html#forwarded-allow-ips

  1. According to this post, you also have to explicitly set
    settings.FLASK_SERVER_NAME to something like http://ec2-10-221-200-56.us-west-2.compute.amazonaws.com:5000

  2. If that does not work, try to deploy a flask-restx example, that should definetely work. This rules out any errors on your end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文