GoSale is a production-ready, enterprise-grade foundation for building scalable Go services. It demonstrates best practices for building robust, observable, and secure web services in Go, featuring custom web framework implementation, comprehensive middleware support, JWT authentication with OPA (Open Policy Agent), and Kubernetes-native deployment.
This project follows a deploy-first philosophy, emphasizing containerization and Kubernetes deployment from day one, making it ideal for cloud-native development workflows.
- Custom Web Framework: Built on top of
httptreemuxwith middleware support for maximum flexibility - Authentication & Authorization:
- JWT (JSON Web Token) based authentication using RSA key pairs
- Policy-based authorization using Open Policy Agent (OPA) with embedded Rego policies
- Secure key management with filesystem-based keystore
- Comprehensive Middleware Stack:
- Request logging with trace IDs
- Error handling and panic recovery
- Metrics collection (requests, errors, panics, goroutines)
- Authentication and authorization enforcement
- Observability:
- Structured JSON logging with custom formatter
- Built-in metrics endpoint (expvar)
- Request tracing with unique trace IDs
- Performance profiling via pprof
- Production Ready:
- Graceful shutdown with load shedding
- Configuration management with environment variables and flags
- Health check endpoints (liveness/readiness)
- CPU quota awareness with GOMAXPROCS tuning
- Container-First Design: Multi-stage Docker builds with minimal Alpine-based images
- Kubernetes Manifests:
- Kustomize-based configuration management
- Resource quotas and limits
- Development and production overlays
- Service and deployment definitions
- Local Development: Kind (Kubernetes in Docker) cluster configuration for local testing
- Build Metadata: Build version and date baked into binaries and container labels
gosale/
βββ app/
β βββ services/
β β βββ sales-api/ # Main HTTP API service
β βββ tooling/
β βββ logfmt/ # Log formatting utility
β βββ sales-admin/ # Admin CLI for JWT generation
βββ business/
β βββ web/ # Business-level web framework
β βββ v1/
β βββ auth/ # Authentication & authorization
β βββ mid/ # HTTP middleware
β βββ response/ # HTTP response helpers
βββ foundation/
β βββ keystore/ # Cryptographic key management
β βββ logger/ # Structured logging
β βββ web/ # Core web framework
βββ zarf/
β βββ docker/ # Dockerfiles
β βββ k8s/ # Kubernetes manifests
β β βββ base/ # Base configurations
β β βββ dev/ # Development overlays
β βββ keys/ # Authentication keys
βββ vendor/ # Vendored dependencies
- Foundation Layer: Core utilities (logging, web framework, key management)
- Business Layer: Business logic, authentication, and middleware
- Application Layer: Executable services and tools
- Infrastructure Layer: Deployment configurations and scripts
- Go 1.25.5 or higher
- Docker
- kubectl
- Kind (for local Kubernetes)
- Make
# Run the service locally
make run
# View help and configuration options
make run-help
# Test the API
make curl
# Test authenticated endpoint (requires TOKEN env var)
export TOKEN="your-jwt-token"
make curl-auth
# Run load tests
make load# Run the admin tool to generate JWT tokens
make admin
# This will:
# - Generate RSA key pairs
# - Create JWT tokens with claims
# - Validate signatures# Create a local Kind cluster
make dev-up
# Build the Docker image
make service
# Load image into Kind cluster
make dev-load
# Deploy to Kubernetes
make dev-apply
# View logs
make dev-logs
# Check deployment status
make dev-status
# Update after code changes
make dev-update
# Tear down the cluster
make dev-down# Run expvarmon to visualize service metrics
make metrics-view-scThe project uses RSA key pairs for JWT signing and verification:
# Generate new RSA key pair
openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:2048
openssl rsa -pubout -in private.pem -out public.pemAuthorization is handled via Open Policy Agent with embedded Rego policies. The auth system supports:
- Token signature validation
- Claims verification
- Role-based access control
- Policy-based authorization rules
All logs are output in structured JSON format with:
- Trace IDs for request correlation
- Timestamps
- Log levels
- Contextual information
Use the included logfmt tool to make logs human-readable:
go run app/services/sales-api/main.go | go run app/tooling/logfmt/main.goThe service exposes metrics via /debug/vars endpoint:
- Request counts
- Error counts
- Panic counts
- Goroutine counts
- Memory statistics
Access the debug server (default: localhost:4000) for:
- Metrics:
http://localhost:4000/debug/vars - CPU profile:
http://localhost:4000/debug/pprof/profile - Heap profile:
http://localhost:4000/debug/pprof/heap
The service is configured via environment variables and command-line flags:
| Variable | Default | Description |
|---|---|---|
WEB_API_HOST |
0.0.0.0:3000 |
API server address |
WEB_DEBUG_HOST |
0.0.0.0:4000 |
Debug/metrics server address |
WEB_READ_TIMEOUT |
5s |
HTTP read timeout |
WEB_WRITE_TIMEOUT |
10s |
HTTP write timeout |
WEB_IDLE_TIMEOUT |
120s |
HTTP idle timeout |
WEB_SHUTDOWN_TIMEOUT |
20s |
Graceful shutdown timeout |
AUTH_KEYS_FOLDER |
zarf/keys/ |
Path to RSA keys |
AUTH_ACTIVE_KID |
Active key ID for JWT signing |
# Run tests (when test infrastructure is added)
go test ./...
# Run with coverage
go test -cover ./...
# Run benchmarks
go test -bench=. ./...Key dependencies managed in go.mod:
github.com/ardanlabs/conf/v3- Configuration managementgithub.com/dimfeld/httptreemux/v5- HTTP routergithub.com/golang-jwt/jwt/v4- JWT implementationgithub.com/google/uuid- UUID generationgithub.com/open-policy-agent/opa- Policy engine
All dependencies are vendored for reproducible builds.
Based on the project's evolution through issues and PRs:
- Kubernetes and Kind setup for deploy-first workflow (#1)
- Docker containerization with multi-stage builds (#2)
- Kubernetes deployment manifests and resource quotas (#3)
- Service startup, shutdown, and configuration management (#5)
- Custom HTTP router with middleware support (#7)
- Comprehensive middleware for logging, errors, panics, and metrics (#9)
- JWT and OPA integration for authentication and authorization (#11)
- Secure keystore implementation (#13)
- Database integration (PostgreSQL)
- API versioning strategy
- Rate limiting and throttling
- Distributed tracing (OpenTelemetry)
- CI/CD pipeline configuration
- API documentation (OpenAPI/Swagger)
- End-to-end tests
- Performance benchmarks
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes with descriptive messages
- Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
- Built with inspiration from Go best practices
- OPA integration for flexible authorization
- Kubernetes-native design principles
- Community-driven development
- Go Official Documentation
- Open Policy Agent Documentation
- Kubernetes Documentation
- JWT Best Practices
Built with β€οΈ using Go
