swagger-play icon indicating copy to clipboard operation
swagger-play copied to clipboard

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

Open robertofabrizi opened this issue 7 years ago • 2 comments

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

robertofabrizi avatar Feb 21 '18 14:02 robertofabrizi

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

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

mrwillis avatar Apr 28 '18 19:04 mrwillis

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.

fcappi avatar Apr 30 '18 12:04 fcappi