An example API written with the Go Gin web framework to demonstrate generating an OpenAPI v3 specification.
Dependencies:
- Go
- Curl
Note: It'll be easiest to follow along if you run steps 2-4 in their own terminal windows or panes.
- Install deps,
go mod tidy. - Start the app,
go run main.go.- It will run on port
8080.
- It will run on port
- Start the Optic proxy,
optic capture --reverse-proxy openapi.yml http://localhost:8080.- The URL specified in the command is where our app is listening. It is the destination the proxy will forward to.
- The output should tell you, but the proxy will default to running on port
8000.
- Send traffic to the Optic proxy by running,
./curl.sh.- Take a peek inside this script too. It's just running
curlcommands for the API endpoints.
- Take a peek inside this script too. It's just running
- Stop the app and the proxy with
ctrl-cin their respective terminals. - Write the captured traffic to
./openapi.yml,optic update openapi.yml --all
If you take a look at newly generated openapi.yml fil. You'll see 3 documented endpoints that correspond to the requests made in the ./curl.sh file. You can change the endpoints, add or remove them, and rerun steps 2-5 to update your OpenAPI file.