首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么flasgger使用的是本地主机,而不是我部署的服务器?

为什么flasgger使用的是本地主机,而不是我部署的服务器?
EN

Stack Overflow用户
提问于 2022-01-27 14:00:11
回答 1查看 713关注 0票数 3

我正在使用python烧瓶应用程序和API文档使用flasgger。当我在本地运行我的烧瓶应用程序http://localhost:8086/swagger/时,我得到了正确的用户界面。

现在,我们已经使用Nginx前端服务部署了应用程序。当我们调用已部署的链接http://localhost:8086/api/core/swagger/时,我们会看到一个空白白页,页面右上半部分是Powered by Flasgger 0.9.5

在中,我得到了这个错误。

代码语言:javascript
复制
Uncaught ReferenceError: SwaggerUIBundle is not defined
    at window.onload ((index):72:16)

我知道这可能是flasgger试图加载CSS文件或其他资源时遇到的一些问题,而且它没有正确地呈现。因为一条路

在我对swagger的配置过程中,我对它具有以下配置函数。

代码语言:javascript
复制
def configure_swagger(app):
    

    swagger_config = {
        "headers": [],
        "specs": [
            {
                "endpoint": "apispec_1",
                "route": "apispec_1.json",
                "rule_filter": lambda rule: True,  # all in
                "model_filter": lambda tag: True,  # all in
            }
        ],
        "static_url_path": "/flasgger_static",
        "swagger_ui": True,
        "specs_route": "/swagger/",
        'openapi': '3.0.3'
    }
 
    swagger_template = {
        'components': {
            'securitySchemes': {
                'bearerAuth': {
                    'type': 'http',
                    'scheme': 'bearer',
                    'bearerFormat': 'JWT'
                }
            },
            'security': [{
                'bearerAuth': []
            }]
        }
    }
    swagger = Swagger(app, config=swagger_config, template=swagger_template)

api.py中的所有其他端点都遵循这个体系结构。

代码语言:javascript
复制
@api.route("/hello")
@swag_from("Swagger/hello.yml")
# @log_writer
def hello():
    projects = {"id": 1, "title": "Hello"}
    return projects["title"]


@api.route("/healtz")
@swag_from("Swagger/healtz.yml")
def healtz():
    return_dict = {"App Version": "1.0.0", "Status": "Healthy"}
    return return_dict

我的healtz.yaml文件示例:

代码语言:javascript
复制
summary: "Upload"
description: "Give the info in json"
consumes:
- "application/json"
produces:
- "application/json"
responses:
  405:
    description: "Invalid input"
security:
- bearerAuth: []

该YAML文件位于项目中,其中有一个名为Swagger的目录。

我已经做了以下的回答,但没有得到答案。

对于部署,我们使用EKS服务,在这里我们有我们的容器。

为什么会发生这个问题?有人能帮我处理密码吗?

EN

回答 1

Stack Overflow用户

发布于 2022-03-28 22:32:37

之所以会发生这种情况,是因为当NGINX将URL转发到您的Flask应用程序时,您的URL正在被NGINX重写。因此,当您的浏览器访问http://your-domain/api/core/swagger/时,NGINX将转发到您的Flask应用程序作为http://flask-app/swagger/。尽管如此,Flasgger并不知道这一点,而且在它的代码中,它将URL硬编码为http://your-domain/swagger/,这显然不存在,因为NGINX不将/swagger路由到您的/swagger应用程序。

一个潜在的解决方案可以看到这里。您不用查询Swagger库的本地版本,而是使用外部CDN托管的JS文件来加载swagger网页。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70879832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档