0

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

http://localhost:5000/apidocs

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

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

Traefik 关闭“apidocs”和“api_documentation.json”并将其直接路由到 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 才能运行此应用程序”。

为什么这在localhost版本的 Chrome 中有效,但在访问部署在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
4

1 回答 1

0

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

flassger:
  rule: PathPrefix(`/flasgger_static`)
  entryPoints:
    - web
  middlewares:
    - gzip
    - mysecurity-no-token
  service: my-python-server
于 2021-08-19T15:30:55.963 回答