This is a simple echo server that utilizes the new features in Go 1.22. I wanted to showcase an example that now we do not need to use external libraries to handle the HTTP server and the routing.
- HTTP Server
- Routing
- Middleware
- Structured Logging
- Prometheus Metrics
- Flag and Environment Variable Configuration
GET /health
: Returns the health of the serverPOST api/v1/echo
: Returns the body of the requestGET /metrics
: Returns the metrics of the server
go run cmd/echo-service.go
Note that environment variables for PORT and LOG_LEVEL take precedence over the flags.
make build
make run
make docker-run
Health Check:
curl http://localhost:8080/health
Echo:
curl -X POST http://localhost:8080/api/v1/echo -d '{"message":"Hello World"}'
Metrics:
curl http://localhost:8080/metrics
make docker-clean
To add on or remove an endpoint just manipulate this section under server/server.go
func (s *Server) SetupRoutes() {}
Then add a handler for your route under handlers/handlers.go it's that simple.
This is to show that frameworks really are unnecessary for microservices with the new features in Go 1.22.