Make REST API Documentation using swagger in Go
For golang based HTTP/REST API documentation,I choose swagger. go-swagger has several features for swagger documentation. The go-swagger can generate swagger spec based code generation but I already have an REST API server. I use go-swagger with golang comment annotation for swagger spec generation.
For REST API development, Design first with writing spec and then generating codes from it is a good approach. goa is a famous tool for this style.
Generate go-swagger spec⌗
The go-swagger has a command that will let you generate a swagger spec document from codes. The command scan all files from current directory or specific file path to sub directories.
To generate a spec:
swagger generate spec
command makes swagger.json spec.
Operations and struct model mapping⌗
One of reasons why I choose swagger or api documentation generation tool is that parsing source code and using on api spec.
The definitions/Article
and definitions/Error
are mapping from struct with swagger:model
It is convenient if you have complex request and response payloads.
Embedded static asset(swagger.json) with go-bindata⌗
I want to embedded the swagger-ui and redoc frontend page to my rest api server. go-bindata converts any text or binary file into Go source code.
That’s it. the swagger.json
is embedded in golang code. With gin framework, you can use it with staticbin.
Embedded with swagger-ui, you can use swagger-doc’s assets for it.
I’m not sure this approach is good. I think that better approach exists for it. apiblueprint is simple yaml structure than swagger’s yaml. but I have not found good apiblueprint utils for my taste for golang code generation or spec generation from code.