A simple web application in Go which accepts math problems via the URL and returns the response in JSON.
By default, HTTP server runs on port 8080.
There are four endpoints, one for each basic math operation:
add
subtract
multiply
divide
All endpoints accept GET requests with two query string parameters: x
and y
.
Results are cached for 1 minute.
go run main.go
go test
Operation | Example |
---|---|
add | http://localhost:8080/add?x=2&y=2 |
subtract | http://localhost:8080/subtract?x=7&y=4 |
multiply | http://localhost:8080/multiply?x=6&y=5 |
divide | http://localhost:8080/divide?x=9&y=3 |
The HTTP server returns JSON response containing following fields:
action
- requested math operationx
- first number for math operationy
- second number for math operationanswer
- result of math operationcached
- is cached result returned
Example:
{
"action": "divide",
"answer": 3,
"x": 9,
"y": 3,
"cached": true
}
Request:
curl "http://localhost:8080/add?x=4&y=3"
Response:
{"action":"add","answer":7,"x":4,"y":3,"cached":true}
docker build -t calcserver .
docker run -p 8080:8080 calcserver