Host swagger API documentation on my own server
I have created two pages which is available publicly. I want to host those pages on my own server.
I saw the documentation and github for swagger-ui and swagger-editor
I installed both of them on my local server and it is pointing to default petstore and when I make changes to it and it saves to my local browser only (seems it has local storage only), not server side (on my server).
[Edited. Originally required hosted JSON spec. Now uses simpler local file approach.]
So, I found that the docker approach was the easiest way to go. From what I've read, there are many different ways to get your API to show in a Swagger UI on your own server but I could never find one place with all the steps. I've pieced together ONE way to do it that worked for me. (I should note that I'm really only using Swagger for its documentation capabilities as I already have an API and a testing framework.)
Here is a complete set of instructions:
- Edit your API in SwaggerHub. Make that your canonical source of truth.
- Export a JSON file of your API. This file should get checked in with your own server's code base. (Remember, I'm only using this for documentation -- I won't be generating any real APIs from this.) Let's call this file api.json. Keept it up to date as you would any documentation.
- Install docker. I got the docker community edition (https://www.docker.com/get-docker). It's free. The process will require you to register. You will need to run this in your server environment.
- Install the docker swagger-ui image using the following command:
docker pull swaggerapi/swagger-ui
- Now you can run the Swagger UI with your specification using docker:
docker run -p 80:8080 -e "SWAGGER_JSON=/api.json" -v /actual/path/to/api.json:/api.json swaggerapi/swagger-ui
- You should now be able to hit port 80 wherever docker is running and see your API spec.
Note, for those of us unfamiliar with docker, the -v option can be confusing and the notes in github really threw me off. The path to api.json specified in the "SWAGGER_JSON=/api.json" assignment is not the actual path to api.json! The -v parameter will map the actual path to the path specified in SWAGGER_JSON. (Thanks ponelat !!)
--Ray