我正在尝试使用 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)
...
仍然没有运气。
更新
因此,以下是根据评论(伪造但在技术上相同)
- 的招标点的更多信息,
my-company.com/chris
(有或没有http:// http:// http:// http:// https:// https:// 't有所不同)
- 连接到上述地址时,资产的请求URL为
my-company.com/swaggerui/swaggerui/swagger-ui.css
- 您 can can can 访问
my-company.com/chris/swaggerui/swagger-ui.css
中的资产,
- 所以我的解决方案(无效)是某种程度上更改
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:

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)
- Access point for the swagger is
my-company.com/chris
(with or without http:// or https:// doesn't make difference)
- When connecting to the above address, the request URL for the assets are
my-company.com/swaggerui/swagger-ui.css
- You can access the asset in
my-company.com/chris/swaggerui/swagger-ui.css
- 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
发布评论
评论(2)
在
apidoc.py
文件。默认
apidoc
在此文件中也创建了对象。因此,如果要自定义它,则在app
和api
初始化之前将其更改。在您的情况下,应该更改
url_prefix
(我建议使用环境变量能够设置url_prefix
灵活地):Swagger parameters defined at
apidoc.py
file. Defaultapidoc
object also created in this file. So if you want to customize it you have change it beforeapp
andapi
initialization.In your case
url_prefix
should be changed (I recommend to use environment variables to be able seturl_prefix
flexibly):当东西在本地工作时,总是非常令人沮丧,但在部署到AWS时不会很沮丧。读取 this github问题,这些404个关于摇摇欲坠资产的错误可能是造成的。作者:
,因为Flask-Restx为您做到这一点。在这种情况下,本地运行它也不应工作。
根据 this 帖子,您还必须明确设置
settings.flask_server_name
tohttp://ec2-10-221-200-56.us-west-2.compute.amazonaws.com:5000
如果不起作用,请尝试部署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:
Probably not the case, since flask-restx does this for you. And running it locally should also not work in this case.
According to this post, you also have to explicitly set
settings.FLASK_SERVER_NAME
to something likehttp://ec2-10-221-200-56.us-west-2.compute.amazonaws.com:5000
If that does not work, try to deploy a flask-restx example, that should definetely work. This rules out any errors on your end.