The mini/ package is a lightweight yet powerful foundation for building modular, event-driven microservices in Go. It provides standardized messaging, in-process transport over NSQ, dynamic routing, structured logging, service discovery, and resilient panic-safe execution.
mini/
├── codec/ # Typed messages (Message, IMessage)
├── config/ # JSON+ENV config loader with fallbacks
├── constant/ # Shared constants and error types
├── context/ # Request lifecycle and response tracking
├── logger/ # Structured and contextual logger
├── recover/ # Safe execution utilities
├── registry/ # In-memory service registry
├── router/ # Declarative message routing
├── selector/ # Service node selection strategies
└── transport/ # NSQ-based message transport with file supportBuilt on top of NSQ and abstracted via ITransport. Includes:
Publish,Request,Respond,Broadcast- Topic and prefix subscriptions (
SubscribeTopic,SubscribePrefix) - Retry policies per topic/subject
- Middleware support (context-aware)
- File chunking (
SendFile,ReceiveFileWithHooks)
Backed by a flexible Conn layer for producer/consumer + reply channels.
Standard message format used throughout the system:
- Core fields:
Type,Node,ContextID,ReplyTo,Headers,Body - Type-safe accessors:
GetString,GetInt,GetBool, etc. SetError,SetResult,Validate,CopyRawBodysupport for low-level access- Interface:
IMessage
- Message types:
request,response,stream,event,health - Errors:
ErrNotFound,ErrEmptyMessage,ErrInvalidPath - Special keys:
error,result,input - Limits:
MaxFileChunkSize = 2MB - Health status:
StatusOK,StatusWarning,StatusCritical
Maps ContextID → Conversation with support for:
- Response tracking for
Request/Respondpairs - Automatic TTL cleanup and lifecycle hooks
- Methods:
Add,Get,Done,WaitTimeout,Range
Used internally to ensure responses are correctly routed.
- Supports JSON files with
${ENV_VAR}interpolation - Env variable fallbacks (e.g.
SRV_LOG_LEVEL) - Methods:
MustString,MustInt,Has,Dump - Automatically injects defaults for missing values
- Contextual and leveled:
Debug,Info,Warn,Error - Add metadata:
With(key, value),WithContext(traceID) - Interfaces:
ILogger,LoggerEntry - Configurable log level:
SetLevel("warn")
In-memory registry with optional plugin support:
- Register/Deregister services and nodes
- TTL-based cleanup
- Watchers for live updates
- Introspectable service state
- Node selection strategies:
RoundRobin,Random,First - Metadata-based filtering
- Internal caching (
cacheTTL) for faster resolution
- Declarative routing via
IActionandIRouter - Register handlers dynamically
- Middleware support:
HandlerWrapper - Input validation: required fields, type checks, custom rules
- Hooks:
OnErrorHook,OnNotFound
RecoverWithContext()for panic-resilient routingSafe("label", fn)for safe goroutines- Global panic hook
- Keeps the system alive even on handler failure
- Built-in counters:
IncMetric,AddMetric,SetMetric - Snapshot:
ExportMetrics()asmap[string]float64 - Scoped recording:
.WithMetricPrefix("db.")
-
Uses
gopsutilto monitor:- Memory (free %, thresholds)
- CPU load (load5 per core)
-
Thresholds configurable via
config -
Register custom health probes with
RegisterHealthProbe
log := logger.NewLogger("auth", "debug")
bus := transport.New(
transport.Addrs("127.0.0.1:4150"),
transport.Subject("auth.v1"),
transport.WithLogger(log),
transport.WithDebug(),
)
svc := service.NewService("auth", "v1",
service.Transport(bus),
service.Logger(log),
)
svc.RegisterAction("auth.login", []service.InputSchemaField{
{Name: "email", Type: "string", Required: true},
{Name: "password", Type: "string", Required: true},
}, func(ctx context.Context, input map[string]any) (any, error) {
return map[string]any{"token": "jwt123"}, nil
})
_ = svc.Init()
_ = svc.Run()- In-process, type-safe transport using NSQ
- Schema-based request validation and introspection
- Middleware chaining for actions and handlers
- File transfer over pub/sub (chunked)
- Dynamic service discovery and routing
- Built-in metrics, health checks, and error recovery
- Internal service-to-service communication
- Background workers (async
Request/Respond) - File processing pipelines
- Gateway → Bus → Worker architecture
- Auth, logging, streaming, task queues