ServiceLib is a comprehensive Go library designed to accelerate the development of robust, production-ready microservices. It provides a collection of reusable components and utilities that address common challenges in service development, allowing developers to focus on business logic rather than infrastructure concerns.
The library follows modern Go practices and design patterns, with a focus on:
- Modularity: Each component can be used independently or together with others
- Testability: All components are designed with testing in mind
- Performance: Optimized for high-throughput microservices
- Reliability: Built-in error handling and recovery mechanisms
- Observability: Integrated logging, metrics, and tracing
Note: The Family-Service repository implements a GraphQL service using servicelib. This serves as an excellent illustration of servicelib's capabilities and potential.
For comprehensive documentation, please see the following resources:
- Developer Guide - Detailed component descriptions, architecture diagrams, usage examples, best practices, and troubleshooting guidance
- Integration Tests - Instructions for running integration tests, test environment setup, and troubleshooting test failures
- UML Diagrams - Architectural and component diagrams showing package dependencies and component relationships
- API Reference - Generated API documentation from Go doc comments
ServiceLib provides a comprehensive set of components organized by functionality:
- Configuration - Flexible configuration management with adapters for various sources (files, environment variables, etc.)
- Context - Context utilities for request handling, cancellation, and value propagation
- Dependency Injection - Container-based DI system for managing service dependencies
- Environment Variables - Utilities for working with environment variables with fallback values
- Error Handling - Structured error types and handling with rich context information
- Logging - Structured logging with Zap for high-performance logging
- Authentication - JWT, OAuth2, and OIDC implementations for secure service-to-service and user authentication
- Validation - Request and data validation using go-playground/validator
- Database - Database connection and transaction management with support for PostgreSQL, SQLite, and MongoDB
- Repository Pattern - Generic repository implementations for data access abstraction
- Transaction - Distributed transaction management with Saga pattern support
- GraphQL - Utilities for building GraphQL services with gqlgen integration
- Middleware - HTTP middleware components for common cross-cutting concerns
- Health Checks - Health check endpoints and handlers for Kubernetes readiness and liveness probes
- Shutdown - Graceful shutdown utilities for clean service termination
- Signal Handling - OS signal handling for responding to system events
- Telemetry - Metrics, tracing, and monitoring with Prometheus and OpenTelemetry
- Value Objects - Immutable objects that represent domain concepts
ServiceLib follows Semantic Versioning. The current version is available in the go.mod file.
- Go Version: Requires Go 1.24 or later
- Dependencies: All dependencies are managed through Go modules
- API Stability: APIs marked as stable will not have breaking changes within the same major version
- Backward Compatibility: We strive to maintain backward compatibility within the same major version
go get github.com/abitofhelp/servicelib
Important: In order to ensure that developers can build and work with this package within different IDEs and environments, please use the Makefile to build, test, etc.
To get started with ServiceLib, you can create a simple HTTP server with logging and middleware:
-
Create a new Go module:
mkdir myservice cd myservice go mod init myservice
-
Install ServiceLib:
go get github.com/abitofhelp/servicelib
-
Create a main.go file with a basic HTTP server using ServiceLib components.
For a complete example, see the Quickstart Example.
Each package in ServiceLib includes its own README.md with detailed documentation and examples:
- Authentication - JWT, OAuth2, and OIDC examples
- Configuration - Configuration management examples
- Database - Database connection and transaction examples
- Dependency Injection - DI container usage examples
- Health Checks - Health check implementation examples
- Logging - Structured logging examples
- Telemetry - Metrics and tracing examples
- Transaction - Distributed transaction examples
For complete example applications, see the Examples Directory.
For comprehensive documentation, please see the Developer Guide.
The auth
package provides implementations for JWT, OAuth2, and OIDC authentication methods:
-
JWT: JSON Web Token implementation for stateless authentication
- Token generation and validation
- Support for standard claims and custom claims
- Configurable signing methods (HMAC, RSA, ECDSA)
-
OAuth2: OAuth 2.0 client implementation
- Authorization Code, Client Credentials, and Password grant types
- Token refresh and validation
- State management for CSRF protection
-
OIDC: OpenID Connect implementation
- Discovery of provider configuration
- ID token validation
- User info retrieval
The config
package provides a flexible configuration system that supports multiple sources and formats:
-
Multiple Sources:
- YAML and JSON files
- Environment variables
- Command-line flags
- In-memory values
-
Features:
- Hierarchical configuration with dot notation
- Default values
- Type conversion
- Configuration reloading
- Validation
-
Adapters: Easily create custom adapters for different configuration sources
The context
package extends Go's standard context package with additional utilities:
- Value Management: Strongly typed context values
- Timeout Management: Utilities for working with context deadlines
- Cancellation: Simplified cancellation patterns
- Propagation: Utilities for propagating context values across service boundaries
The db
package provides utilities for database connection management and operations:
-
Connection Management:
- Connection pooling
- Automatic reconnection
- Health checks
-
Supported Databases:
- PostgreSQL (via pgx)
- SQLite
- MongoDB
-
Features:
- Transaction management
- Query execution with retries
- Result mapping
- Migrations
The date
package provides utilities for working with dates and times:
- Formatting: Consistent date/time formatting
- Parsing: Robust date/time parsing with error handling
- Comparison: Date comparison utilities
- Timezone: Timezone conversion and management
The di
package provides a container-based dependency injection system:
-
Container Types:
- Base container
- Service container
- Repository container
- Generic container
-
Features:
- Constructor injection
- Singleton instances
- Lazy initialization
- Scoped instances
- Circular dependency detection
The errors
package provides structured error types and handling:
-
Error Types:
- Domain errors
- Infrastructure errors
- Application errors
- Validation errors
-
Features:
- Error wrapping with context
- Error codes
- Localized error messages
- Stack traces
- Error categorization
The graphql
package provides utilities for building GraphQL services:
- Integration: Integration with gqlgen
- Error Handling: Structured error handling for GraphQL
- Middleware: GraphQL-specific middleware
- Resolvers: Utilities for implementing resolvers
The health
package provides components for implementing health check endpoints:
-
Check Types:
- Liveness checks
- Readiness checks
- Startup checks
-
Features:
- Configurable check intervals
- Automatic registration with HTTP server
- Detailed health status reporting
- Integration with Kubernetes probes
The logging
package provides structured logging with Zap:
- Log Levels: Debug, Info, Warn, Error, Fatal
- Structured Logging: Key-value pairs for better searchability
- Output Formats: JSON, console
- Integration: Context-aware logging
- Performance: High-performance logging with minimal allocations
The middleware
package provides HTTP middleware components:
- Authentication: JWT authentication middleware
- Logging: Request/response logging
- Metrics: Request metrics collection
- Tracing: Distributed tracing
- Recovery: Panic recovery
- CORS: Cross-Origin Resource Sharing
- Rate Limiting: Request rate limiting
The repository
package provides generic repository implementations:
- Generic Repository: Type-safe repository implementation
- CRUD Operations: Create, Read, Update, Delete
- Query Building: Fluent query building
- Pagination: Offset and cursor-based pagination
- Sorting: Multi-field sorting
The shutdown
package provides graceful shutdown utilities:
- Graceful Shutdown: Orderly shutdown of services
- Timeout Management: Configurable shutdown timeouts
- Dependency Order: Shutdown in the correct order
- Resource Cleanup: Ensure all resources are properly released
The signal
package provides OS signal handling:
- Signal Types: SIGINT, SIGTERM, SIGHUP
- Custom Handlers: Register custom signal handlers
- Graceful Shutdown: Integration with shutdown package
The stringutil
package provides string manipulation utilities:
- Formatting: String formatting utilities
- Validation: String validation
- Transformation: Case conversion, trimming, etc.
- Generation: Random string generation
The telemetry
package provides utilities for metrics, tracing, and monitoring:
-
Metrics:
- Prometheus integration
- Counter, gauge, histogram, and summary metrics
- Default metrics for HTTP, gRPC, and database
-
Tracing:
- OpenTelemetry integration
- Span creation and management
- Context propagation
- Automatic instrumentation for HTTP and gRPC
-
Monitoring:
- Health check integration
- Alerting utilities
- Dashboard templates
The transaction
package provides utilities for managing distributed transactions:
- Saga Pattern: Implementation of the Saga pattern for distributed transactions
- Compensation: Transaction compensation for rollback
- Coordination: Transaction coordination across services
The validation
package provides request and data validation:
- Integration: Integration with go-playground/validator
- Custom Validators: Define custom validation rules
- Validation Middleware: HTTP request validation
- Error Handling: Structured validation errors
ServiceLib uses Go modules for dependency management and Make for build automation.
- Go 1.24 or higher
- Make (optional, for using the Makefile)
# Build the library
make build
# Run tests
make test
# Run tests with coverage
make coverage
# Run linter
make lint
# Format code
make fmt
# Check for security vulnerabilities
make security
Issue: Database connections are not being properly released, leading to connection pool exhaustion.
Solution: Ensure that all database operations properly close their resources, especially in error cases. Use the defer
statement to ensure connections are returned to the pool.
Issue: Memory usage grows over time, indicating potential memory leaks.
Solution: Use the telemetry package to monitor memory usage and identify leaks. Common causes include:
- Forgetting to close response bodies
- Goroutines that never terminate
- Large objects stored in context values
Issue: Dependency injection container fails with circular dependency errors.
Solution: Restructure your dependencies to break the cycle. Consider:
- Using interfaces to break direct dependencies
- Introducing a mediator or facade
- Using lazy initialization for some dependencies
To enable debug logging for troubleshooting, configure the logger with debug level and console format for better readability during development.
For detailed request tracing:
- Enable the tracing middleware
- Set the sampling rate to 1.0 (100%)
- Use the OpenTelemetry UI or Jaeger to view traces
-
Layered Architecture: Organize your service with clear separation between:
- API/Transport layer (HTTP, gRPC)
- Service layer (business logic)
- Repository layer (data access)
-
Dependency Injection: Use the DI container to manage dependencies and make testing easier
-
Configuration: Externalize all configuration and use environment variables for deployment-specific settings
-
Structured Errors: Use the errors package to create structured errors with context information for better error handling and debugging.
-
Error Categorization: Categorize errors to handle them appropriately at the API boundary, mapping different error types to appropriate HTTP status codes.
-
Connection Pooling: Configure database connection pools based on expected load by setting appropriate values for max open connections, max idle connections, and connection lifetime.
-
Caching: Use caching for frequently accessed, rarely changed data
-
Pagination: Always implement pagination for endpoints that return collections
-
Unit Tests: Test each component in isolation using mocks
-
Integration Tests: Test the integration between components
-
End-to-End Tests: Test the complete service flow
-
Load Tests: Test performance under load to identify bottlenecks
ServiceLib is designed with the following architectural principles:
Each package in ServiceLib is designed to be used independently or together with other packages. This allows you to use only the components you need without bringing in unnecessary dependencies.
ServiceLib implements several design patterns:
- Repository Pattern: Abstracts data access behind interfaces
- Dependency Injection: Manages dependencies and facilitates testing
- Factory Pattern: Creates complex objects with consistent configuration
- Adapter Pattern: Converts interfaces to work with different systems
- Observer Pattern: Implements event-based communication
ServiceLib follows semantic versioning (SemVer):
- Major version (X.y.z): Incompatible API changes
- Minor version (x.Y.z): Backwards-compatible functionality
- Patch version (x.y.Z): Backwards-compatible bug fixes
- Minimum Go version: 1.24
- Tested Go versions: 1.24, 1.25
ServiceLib has the following major dependencies:
- zap: Structured logging
- prometheus: Metrics collection
- opentelemetry: Distributed tracing
- validator: Request validation
- pgx: PostgreSQL driver
- gqlgen: GraphQL implementation
- No breaking changes will be introduced in minor or patch releases
- Deprecated features will be marked with
Deprecated
in the documentation - Deprecated features will be removed only in major version releases
- Migration guides will be provided for major version upgrades
Contributions are welcome! Please feel free to submit a Pull Request. See the CONTRIBUTING.md file for detailed guidelines and instructions.
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and linting
- Submit a pull request
- Follow Go best practices and style guidelines
- Write tests for new functionality
- Document public APIs
- Keep backward compatibility in mind
This project is licensed under the MIT License - see the LICENSE file for details.