To replace the demo URL, we are going to use a Bash script that will use environment variables to compose the URL of our openapi.yaml, and then substitute it in using sed. However, the SERVER_* environment variables we have set are internal, and won't be valid for our clients. Therefore, we need to add three more environment variables to hold the external URL of our API server.
In envs/.env and envs/.env.example, add the following three environment variables:
SERVER_EXTERNAL_PROTOCOL=http
SERVER_EXTERNAL_HOSTNAME=api.hobnob.jenkins
SERVER_EXTERNAL_PORT=80
Then, create a new file at scripts/swagger-ui/format.sh with execute permissions and paste in the following script:
#!/usr/bin/env bash
sed -i "s!https://petstore.swagger.io/v2/swagger.json!$SERVER_EXTERNAL_PROTOCOL://$SERVER_EXTERNAL_HOSTNAME:$SERVER_EXTERNAL_PORT/openapi.yaml!g" docs/dist/index.html
Then, also add a new NPM script to call our the format.sh script:
"docs:format": "dotenv -e envs/.env ./scripts/swagger-ui/format.sh",
We must also update our docs:update script in order to:
- Reset any changes made in the Git submodules.
- Pull the latest Swagger UI repository.
- Run docs:format to replace the URL:
"docs:update": "git submodule foreach --recursive git reset --hard && git submodule update --init --recursive && yarn run docs:format",
Now, run yarn run docs:update and then reload our Swagger UI page, it'll default to using our API specification instead of the demo specification.