A comprehensive microservice framework for Go that provides a unified, production-ready foundation for building scalable microservices.
π Complete Documentation | δΈζζζ‘£
- π Complete Server Framework: Unified HTTP and gRPC transport coordination
- π Dependency Injection: Factory-based container with automatic lifecycle management
- π Performance Monitoring: Built-in metrics collection and monitoring hooks
- π Service Discovery: Consul-based registration with health check integration
- π‘οΈ Middleware Stack: Configurable CORS, rate limiting, authentication, and timeouts
- β‘ Protocol Buffers: Full Buf toolchain support for API development
- π Saga Distributed Transactions: Enterprise-grade distributed transaction management with orchestration and choreography patterns
- π± Example Services: Complete reference implementations and usage patterns
pkg/server/- Base server framework and lifecycle managementpkg/transport/- HTTP/gRPC transport coordination layerpkg/middleware/- Configurable middleware stackpkg/discovery/- Service discovery integrationpkg/saga/- Distributed transaction orchestration and state management
examples/- Simple examples for getting startedinternal/switserve/- User management reference serviceinternal/switauth/- Authentication service with JWTinternal/switctl/- CLI tool implementation
- Go 1.23.12+ with generics support
- Optional: MySQL 8.0+, Redis 6.0+, Consul 1.12+ (for examples)
- Development: Buf CLI, Docker, Make
git clone https://github.com/innovationmech/swit.git
cd swit
go mod downloadpackage main
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
"github.com/innovationmech/swit/pkg/server"
)
type MyService struct{}
func (s *MyService) RegisterServices(registry server.BusinessServiceRegistry) error {
return registry.RegisterBusinessHTTPHandler(&MyHTTPHandler{})
}
type MyHTTPHandler struct{}
func (h *MyHTTPHandler) RegisterRoutes(router interface{}) error {
ginRouter := router.(*gin.Engine)
ginRouter.GET("/hello", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Hello from Swit!"})
})
return nil
}
func (h *MyHTTPHandler) GetServiceName() string {
return "my-service"
}
func main() {
config := &server.ServerConfig{
ServiceName: "my-service",
HTTP: server.HTTPConfig{Port: "8080", Enabled: true},
GRPC: server.GRPCConfig{Enabled: false},
}
service := &MyService{}
baseServer, _ := server.NewBusinessServerCore(config, service, nil)
ctx := context.Background()
baseServer.Start(ctx)
defer baseServer.Shutdown()
select {} // Keep running
}go run main.go
curl http://localhost:8080/helloSwit provides enterprise-grade distributed transaction management using the Saga pattern.
package main
import (
"context"
"github.com/innovationmech/swit/pkg/saga"
"github.com/innovationmech/swit/pkg/saga/base"
)
func main() {
// Create Saga definition
def := saga.NewSagaDefinition("order-saga", "v1")
// Add steps with compensation
def.AddStep("reserve-inventory", reserveInventory, compensateInventory)
def.AddStep("process-payment", processPayment, refundPayment)
def.AddStep("create-order", createOrder, cancelOrder)
// Create coordinator
coordinator := saga.NewCoordinator(storage, publisher)
// Execute Saga
instance, err := coordinator.Execute(context.Background(), def, orderData)
if err != nil {
// Saga failed, compensations executed automatically
}
}- Orchestration & Choreography: Support for centralized and event-driven patterns
- Reliable State Management: PostgreSQL, MySQL, SQLite, and in-memory storage
- Flexible Retry Strategies: Exponential backoff, fixed delay, linear backoff
- Compensation Patterns: Sequential, parallel, and custom compensation
- DSL Support: YAML-based workflow definition
- Dashboard: Web UI for monitoring and management
- Security: Authentication, RBAC, ACL, and data encryption
- Observability: Prometheus metrics, OpenTelemetry tracing, health checks
- π User Guide - Getting started and core concepts
- π API Reference - Complete API documentation
- π Tutorials - Step-by-step guides and best practices
- π Deployment Guide - Production deployment
- π§ Developer Guide - Architecture and extension development
# HTTP service
cd examples/simple-http-service && go run main.go
# gRPC service
cd examples/grpc-service && go run main.go
# Full-featured service
cd examples/full-featured-service && go run main.go# Build all services
make build
# Run services
./bin/swit-serve # User management (HTTP: 9000, gRPC: 10000)
./bin/swit-auth # Authentication (HTTP: 9001, gRPC: 50051)
./bin/switctl --help # CLI tool
./bin/saga-migrate --help # Database migration tool
./bin/saga-dsl-validate --help # Saga DSL validation tool# Saga orchestrator example
cd examples/saga-orchestrator && go run main.go
# Saga choreography example
cd examples/saga-choreography && go run main.go
# Saga with publisher
cd examples/saga-publisher && go run main.go
# Saga retry patterns
cd examples/saga-retry && go run main.goThe saga-migrate tool manages Saga database schema migrations:
# Apply all migrations
saga-migrate -dsn 'postgres://localhost/saga' -action migrate
# Check migration status
saga-migrate -dsn 'postgres://localhost/saga' -action status
# Apply specific version
saga-migrate -dsn 'postgres://localhost/saga' -action apply -version 2
# Rollback migration
saga-migrate -dsn 'postgres://localhost/saga' -action rollback -version 2
# Validate schema version
saga-migrate -dsn 'postgres://localhost/saga' -action validate -version 2
# See full documentation
cat docs/saga-database-migrations.md# Complete setup
make setup-dev
# Quick setup
make setup-quick# Build
make build # Full build
make build-dev # Quick build
# Test
make test # All tests
make test-dev # Quick tests
make test-coverage # Coverage report
# API Development
make proto # Generate protobuf code
make swagger # Generate API docs
# Code Quality
make tidy # Tidy modules
make format # Format code
make quality # Quality checks# Build images
make docker
# Run with Docker Compose
docker-compose up -d
# Or run individually
docker run -p 9000:9000 -p 10000:10000 swit-serve:latest
docker run -p 9001:9001 swit-auth:latest- Fork the repository
- Set up development environment:
make setup-dev - Run tests:
make test - Make changes following existing patterns
- Add tests for new functionality
- Submit a pull request
Read our Code of Conduct before contributing.
MIT License - See LICENSE file for details
For complete documentation, examples, and advanced usage, visit innovationmech.github.io/swit