Skip to content

Conversation

@mrparkers
Copy link
Contributor

Description

This change allows the user to pass a BASE_URL environment variable when running the docker container in order to add a URL prefix to the UI.

Motivation and Context

This change is needed if you need to run Swagger UI behind some reverse proxy that does path re-writes. This will enable you to add a URL prefix (such as /swagger) to the UI and all of the CSS and JS files it needs, allowing you to use the UI at a new path instead of the root directory.

How Has This Been Tested?

Build a local docker image from this branch:

docker build . --tag swagger-ui:latest

Run the container normally (without the new environment variable) to make sure it is backward compatible:

docker run -p 80:8080 -e SWAGGER_JSON=/swagger/schema.json -v $(pwd)/schema:/swagger swagger-ui:latest

Visiting http://localhost:80 confirms that it still works.

Run the container with a new base url:

docker run -p 80:8080 -e BASE_URL=/swagger -e SWAGGER_JSON=/swagger/schema.json -v $(pwd)/schema:/swagger swagger-ui:latest

Visit http://localhost:80/swagger to see that it still works with the new URL.

Types of changes

  • No code changes (changes to documentation, CI, metadata, etc)
  • Dependency changes (any modification to dependencies in package.json)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@mrparkers
Copy link
Contributor Author

Replaces #3054

NGINX_WITH_BASE_URL="${NGINX_ROOT}${BASE_URL}"

mkdir -p ${NGINX_WITH_BASE_URL}
mv ${NGINX_ROOT}/*.* ${NGINX_WITH_BASE_URL}/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this behave when the container is run multiple times? I think it would nest the folder each time, i.e. /swagger then /swagger/swagger and so on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The state of the container is reset when you kill it, so each time you run the container, this script runs against a fresh file system, with all of the UI files in /usr/share/nginx/html.

I verified this locally by running it multiple times with the same base url, and a few times with the url switched back and forth to something else. Each time it worked as expected.

@shockey shockey added this to the December 1, 2017 milestone Nov 29, 2017
Copy link
Contributor

@shockey shockey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved.

Thanks for the detail in your PR description, it was helpful 😄

@shockey shockey merged commit 8f73519 into swagger-api:master Dec 2, 2017
@shockey
Copy link
Contributor

shockey commented Dec 2, 2017

Not sure why I didn't hit merge on Wednesday...

Anyway, thanks for the contribution @mrparkers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants