A simple REST service for demos (written in Go).
by William Mortl
This example collection of Go REST services demostrate several important Go principles:
- ping.go - a simple Go REST service
- hello.go - a Go REST service that reads from the query string
- dbadd.go & dbget.go - adds and retrieves records from an in-memory database, shows how to read JSON from the post body, and demonstrates how to read a variable from the REST path
- math.go - demonstrates Go routines... which is a Go "thread"
This solution contains a devcontainer that allows one to run and debug this REST service within Visual Studio Code. For a tutorial on devcontainers, visit this link.
Simply go to the "Debug" icon (on the left) and click the "play" button (near the top).
- Clone the repository into your Go path
- In the project directory, first build the project using: make
- Next, again in the project directory, create a Docker container: docker build -t {image name}:{tag name} .
From the CLI, run the container: docker container run -p 8080:8080 -it {image name}:{tag}
Ping service: http://localhost:8080/Ping
Hello service: http://localhost:8080/Hello?name=YourNameHere
Database Get service (retrieves by ID which is an int value, replace the trailing 0 with any int): http://localhost:8080/Db/0
Database Add / Update service (adds / updates by ID, replace the trailing 4 with any int): http://localhost:8080/Db/4
Post JSON in this format to the above URL to add this person to the database with id 4:
{
"name": "Taylor Swift",
"address": "10th Street",
"phone": "6155551212",
}
Math service (allowed operators: Add, Subtract, Multiple): http://localhost:8080/Math/Add
Post JSON in this format to the above URL to sum the numbers below:
{
"numbers": [
1,
2,
3,
4
]
}
Metrics are provided by Prometheus.
Prometheus metrics service: http://localhost:8080/metrics
Service documentation provided by swaggo.
Swagger service: http://localhost:8080/swagger/index.html