Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change default swagger.json file path?

I have created Ngnix-Consul Docker setup referred https://github.com/nginxinc/NGINX-Demos/tree/master/consul-template-demo.

And have created many microservices. So All the microservices are accessible only after adding the service name for e.g.

http://example.com/service_name/get_data

All is working fine then I wanted to add swagger for all microservices so tried with below snippet I am able to access swagger ui by using

http://example.com/service_name/ui

But the problem is I am not able to load swagger.json in that ui as its trying to access swagger.json on below url

http://example.com/swagger.json

but the json file is on

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

How can I change the default path of swagger.json?

The applications in microservices are created in python-flask I have tried below snippet

swagger: "2.0"
    info:
      description: "Add service"
      version: "1.0.0"
      title: "Add Service"
      contact:
        email: "[email protected]"
      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:

and even I have tried to add basePath in the swagger.yaml file then It did not even open swaggerui

swagger: "2.0"
    info:
      description: "Add service"
      version: "1.0.0"
      title: "Add Service"
      contact:
        email: "[email protected]"
      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:

Update:

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)
like image 925
happy Avatar asked Nov 08 '22 06:11

happy


1 Answers

Had similar problem myself. The solution for me was to disable path rewrite in the NGINX level, so that the microservice would receive the full url:

Before:

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

Service sees:
/get_data

After:

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

Service sees:
/service_name/get_data

Only then you can specify basePath as "service_name" in the swagger.yaml file:

swagger: "2.0"
    info:
      description: "Add service"
      version: "1.0.0"
      title: "Add Service"
    host: "abc.efg.com"
    basePath: "service_name"
like image 148
GoKarl Avatar answered Nov 16 '22 16:11

GoKarl