A production-ready Go service template that supports both gRPC and REST APIs using the same protobuf definitions, built with clean architecture principles and comprehensive test coverage.
- Dual API Support: gRPC and REST APIs from the same protobuf definitions
- Clean Architecture: Domain, Service, Transport, and Infrastructure layers
- High Scalability: Structured logging, context handling, graceful shutdown
- Production Ready: Interceptors, middleware, request validation
- Development Friendly: Hot reload, Docker Compose, comprehensive tooling
- Comprehensive Testing: Extensive unit tests with >80% coverage target
- Overall Coverage: 0.9% (increasing daily)
- Server Layer: 3.6% - Auth server, health server, middleware, interceptors
- Storage Layer: 0.6% - Core storage, users, validations
- Utils: 3.2% - Hashing, tokens, helpers, error handling
- Config: 3.4% - Configuration loading and validation
- Services: 0.2% - Auth service, user service
- Models: 0.0% - Basic struct validation
- β Server Layer: Complete test suite for auth server, health server, middleware
- β Storage Layer: Core storage utilities, user operations, validations
- β Utils: Hashing, JWT tokens, helper functions, custom error handling
- β Config: Environment variable parsing, configuration validation
- β Services: Service composition and basic functionality
- β Models: Data structure validation and edge cases
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Transport Layer β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β gRPC Server (Port 8080) β REST Gateway (Port 8081) β
β βββββββββββββββββββββββ β βββββββββββββββββββββββββββ β
β β Auth Server β β β REST Gateway β β
β β Health Server β β β (HTTP/JSON) β β
β β Middleware β β β β β
β β Interceptors β β β β β
β βββββββββββββββββββββββ β βββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Service Layer β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β AuthService β βUserService β βHealthServiceβ β
β β β’ SignUp β β β’ GetUsers β β β’ Check β β
β β β’ SignIn β β β’ Paginationβ β β’ Watch β β
β β β’ SignOut β β β’ Validationβ β β’ DB Health β β
β β β’ Refresh β βββββββββββββββ βββββββββββββββ β
β β β’ Revoke β β
β βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Domain Layer β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β User β β Auth β β Validation β β
β β β’ Models β β β’ Tokens β β β’ Rules β β
β β β’ Credentialsβ β β’ JWT β β β’ Errors β β
β β β’ Validationβ β β’ Security β β β’ Messages β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Infrastructure Layer β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β PostgreSQL β β Redis β β Logger β β
β β β’ Migrationsβ β β’ Caching β β β’ Structuredβ β
β β β’ Connectionβ β β’ Sessions β β β’ Correlationβ β
β β β’ Pool β β β’ Rate Limitβ β β’ Levels β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Go 1.24+
- Docker & Docker Compose
- Protocol Buffers compiler
-
Clone the repository
git clone <repository-url> cd go-starter-grpc
-
Install protobuf tools
cd auth-service make install-tools -
Generate protobuf code
make proto
-
Start services with Docker Compose
docker-compose up -d
- Run with hot reload:
make dev - Build:
make build - Run:
make run - Test:
make test - Clean:
make clean
Run all tests:
go test ./...Run tests with coverage:
go test -coverpkg=./... -coverprofile=coverage.out ./...
go tool cover -func=coverage.outRun specific package tests:
go test -v ./server/... # Server layer tests
go test -v ./storage/... # Storage layer tests
go test -v ./utils/... # Utility function tests
go test -v ./config/... # Configuration tests
go test -v ./services/... # Service layer testsView coverage in browser:
go tool cover -html=coverage.out- Unit Tests: Comprehensive coverage of individual functions
- Integration Tests: Service layer and storage interactions
- Benchmark Tests: Performance testing for critical paths
- Edge Case Testing: Boundary conditions and error scenarios
- Mock Testing: Isolated testing with mocked dependencies
- AuthService: Authentication and user management
SignUp- User registration with validationSignIn- User authentication with JWT tokensSignOut- User logout and token invalidationRefreshToken- Token refresh mechanismRevokeToken- Token revocationListUsers- User listing with pagination
- HealthService: Service health checks
Check- Single health checkWatch- Streaming health monitoring
- POST
/v1/auth/signup- User registration - POST
/v1/auth/signin- User login - POST
/v1/auth/signout- User logout - POST
/v1/auth/refresh- Token refresh - POST
/v1/auth/revoke- Token revocation - GET
/v1/users- List users with pagination - GET
/v1/health- Health check endpoint
Environment variables:
APP_PORT: gRPC server port (default: 8080)REST_PORT: REST gateway port (default: 8081)POSTGRES_HOST: PostgreSQL host (default: localhost)POSTGRES_PORT: PostgreSQL port (default: 5432)LOG_LEVEL: Logging level (default: debug)JWT_ACCESS_TOKEN_SECRET: JWT signing secretJWT_REFRESH_TOKEN_SECRET: JWT refresh secretHEALTH_CHECK_TIMEOUT: Health check timeout in seconds
auth-service/
βββ cmd/ # Application entry points
βββ internal/ # Private application code
β βββ domain/ # Domain entities and interfaces
β βββ service/ # Business logic implementation
β βββ transport/ # gRPC and REST handlers
β βββ infrastructure/ # Database, external services
βββ proto/ # Protocol buffer definitions
βββ config/ # Configuration management
β βββ config.go # Main configuration struct
β βββ validation.go # Configuration validation
βββ server/ # Server setup and middleware
β βββ auth_server.go # Authentication gRPC server
β βββ health_server.go # Health check server
β βββ middleware.go # gRPC middleware (metrics, recovery, rate limiting)
β βββ interceptors.go # Logging and correlation interceptors
β βββ register.go # Service registration
β βββ server.go # Main server orchestration
βββ services/ # Business logic services
β βββ auth/ # Authentication service
β β βββ service.go # Service interface
β β βββ signin.go # Sign-in logic
β β βββ signup.go # Sign-up logic
β β βββ token.go # Token management
β β βββ refresh.go # Token refresh
β βββ users/ # User management service
βββ storage/ # Data persistence layer
β βββ storage.go # Core database operations
β βββ users.go # User CRUD operations
β βββ user_tokens.go # Token storage and management
β βββ validations.go # Data validation rules
βββ utils/ # Utility functions
β βββ hash.go # Password hashing (bcrypt)
β βββ token.go # JWT token utilities
β βββ helpers.go # Validation helpers
β βββ errors.go # Custom error types
βββ models/ # Data models
β βββ user.go # User entity
β βββ user_token.go # Token entity
βββ scripts/ # Database scripts
βββ Dockerfile.dev # Development container
βββ .air.toml # Hot reload configuration
βββ Makefile # Build and development commands
- Define APIs: Update
.protofiles with HTTP annotations - Generate Code: Run
make prototo generate Go code - Implement Logic: Add business logic in service layer
- Add Tests: Create comprehensive unit tests for new functionality
- Test: Run
make testto verify functionality and coverage - Run: Use
make devfor development with hot reload
- Server Layer: Complete coverage of gRPC servers and middleware
- Storage Layer: Database operations and validation logic
- Utils: Core utility functions and error handling
- Config: Configuration loading and validation
- Services: Business logic and service composition
- Unit Tests: Test individual functions in isolation
- Mock Dependencies: Use mocks for external dependencies
- Edge Cases: Test boundary conditions and error scenarios
- Performance: Include benchmark tests for critical paths
- Coverage: Aim for >80% overall test coverage
- Health Checks: Built-in health check endpoints with database connectivity
- Graceful Shutdown: Proper signal handling and cleanup
- Structured Logging: JSON-formatted logs with correlation IDs
- Metrics: Request/response metrics and monitoring
- Security: JWT authentication, rate limiting, CORS
- Database: Connection pooling, migrations, error handling
- Monitoring: Health endpoints, structured logging, metrics
- Follow Go coding standards and best practices
- Add comprehensive tests for new functionality
- Maintain or improve test coverage
- Update documentation and examples
- Use conventional commit messages
- Test edge cases and error scenarios
This package is open-source and available under the MIT license. See LICENSE for more details.