A production-ready Go HTTP API service built for the Zama DevOps challenge. This service provides a simple sum calculation endpoint with comprehensive observability, security, and reliability features.
- Health Check Endpoint:
/healthzwith comprehensive system checks - Sum Calculation:
/api/v1/sum- Calculate sum of numbers with validation - API Documentation: Built-in endpoint documentation
- Structured Logging: JSON logging with request tracing
- Metrics: Prometheus metrics for observability
- Graceful Shutdown: Proper signal handling
- Request Validation: Input validation and error handling
- Security: Request ID tracking, recovery middleware
- Health Checks: Separate liveness and readiness probes
- Go 1.21+
- Make (optional, for convenience commands)
# Build and run
make build
./server
# Or run directly
go run ./cmd/server
# Or use make for development
make devThe service will start on port 8080 by default.
# Health check
curl http://localhost:8080/healthz
# Sum calculation
curl -X POST http://localhost:8080/api/v1/sum \
-H "Content-Type: application/json" \
-d '{"numbers": [1.5, 2.5, 3.0]}'
# API documentation
curl http://localhost:8080/api/v1/sum
# Metrics
curl http://localhost:8080/metricsThe service is configured via environment variables:
| Variable | Default | Description |
|---|---|---|
PORT |
8080 |
Server port |
HOST |
0.0.0.0 |
Server host |
LOG_LEVEL |
info |
Log level (debug, info, warn, error) |
LOG_FORMAT |
json |
Log format (json, text) |
ENVIRONMENT |
development |
Environment (development, production) |
METRICS_ENABLED |
true |
Enable Prometheus metrics |
READ_TIMEOUT |
30s |
HTTP read timeout |
WRITE_TIMEOUT |
30s |
HTTP write timeout |
SHUTDOWN_TIMEOUT |
15s |
Graceful shutdown timeout |
Comprehensive health check with system status.
Response:
{
"status": "healthy",
"timestamp": "2024-01-01T00:00:00Z",
"version": "1.0.0",
"uptime": "1h23m45s",
"checks": {
"memory": "ok",
"goroutines": "ok",
"uptime": "ok"
},
"request_id": "req-123"
}Kubernetes liveness probe endpoint.
Kubernetes readiness probe endpoint.
Calculate the sum of an array of numbers.
Request:
{
"numbers": [1.5, 2.5, 3.0]
}Response:
{
"sum": 7.0,
"count": 3,
"numbers": [1.5, 2.5, 3.0],
"timestamp": "2024-01-01T00:00:00Z",
"request_id": "req-123"
}Validation:
- Minimum 2 numbers required
- Maximum 100 numbers allowed
- Numbers must be valid floats
Get API documentation and examples.
Prometheus metrics endpoint with:
- HTTP request duration and count
- Request/response sizes
- Active connections
- Go runtime metrics
- Application info
make help # Show available commands
make build # Build the application
make test # Run tests
make test-race # Run tests with race detection
make coverage # Generate test coverage report
make lint # Run linter (requires golangci-lint)
make fmt # Format code
make vet # Run go vet
make dev # Build and run locally
make docker-build # Build Docker image
make docker-run # Run Docker container# Run all tests
go test ./...
# Run tests with coverage
go test ./... -coverprofile=coverage.out
go tool cover -html=coverage.out -o coverage.html
# Run tests with race detection
go test ./... -race
# Benchmark tests
go test -bench=. ./...api-go-service/
├── cmd/
│ └── server/ # Application entry point
├── internal/
│ ├── config/ # Configuration management
│ ├── handlers/ # HTTP handlers
│ ├── middleware/ # HTTP middleware
│ ├── models/ # Request/response models
│ └── server/ # Server setup and routing
├── pkg/
│ └── logger/ # Logging utilities
├── Dockerfile # Container definition
├── Makefile # Development commands
└── go.mod # Go module definition
# Build image
docker build -t zama-api-service .
# Build with version info
docker build \
--build-arg VERSION=1.0.0 \
--build-arg COMMIT=abc123 \
-t zama-api-service:1.0.0 .# Run container
docker run -p 8080:8080 zama-api-service
# Run with custom environment
docker run -p 8080:8080 \
-e LOG_LEVEL=debug \
-e ENVIRONMENT=development \
zama-api-service- Structured JSON logging
- Request ID tracking
- Component-based logging
- Configurable log levels
- HTTP request metrics (duration, count, size)
- Application metrics (active connections, goroutines)
- Go runtime metrics
- Custom business metrics
- Liveness probe for Kubernetes
- Readiness probe for load balancers
- Comprehensive system health status
- Memory and goroutine monitoring
- Request ID generation and tracking
- Panic recovery middleware
- Input validation and sanitization
- Structured error responses
- Security headers (via reverse proxy)
- Efficient JSON handling
- Connection pooling ready
- Graceful shutdown
- Resource monitoring
- Benchmark tests included
The service includes comprehensive tests:
- Unit Tests: 89.9% handler coverage
- Integration Tests: HTTP endpoint testing
- Benchmark Tests: Performance testing
- Race Detection: Concurrency safety
- Edge Cases: Validation and error handling
Current test coverage:
- Handlers: 89.9%
- Overall: 24.9% (handlers are the main business logic)
Set appropriate values for production:
export ENVIRONMENT=production
export LOG_LEVEL=info
export LOG_FORMAT=json
export PORT=8080Configure your load balancer/orchestrator:
- Liveness:
GET /healthz/live - Readiness:
GET /healthz/ready - Health:
GET /healthz
The service exposes Prometheus metrics at /metrics for:
- Request latency and throughput
- Error rates
- Resource utilization
- Application health
- Run tests:
make test - Run linter:
make lint - Format code:
make fmt - Check coverage:
make coverage
This project is part of the Zama DevOps challenge.