go-common

module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT

README ¶

go-common

A collection of production-ready, framework-agnostic Go packages for building backend services.

Go Reference

Packages

Package Description
foundation Core application lifecycle orchestrator (Startup Hooks, Graceful Shutdown, Health Checks)
logger Async structured logger (log/slog) with optional lumberjack file rotation
config Environment variable and configuration file loader (Viper wrapper)
xerrors Structured error handling with HTTP status codes and string codes
recovery Framework-independent structured panic capture with net/http / Fiber / Gin / Echo middleware adapters
redis Redis client wrapper (single / cluster / sentinel) with health-check
nats NATS client wrapper — Core Pub/Sub, JetStream streams/consumers, KV Store
kafka Apache Kafka client wrapper (franz-go) — Producer, Consumer, Admin, TLS/SASL
queue Durable Redis Streams job queue — delays, retries, DLQ, reclaim
queue_nats NATS JetStream job queue — feature parity with Redis queue
queue_kafka Kafka job queue — feature parity with Redis queue, partition key routing
websocket Clustered, framework-agnostic real-time WebSocket server (net/http / Fiber / Gin / Echo)
telemetry OpenTelemetry integration for distributed tracing and metrics via OTLP
db Database abstraction (GORM & MongoDB) with auto-instrumented telemetry
web Web core utilities (standard response, validator, net/http / Fiber / Gin / Echo middleware adapters)
graphql Framework-agnostic GraphQL engine with Generic DataLoader & Telemetry
utils Go Generics toolset (slice, maps, str), Cryptography (AES, ChaCha20, Argon2id, Hashing), and Graceful Shutdown
cache Multi-tier Caching (Ristretto Local + Redis Remote) & Redis Distributed Lock
auth Enterprise Authentication with Encrypted JWT (AES-GCM) and Dynamic Token Binding
httpclient Resilient REST Client with Circuit Breaker (gobreaker) and Auto-Retry
ratelimit Distributed Rate Limiter (Fixed Window) using Redis Lua scripting
cron Distributed Job Scheduler with Redis-backed Leader Election
health Kubernetes-ready health checks (/live, /ready) with concurrent Checkers and HTTP adapters
grpc Pre-configured gRPC server with unified Interceptors (Recovery, Logging, Auth)

Requirements

  • Go 1.26+
  • Redis 6.x+ (Redis 6.2+ recommended for XAUTOCLAIM support)
  • NATS Server 2.10+ with JetStream enabled (-js flag or jetstream: enabled in config)
  • Apache Kafka 2.8+ (or compatible brokers like Redpanda)

Installation

go get github.com/thanhbvha/go-common

Documentation

Each module contains its own detailed README.md with usage examples and API references. Please navigate to the respective package directory to learn more:

HTTP support matrix

Capability Core net/http Fiber Gin Echo
Authentication auth managers Middleware Middleware Middleware Middleware
Request logging / ID logger, ctxkey Middleware Middleware Middleware Middleware
Health probes health.Checker Handler Handler Handler Handler
Rate limiting ratelimit.Limiter Middleware Middleware Middleware Middleware
Responses / recovery / telemetry web primitives Adapter Adapter Adapter Adapter
GraphQL http.Handler Direct Adapter Adapter Adapter
WebSocket core.Conn Adapter Adapter Adapter Adapter

🤖 AI / MCP Integration (Cursor, Cline, VSCode)

This library is fully agentic. We provide an official Model Context Protocol (MCP) server that allows AI coding assistants to instantly fetch and read the latest source code, architectural rules, and boilerplate code directly from both our root modules and the examples/ directory.

This guarantees that the AI writes code adhering to our strict standards (e.g., proper graceful shutdown, structured error handling) and can adapt to the most recent changes in the library without leaving the editor.

Setup for Cursor IDE
  1. Open Cursor Settings -> Features -> MCP
  2. Click + Add New MCP Server
  3. Configure as follows:
    • Type: command
    • Command: npx
    • Args: -y go-common-mcp-server@latest
Setup for VSCode (Cline)

Add this to your cline_mcp_settings.json:

{
  "mcpServers": {
    "go-common": {
      "command": "npx",
      "args": ["-y", "go-common-mcp-server@latest"]
    }
  }
}

License

This project is free and open-source software licensed under the MIT License.

Directories ¶

Path Synopsis
Package auth provides enterprise-grade authentication tools.
Package auth provides enterprise-grade authentication tools.
middleware/echo
Package echoauth provides JWT authentication middleware for Echo.
Package echoauth provides JWT authentication middleware for Echo.
middleware/fiber
Package fiberauth provides JWT authentication middleware for Fiber.
Package fiberauth provides JWT authentication middleware for Fiber.
middleware/gin
Package ginauth provides JWT authentication middleware for Gin.
Package ginauth provides JWT authentication middleware for Gin.
middleware/internal/authhttp
Package authhttp contains framework-neutral HTTP authentication helpers used by the Fiber, Gin, Echo, and net/http middleware adapters.
Package authhttp contains framework-neutral HTTP authentication helpers used by the Fiber, Gin, Echo, and net/http middleware adapters.
middleware/nethttp
Package nethttpauth provides authentication middleware for the standard library's net/http package.
Package nethttpauth provides authentication middleware for the standard library's net/http package.
Package cache provides JSON-aware helper functions built on top of the Cache interface.
Package cache provides JSON-aware helper functions built on top of the Cache interface.
Package config provides a simple, structured way to load application configuration from both files (like .yaml or .env) and environment variables.
Package config provides a simple, structured way to load application configuration from both files (like .yaml or .env) and environment variables.
Package cron provides a distributed job scheduler.
Package cron provides a distributed job scheduler.
db
mongodb
Package mongodb provides a robust, production-ready MongoDB client wrapper.
Package mongodb provides a robust, production-ready MongoDB client wrapper.
orm
Package orm provides a robust, production-ready database client using GORM.
Package orm provides a robust, production-ready database client using GORM.
examples
auth command
cache command
config command
cron command
db command
db_mongodb command
foundation command
graphql_web command
grpc_simple command
health command
httpclient command
kafka command
Package main demonstrates basic usage of the github.com/thanhbvha/go-common/kafka package.
Package main demonstrates basic usage of the github.com/thanhbvha/go-common/kafka package.
logger command
nats command
queue command
queue_kafka command
Package main demonstrates basic usage of the github.com/thanhbvha/go-common/queue_kafka package.
Package main demonstrates basic usage of the github.com/thanhbvha/go-common/queue_kafka package.
queue_nats command
ratelimit command
recovery command
redis command
telemetry command
utils command
web command
websocket command
xerrors command
Package foundation provides the core application lifecycle manager for microservices.
Package foundation provides the core application lifecycle manager for microservices.
Package graphql provides a framework-agnostic wrapper around gqlgen.
Package graphql provides a framework-agnostic wrapper around gqlgen.
adapter/echo
Package echoadapter adapts the GraphQL core HTTP handler to Echo.
Package echoadapter adapts the GraphQL core HTTP handler to Echo.
adapter/fiber
Package fiberadapter adapts the GraphQL core HTTP handler to Fiber.
Package fiberadapter adapts the GraphQL core HTTP handler to Fiber.
adapter/gin
Package ginadapter adapts the GraphQL core HTTP handler to Gin.
Package ginadapter adapts the GraphQL core HTTP handler to Gin.
Package health provides HTTP health check endpoints for microservices.
Package health provides HTTP health check endpoints for microservices.
handler/echo
Package echohealth exposes health check handlers for Echo.
Package echohealth exposes health check handlers for Echo.
handler/fiber
Package fiberhealth exposes health check handlers for Fiber.
Package fiberhealth exposes health check handlers for Fiber.
handler/gin
Package ginhealth exposes health check handlers for Gin.
Package ginhealth exposes health check handlers for Gin.
handler/nethttp
Package nethttp provides health check handlers for the standard library's net/http package.
Package nethttp provides health check handlers for the standard library's net/http package.
Package httpclient provides a resilient HTTP client wrapper around go-resty.
Package httpclient provides a resilient HTTP client wrapper around go-resty.
internal
httpkit
Package httpkit contains internal, framework-independent HTTP primitives shared by the repository's net/http adapters.
Package httpkit contains internal, framework-independent HTTP primitives shared by the repository's net/http adapters.
Package kafka provides a production-ready Apache Kafka client wrapper built on top of github.com/twmb/franz-go.
Package kafka provides a production-ready Apache Kafka client wrapper built on top of github.com/twmb/franz-go.
Package logger provides a structured, asynchronous logger built on top of the standard library's log/slog.
Package logger provides a structured, asynchronous logger built on top of the standard library's log/slog.
middleware/echo
Package echolog provides request ID and access logging middleware for Echo.
Package echolog provides request ID and access logging middleware for Echo.
middleware/fiber
Package fiberlog provides request ID and access logging middleware for Fiber.
Package fiberlog provides request ID and access logging middleware for Fiber.
middleware/gin
Package ginlog provides request ID and access logging middleware for Gin.
Package ginlog provides request ID and access logging middleware for Gin.
middleware/nethttp
Package nethttplog provides request ID and access logging middleware for the standard library's net/http package.
Package nethttplog provides request ID and access logging middleware for the standard library's net/http package.
Package nats provides a production-ready NATS client wrapper built on top of github.com/nats-io/nats.go.
Package nats provides a production-ready NATS client wrapper built on top of github.com/nats-io/nats.go.
Package queue implements a durable, Redis-Streams-backed job queue with support for delayed execution, per-type worker pools, automatic retry with exponential back-off, and a dead-letter queue (DLQ) for exhausted jobs.
Package queue implements a durable, Redis-Streams-backed job queue with support for delayed execution, per-type worker pools, automatic retry with exponential back-off, and a dead-letter queue (DLQ) for exhausted jobs.
Package queue_kafka implements a durable, Apache Kafka-backed job queue with support for delayed execution, per-type worker pools, automatic retry via dedicated retry topics, and a dead-letter queue (DLQ) for exhausted jobs.
Package queue_kafka implements a durable, Apache Kafka-backed job queue with support for delayed execution, per-type worker pools, automatic retry via dedicated retry topics, and a dead-letter queue (DLQ) for exhausted jobs.
Package queue_nats implements a highly reliable, NATS JetStream-backed job queue.
Package queue_nats implements a highly reliable, NATS JetStream-backed job queue.
Package ratelimit provides a distributed rate limiter backed by Redis.
Package ratelimit provides a distributed rate limiter backed by Redis.
middleware/echo
Package echolimit provides rate limiting middleware for Echo.
Package echolimit provides rate limiting middleware for Echo.
middleware/fiber
Package fiberlimit provides rate limiting middleware for Fiber.
Package fiberlimit provides rate limiting middleware for Fiber.
middleware/gin
Package ginlimit provides rate limiting middleware for Gin.
Package ginlimit provides rate limiting middleware for Gin.
middleware/nethttp
Package nethttplimit provides rate limiting middleware for the standard library's net/http package.
Package nethttplimit provides rate limiting middleware for the standard library's net/http package.
Package recovery captures panics as structured, framework-independent reports.
Package recovery captures panics as structured, framework-independent reports.
Package redis provides a production-ready Redis client wrapper built on top of github.com/redis/go-redis/v9.
Package redis provides a production-ready Redis client wrapper built on top of github.com/redis/go-redis/v9.
Package telemetry provides production-ready OpenTelemetry integration.
Package telemetry provides production-ready OpenTelemetry integration.
utils
crypt
Package crypt provides modern, secure cryptographic utilities.
Package crypt provides modern, secure cryptographic utilities.
ctxkey
Package ctxkey provides a strongly-typed mechanism for managing context keys.
Package ctxkey provides a strongly-typed mechanism for managing context keys.
graceful
Package graceful provides utilities for graceful shutdown of services.
Package graceful provides utilities for graceful shutdown of services.
maps
Package maps provides generic utility functions for Go maps.
Package maps provides generic utility functions for Go maps.
slice
Package slice provides generic utility functions for Go slices.
Package slice provides generic utility functions for Go slices.
str
Package str provides utility functions for common string manipulation tasks.
Package str provides utility functions for common string manipulation tasks.
web
middleware/echo
Package echomw provides recovery, telemetry, and error middleware for Echo.
Package echomw provides recovery, telemetry, and error middleware for Echo.
middleware/fiber
package fibermw provides standard HTTP middlewares for the Fiber web framework.
package fibermw provides standard HTTP middlewares for the Fiber web framework.
middleware/gin
Package ginmw provides recovery, telemetry, and error middleware for Gin.
Package ginmw provides recovery, telemetry, and error middleware for Gin.
middleware/internal/recoveryhttp
Package recoveryhttp contains framework-neutral HTTP policy shared by the standard net/http, Fiber, Gin, and Echo panic-recovery adapters.
Package recoveryhttp contains framework-neutral HTTP policy shared by the standard net/http, Fiber, Gin, and Echo panic-recovery adapters.
middleware/nethttp
Package nethttpmw provides panic recovery and OpenTelemetry middleware for the standard library's net/http package.
Package nethttpmw provides panic recovery and OpenTelemetry middleware for the standard library's net/http package.
response
Package response provides a standardized JSON response structure for API endpoints.
Package response provides a standardized JSON response structure for API endpoints.
response/echo
Package echoresp writes standardized API responses for Echo.
Package echoresp writes standardized API responses for Echo.
response/fiber
Package fiberresp writes standardized API responses for Fiber.
Package fiberresp writes standardized API responses for Fiber.
response/gin
Package ginresp writes standardized API responses for Gin.
Package ginresp writes standardized API responses for Gin.
response/nethttp
Package nethttp writes standardized API responses with the standard library's net/http package.
Package nethttp writes standardized API responses with the standard library's net/http package.
validator
Package validator provides struct validation using github.com/go-playground/validator.
Package validator provides struct validation using github.com/go-playground/validator.
websocket
adapter/echo
Package echo provides an adapter to bridge the Echo web framework and our generic WebSocket core.
Package echo provides an adapter to bridge the Echo web framework and our generic WebSocket core.
adapter/fiber
Package fiber provides an adapter to bridge the Fiber web framework and our generic WebSocket core.
Package fiber provides an adapter to bridge the Fiber web framework and our generic WebSocket core.
adapter/gin
Package gin provides an adapter to bridge the Gin web framework and our generic WebSocket core.
Package gin provides an adapter to bridge the Gin web framework and our generic WebSocket core.
adapter/internal/wshttp
Package wshttp contains framework-neutral HTTP policy shared by WebSocket adapters.
Package wshttp contains framework-neutral HTTP policy shared by WebSocket adapters.
adapter/nethttp
Package nethttp provides a WebSocket adapter for the standard library's net/http package.
Package nethttp provides a WebSocket adapter for the standard library's net/http package.
core
package core contains the core WebSocket hub, connection lifecycle, and manager logic.
package core contains the core WebSocket hub, connection lifecycle, and manager logic.
durable
Package durable defines delivery contracts for persistent WebSocket events.
Package durable defines delivery contracts for persistent WebSocket events.
internal/wsrecovery
Package wsrecovery centralizes structured panic reporting for WebSocket runtime goroutines.
Package wsrecovery centralizes structured panic reporting for WebSocket runtime goroutines.
limiter
Package limiter provides token-bucket rate limiting tailored for WebSocket connections.
Package limiter provides token-bucket rate limiting tailored for WebSocket connections.
pool
Package pool provides memory and concurrency pools to optimize resource usage under high traffic.
Package pool provides memory and concurrency pools to optimize resource usage under high traffic.
pubsub
Package pubsub provides a distributed publish-subscribe abstraction for the WebSocket server.
Package pubsub provides a distributed publish-subscribe abstraction for the WebSocket server.
pubsub/redis
package redispubsub handles multi-node coordination using Redis pub/sub to route messages in a clustered environment.
package redispubsub handles multi-node coordination using Redis pub/sub to route messages in a clustered environment.
Package xerrors provides a structured error handling mechanism for HTTP applications.
Package xerrors provides a structured error handling mechanism for HTTP applications.

Jump to

Keyboard shortcuts

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