This repository contains an almost minimal setup for developing a REST service using Echo, OpenAPI and Swagger-UI integration.
The generation and build process is using air for easy setup.
The OpenAPI defintion is defined with code using go in the internal/openapi/definition.go
file.
During the build steps air is going to generate the openapi3.json
file in the cmd/swagger
directory.
Additionally, the according server interfaces are generated with oapi-codegen into the pkg/api/
folder.
The application will serve the Swagger-UI at swagger/
endpoint.
The backend is implemented in the internal/server/server.go
file.
- Clone the project
git clone https://github.com/b1tray3r/go-openapi3.git
- Change the directory
cd go-openapi3
- Install openapi-gen
go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@latest
- Install air
go install github.com/air-verse/air@latest
- Launch
air
Many things are hardcoded on purpose to keep the repository as barebone as possible.
This contains the swagger-ui code and requires access to unpkg servers.
The file is requesting the generated /swagger/openapi3.json
file to build the GUI.
Using air the complete build pipeline is executed on the fly.
When building the final application you have to execute all pre_cmd
entries in the .air.toml
configuration.
Please note that all *.gen.go
files are ignored by air.
It is important for air to exclude this files, because generating these file(s) will cause air to loop indefinetly.
This configuration is responsible to generate the api interfaces and types for echo. I am not sure whether this is an optimal configuration, but it seems to work.
-
Thanks to MarioCarrion for his Youtube guide.