python - how to create a host field in the swagger.json that is generated via flask restplus with blueprint
问题描述
I created a flask app as below. In local, I can see the Swagger UI; however, when deployed to a server, the Swagger UI is not available (kept getting an error "SCRIPT5009: SCRIPT5009: 'SwaggerUIBundle' is not defined"). It seems like the swagger is looking for files at the root URL level (local host or server, not at the url_prefix level) even though the Swagger.json is at the url_prefix level.
app = Flask(__name__)
blueprint = Blueprint('queue', __name__, url_prefix='/apipath/api/v1'
authorizations = {
'apikey': {
'type': 'apiKey',
'in': 'header',
'name': 'X-Api-Key'
}
}
api = Api(blueprint, version='1.0.0', title='title', description='description', contact='email', authorizations=authorizations, doc='/')
app.register_blueprint(blueprint)
ns = api.namespace('queue', description='description')
So, I decide to use the Swagger.json that is generated. However, the Swagger.json doesn't have the host field, so that the onboarded Swagger UI doesn't work (meaning I can't do 'Try it out') - shown below.
swagger "2.0"
basePath "/apipath/api/v1"
paths
/queueprediction
post
responses
200
解决方案
我想出了如何在 Swagger.json 中包含主机字段。
通过设置 app.config['SERVER_NAME'] = 'some host' 而不是在 app.run 语句中设置 'host' 或 'port' 就可以了。
谢谢!
推荐阅读
- python - 获取vlc中的输出设备名称
- javascript - 神秘的 widgets.map 文件
- javascript - 缺少或权限不足的火库,尽管它适用于规则游乐场
- sqlplus - SQL*PLUS 如何在字符串中放置参数
- shell - 为什么从 Makefile 调用时 printf 的行为会有所不同?
- angular - Angular 12 会支持 @ng-toolkit/universal 并允许无服务器部署吗?
- javascript - 如何加入对象js中的所有元素
- flutter - 在 Flutter 中从地图中获取数据
- javascript - 在反应中使用 setState 钩子更改嵌套对象的值
- docker - 无法从 linux 容器(docker-desktop)连接到 localhost windows 上的服务