go-utilities

module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2025 License: MIT

README ΒΆ

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:

  1. Fork the repository.
  2. Create a feature or fix branch.
  3. Submit a pull request with a detailed description.

πŸ“¬ Contact

For questions or support, please reach out to the repository owner or open an issue.

Directories ΒΆ

Path Synopsis
examples
logger command
server command
logger
api

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL