如何修改swagger.json的默认文件路径?

时间:2018-05-19 09:41:53

标签: nginx swagger swagger-ui swagger-2.0 consul

我已经创建了引用https://github.com/nginxinc/NGINX-Demos/tree/master/consul-template-demo的Ngnix-Consul Docker设置。

并创造了许多微服务。因此,只有在添加服务名称后才能访问所有微服务。

http://example.com/service_name/get_data

一切正常,然后我想为所有微服务添加招摇,所以尝试使用下面的代码片段 我可以使用

访问swagger ui
http://example.com/service_name/ui

但问题是我无法在ui中加载swagger.json,因为它试图在下面的url上访问swagger.json

http://example.com/swagger.json

但是json文件在

http://example.com/service_name/swagger.json    

如何更改swagger.json的默认路径?

微服务中的应用程序是在python-flask中创建的     我试过下面的片段

swagger: "2.0"
    info:
      description: "Add service"
      version: "1.0.0"
      title: "Add Service"
      contact:
        email: "abc@efg.com"
      license:
        name: "s1.0"
        url: "http://sample.com"
    host: "abc.efg.com"
    tags:
    - name: "add service"
      description: "service"
    - name: "delete service"
      description: "data"
    schemes:
    - "http"
    paths:
      /service_name/get_data:

甚至我试图在swagger.yaml文件中添加basePath 然后它甚至没有打开swaggerui

swagger: "2.0"
    info:
      description: "Add service"
      version: "1.0.0"
      title: "Add Service"
      contact:
        email: "abc@efg.com"
      license:
        name: "s1.0"
        url: "http://sample.com"
    host: "abc.efg.com"
    basePath: "service_name"
    tags:
    - name: "add service"
      description: "service"
    - name: "delete service"
      description: "data"
    schemes:
    - "http"
    paths:
      /get_data:

更新

from flask import Flask
import connexion

app = Flask(__name__)
app = connexion.App(__name__)
app.add_api('swagger.yaml')

//apis

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8090, debug=True)

2 个答案:

答案 0 :(得分:0)

如果文件swagger.json在NGINX配置中是静态的,你可以像这样建立别名规则:

location ^~ /swagger.json {
    alias /path_to/swagger.json;
}

答案 1 :(得分:0)

我自己也遇到过类似的问题。对我来说,解决方案是在NGINX级别禁用路径重写,以便微服务将接收完整的URL:

之前:

Request: 
http://example.com/service_name/get_data

Service sees:
/get_data

之后:

Request: 
http://example.com/service_name/get_data

Service sees:
/service_name/get_data

只有这样,您才能在swagger.yaml文件中将basePath指定为“ service_name ”:

swagger: "2.0"
    info:
      description: "Add service"
      version: "1.0.0"
      title: "Add Service"
    host: "abc.efg.com"
    basePath: "service_name"