Go Utilities
A modular collection of reusable Go components to simplify backend development. It includes robust tools for structured logging, resilient HTTP clients, Kafka integration, database utilities, and server setupβengineered for scalability and maintainability.
π Features
1. Logger
- Structured logging with pluggable backends (e.g.,
zerolog
).
- Context-aware logging with trace IDs and component tagging.
- Mockable for unit testing.
- π Logger Documentation
2. HTTP Client
- Built-in circuit breaker and retry mechanism via
heimdall
.
- Supports GET, POST, PUT, and DELETE requests.
- Configurable timeouts, retries, and backoff strategies.
- π HTTPClient Documentation
3. Kafka
- Easy setup for Kafka producers and consumers.
- Compatible with local and AWS MSK environments.
- Configurable compression, batching, retries, and message encoding.
- π Kafka Documentation
4. Database
- Connection pooling and transaction support.
- SQL query pagination helpers.
- Common error handling utilities.
- π Database Documentation
5. Server
- Modular HTTP server setup with customizable middleware.
- Graceful shutdown support.
- Built-in middleware for CORS, panic recovery, and request size limits.
- π Server Documentation
π Project Structure
go-utilities/
βββ database/ # DB connection pooling, transactions, pagination
βββ httpclient/ # HTTP client with circuit breaker & retries
βββ kafka/ # Kafka producer and consumer implementations
βββ logger/ # Structured logging utilities
βββ server/ # HTTP server setup and middleware
βββ go.mod # Module dependencies
βββ LICENSE # License information
βββ README.md # Project documentation
π¦ Installation
Add the module to your project using:
go get github.com/bignyap/go-utilities
π οΈ Usage Examples
Logger
import (
"github.com/bignyap/go-utilities/logger/factory"
"github.com/bignyap/go-utilities/logger/config"
)
func main() {
logger := factory.GetGlobalLogger()
logger.Info("Application started")
}
HTTP Client
import (
"github.com/bignyap/go-utilities/httpclient"
)
func main() {
client := httpclient.NewHystixClient("https://api.example.com", httpclient.DefaultConfig(), nil)
var response map[string]interface{}
err := client.Get("/endpoint", nil, &response)
if err != nil {
panic(err)
}
}
Kafka Producer
import (
"github.com/bignyap/go-utilities/kafka"
)
func main() {
config := &kafka.LocalConfig{
BrokerSasl: "localhost:9092",
Topic: "example-topic",
}
producer, _ := kafka.NewLocalProducer(config, nil)
defer producer.Close()
producer.SendMessage(map[string]string{"key": "value"})
}
Database
import (
"log"
"github.com/bignyap/go-utilities/database"
)
func main() {
connStr := database.NewConnectionString(
"localhost", "5432", "user", "password", "dbname", nil,
)
db, err := database.NewDatabase(&database.DatabaseConfig{
Name: "main-db", // Optional: useful for logging or multi-DB setups
Driver: "postgres", // Supports: "postgres", "mysql", "sqlite"
ConnectionString: connStr,
// ConnectionPoolConfig: nil, // Optional: uses default pool settings
})
if err != nil {
log.Fatalf("failed to create database: %v", err)
}
if err := db.Connection.Connect(); err != nil {
log.Fatalf("failed to connect: %v", err)
}
defer db.Connection.Close()
log.Println("Database connection established")
}
π License
This project is licensed under the MIT License.
π€ Contributing
We welcome contributions! To contribute:
- Fork the repository.
- Create a feature or fix branch.
- Submit a pull request with a detailed description.
For questions or support, please reach out to the repository owner or open an issue.