swagger-api / swagger-play

Apache License 2.0
330 stars 181 forks source link

How to avoid declaring a fixed swagger.api.basepath? #170

Open robertofabrizi opened 6 years ago

robertofabrizi commented 6 years ago

Dear friends, I'm currently running a Play Swagger container in a highly dynamic environment, therefore I cannot commit to one hostname/port.

How can I avoid having to set a fixed basepath?

For example, if my application is running on the host testhost1:8787, then the Request URL is simply http://testhost1:8787/my/restful/api

Thank you for your help, Best regards, Roberto

mrwillis commented 6 years ago

You could try to use an environment variable like ${VARIABLE} in your application.conf file. So like this:

swagger.api.basepath = "${VARIABLE}"

fcappi commented 6 years ago

Hi @robertofabrizi ,

My current workaround for this is to use request interceptor on swagger UI config:

swagger.html

requestInterceptor: function(request) {
     // Build a system
      const ui = SwaggerUIBundle({
        url: specUrl,
        requestInterceptor: function(request) {
          // Replace the default host by the correct one.
          // DO NOT CHANGE here without changing application.conf
          request.url = request.url.replace("HOST_TO_REPLACE", host);
          return request;
        },
        dom_id: '#swagger-ui',
        deepLinking: true,
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIStandalonePreset.slice(1)
        ],
        plugins: [
          SwaggerUIBundle.plugins.DownloadUrl
        ],
        layout: "StandaloneLayout",
        validatorUrl: ""
      })
{code}

application.conf

# This value will be replaced on swagger page by the correct hostname.
# DO NOT CHANGE here without changing swagger.html
swagger.api.host = "HOST_TO_REPLACE"

This way it will work without any host configuration.