This repository contains a starter template for developing a microservice in Go. It provides a basic structure and functionality to get started with building a microservice from scratch.
- HTTP server implementation with customizable endpoints
- Key-value store functionality for storing and retrieving data
- Graceful shutdown handling
- Logging middleware for request logging
- Health and readiness probe endpoints
This starter template does not have any external dependencies. It is built using standard packages provided by Go.
To use this starter template, follow the steps below:
- Clone this repository to your local machine.
- Navigate to the project directory.
- Build the project using the following command:
go build -ldflags "-X main.version=1.5.0" -o main service.go
- Run the built executable:
./main
- The microservice will start running on the specified server address (default is
localhost:8080
). - You can now access the endpoints using HTTP requests.
go test -bench=. -benchmem
go test -v ./...
GET /healthz
: Health probe endpoint that returns HTTP 200 OK to indicate that the service is alive.GET /readyz
: Readiness probe endpoint that returns HTTP 200 OK to indicate that the service is ready to serve requests.POST /set
: Endpoint for setting key-value pairs. The request should include a JSON payload withkey
andvalue
fields.POST /get
: Endpoint for retrieving values by key. The request should include a JSON payload with thekey
field.
To set a key-value pair:
curl -X POST -H "Content-Type: application/json" -d '{"key": "key1", "value": "value1"}' http://localhost:8080/set
To retrieve a value by key:
curl -X POST -H "Content-Type: application/json" -d '{"key": "key1"}' http://localhost:8080/get
The starter template uses command-line flags and environment variables for configuration. The following options are available:
address
(flag) orSERVER_ADDRESS
(environment variable): Specifies the server address to listen on. Default islocalhost:8080
.shutdown-timeout
(flag) orSHUTDOWN_TIMEOUT
(environment variable): Specifies the timeout duration for graceful shutdown. Default is 10 seconds.enable-logging-middleware
(flag) orENABLE_LOGGING_MIDDLEWARE
(environment variable): Enables or disables the logging middleware for request logging. Default isfalse
.
The values of flags take precedence over environment variables. If a flag is not provided, the corresponding environment variable will be used. If neither is set, a default value will be used.
This starter template is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.