An open API service indexing awesome lists of open source software.

https://github.com/roshbhatia/go-service-template

Template project for golang microservices
https://github.com/roshbhatia/go-service-template

docker go microservice rest-api template

Last synced: 2 months ago
JSON representation

Template project for golang microservices

Awesome Lists containing this project

README

          

# go-service-template

This is a template for Go-based microservices -- referred to as `echo-service` throughout the documentation. As the name suggests, the service echoes text sent via a REST endpoint.

Updated to Go 1.26.1 using idiomatic practices including `log/slog` and enhanced `net/http` routing.

## REST API Contract

echo-service provides the following endpoints

`GET /health`
- Description: Returns a JSON object comprised of a timestamp of when the request was processed, the health status, and the http status returned by the endpoint

Response body schema:
```json
{
"timestamp" : "string (RFC3339)",
"health_status" : "string",
"http_status": "string"
}
```

`POST /echo`
- Description: Returns a JSON object comprised of a timestamp of when the request was processed, and the string POSTed to the endpoint

Request body schema:
```json
{
"echo_str" : "string"
}
```

Response body schema:
```json
{
"timestamp" : "string (RFC3339)",
"echo_str" : "string"
}
```

## Development Environment

This project includes a `shell.nix` for use with the Nix package manager. To enter the development environment with the correct Go version and tools:

```bash
nix-shell
```

## Config

echo-service fetches its config through the environment. The following environment variables are expected (or supported):
- `SERVICE_PORT`
- optional: true (default: 8080)
- description: The port the REST API runs on.
- `SSL_CERT_PATH`
- optional: true
- description: The full path of the SSL cert, must be set alongside `SSL_KEY_PATH` to enable HTTPS.
- `SSL_KEY_PATH`
- optional: true
- description: The full path of the SSL key, must be set alongside `SSL_CERT_PATH` to enable HTTPS.

## Makefile Usage

`make clean`
- Deletes `./bin` folder and associated artifacts.

`make test`
- Runs `go test` on all packages.

`make run`
- Runs the echo-service locally.

`make build`
- Compiles the echo-service binary, storing it in `./bin/echo-service`.

`make docker-build`
- Builds the docker container for echo-service.

`make docker-run`
- Runs the docker container.

## Local Testing

Test it with curl!
```bash
# For HTTP, locally
$> SERVICE_PORT=8080 make run
$> curl http://localhost:8080/health
$> curl -d '{"echo_str":"hello world"}' -H 'Content-Type: application/json' http://localhost:8080/echo
```

## Features
- Structured logging with `log/slog`.
- Graceful shutdown using `signal.NotifyContext`.
- Modern `net/http` routing (Go 1.22+).
- Nix development shell.