【问题标题】:Flasgger / Swagger - apidocs works with localhost but not Openshift (You need to enable JavaScript to run this app)Flasgger / Swagger - apidocs 适用于 localhost 但不适用于 Openshift(您需要启用 JavaScript 才能运行此应用程序)
【发布时间】:2021-10-21 05:07:34
【问题描述】:

我正在运行 Python Flask 服务器。一位同事添加了 Flasgger/Swagger 支持,我可以使用

成功显示 API
http://localhost:5000/apidocs

同样,我可以得到json版本的API

http://localhost:5000/api_documentation.json
  Python code configures that filename

同样的代码部署在 Openshift 项目中,并使用 Traefik 将外部请求路由到 Python Flask 服务器

https://my-openshift-url-here/apidocs
  does not display the API
  only displays "Powered by Flasgger 0.9.5
https://my-openshift-url-here/api_documentation.json
  works same as the localhost request

“apidocs”和“api_documentation.json”的 Traefik 键,并将其直接路由到 Python Flask 服务器

        rule: PathPrefix(`/apidocs`)
        entryPoints:
          - web
        middlewares:
          - gzip
          - mysecurity-no-token
        service: my-python-server

      apidocumentation:
        rule: PathPrefix(`/api_documentation.json`)
        entryPoints:
          - web
        middlewares:
          - gzip
          - mysecurity-no-token
        service: my-python-server

我在 Chrome 浏览器调试器(F12 - 网络)中看到的 swagger-ui-bundle.js 响应是“您需要启用 JavaScript 才能运行此应用程序。”

为什么这在 Chrome 的 localhost 版本中有效,但在访问部署在 Openshift 上的服务器时却无效?两者都是从同一个 Chrome 窗口访问的 - 只是不同的标签。

这是 localhost 版本的 apidocs 请求的 Headers 内容

    Request Method: GET
    Status Code: 200 OK
    Remote Address: 127.0.0.1:5000
    Referrer Policy: strict-origin-when-cross-origin
    Content-Length: 3041
    Content-Type: text/html; charset=utf-8
    Date: Thu, 19 Aug 2021 12:08:41 GMT
    Server: Werkzeug/2.0.1 Python/3.7.4
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
    Accept-Encoding: gzip, deflate, br
    Accept-Language: en-US,en;q=0.9
    Cache-Control: no-cache
    Connection: keep-alive
    Host: localhost:5000
    Pragma: no-cache
    sec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"
    sec-ch-ua-mobile: ?0
    Sec-Fetch-Dest: document
    Sec-Fetch-Mode: navigate
    Sec-Fetch-Site: none
    Sec-Fetch-User: ?1
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

这是 Openshift 版本的 apidocs 请求的 Headers 内容

    Request Method: GET
    Status Code: 200 OK
    Remote Address: 123.456.789.123:443 (obfuscated, of course)
    Referrer Policy: origin
    content-encoding: gzip
    content-length: 1264
    content-type: text/html; charset=utf-8
    date: Thu, 19 Aug 2021 12:08:59 GMT
    server: istio-envoy
    vary: Accept-Encoding
    x-envoy-upstream-service-time: 28
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
    Accept-Encoding: gzip, deflate, br
    Accept-Language: en-US,en;q=0.9
    Cache-Control: no-cache
    Connection: keep-alive
    Host: my-openshift-server-url-here
    Pragma: no-cache
    sec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"
    sec-ch-ua-mobile: ?0
    Sec-Fetch-Dest: document
    Sec-Fetch-Mode: navigate
    Sec-Fetch-Site: none
    Sec-Fetch-User: ?1
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

【问题讨论】:

    标签: javascript swagger openshift traefik flasgger


    【解决方案1】:

    原来是 Traefik 路由问题。在对 /apidocs 请求的初始响应之后,它也在向 /flasgger_static/foo 发出请求。我必须在我的 Traefik 路由表中为flassgger_static 添加一个路由。

    flassger:
      rule: PathPrefix(`/flasgger_static`)
      entryPoints:
        - web
      middlewares:
        - gzip
        - mysecurity-no-token
      service: my-python-server
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-16
      • 2020-05-09
      • 2022-11-30
      • 2022-01-25
      • 1970-01-01
      • 2013-03-26
      • 2021-10-05
      • 1970-01-01
      相关资源
      最近更新 更多