Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best way to tell swaggerui where the host is

Tags:

swagger-ui

When I build my swagger.json file I do not know which host to use. However I can work it out when my page that hosts swaggerui loads (in fact I might want to offer the user a choice). I hoped to see an options.host on the config for the swaggerUI object - I dont see one. Is there an existing way of doing this that I cant find or do I simply have to hack my way through the code and add this capability (pointers to the best place to do it would be welcome)

like image 727
pm100 Avatar asked Jun 19 '15 00:06

pm100


2 Answers

Swagger has a built-in json definition for host config, or can accept multiple inputs.

{
    "swagger": "2.0",
    "info": {
        "title": "Why API",
        "description": "Don't make that mistake again",
        "version": "0.0.1"
    },

    "host": "127.0.0.1:3000",
    "schemes": [
        "https"
    ]
}

Or

"host": "test.mydomain.com:3000",
"schemes": [
    "https"
],

Or you can have a dynamic host by defining a var and calling a hostname or machine name or other environment variables.

dynamic example

if (typeof this.host === 'undefined' || this.host === '') {
  this.host = location.host;
}
if (location.port) {
  this.host = this.host + ':' + location.port;
}
like image 53
Fitch Avatar answered Oct 18 '22 12:10

Fitch


Here is what I do, since the loaded in document is just a JSON object:

var swaggerDoc = require('./api/swagger.json');
if (process.env.NODE_ENV === 'development') {
  swaggerDoc.host="localhost:" + process.env.PORT
}

// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
  // Other initialization
}

This way you don't pollute your API specification with development environment configuration.

like image 23
Chris Crewdson Avatar answered Oct 18 '22 14:10

Chris Crewdson