Adding OpenAPI documentation to your API
Because we already have most of our API defined, I think the right approach here is to generate an OpenAPI document for our code, so let’s do that.
We will incorporate the swaggo
library annotations into our previous code, and then we will generate an OpenAPI document from it. So, let’s start with general details of the API. For that, we will add annotations to our main
package with all the important information about the API, such as the title, the version, the description, and so on. So, let’s see what it looks like:
// @title Shopping List API
// @version 0.1
// @description An API for managing shopping lists
// @host my-shopping-lists.com/api
// @BasePath /
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
// @description Type "Bearer" followed by a space and the JWT token
package main
At this point, I’m already providing a lot of information about the API,...