This repository contains a demo API built with go. Its main purposes are:
- Show how the standard library may be used to build an API, and
- Show how Docker image sizes may be minimized by using multi-stage builds.
The API listens on port 3000 and exposes a /ping
endpoint for GET
, POST
, PUT
, PATCH
and
DELETE
HTTP requests. It responds with the values of the request's HTTP method, query parameters
and body, in case there was one.
Example request:
curl --location --request POST 'localhost:3000/ping?qs1-key=qs1-value&qs2-key=qs2-value' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'body1-key=body1-value' \
--data-urlencode 'body2-key=body2-value'
Example response:
{
"method": "POST",
"queryParams": {
"qs1-key": ["qs1-value"],
"qs2-key": ["qs2-value"]
},
"body": {
"body1-key": ["body1-value"],
"body2-key": ["body2-value"]
}
}
go build -o app && ./app
Three Dockerfiles have been added to the repository:
Dockerfile.full
: contains a basic setup that uses thegolang:1.14.3
base image.Dockerfile.alpine
: same setup asDockerfile.full
, but based on a golang alpine image.Dockerfile
: uses a multi-stage build to optimize image size.
The resulting image sizes are:
Dockerfile.full
: 817MBDockerfile.alpine
: 378MBDockerfile
: 13.3MB
In order to try out the multi-stage image, you must run the following:
# Build the image
docker build -t sasalatart/go-api-demo:latest .
# Run a container
docker run -p 3000:3000 sasalatart/go-api-demo:latest
The API will then be listening on your machine's port 3000.
go test