A comprehensive Go toolkit for building modern web applications, providing HTTP and gRPC server management, caching, database integration, and logging utilities.
- Server Management: Unified lifecycle management for HTTP and gRPC servers with graceful shutdown
- HTTP Server: Powered by Echo framework with middleware support, validation, and rendering
- gRPC Server: Easy-to-use gRPC server implementation with graceful shutdown
- Caching: Redis integration for distributed caching
- Database: GORM integration for database operations
- Logging: Structured logging utilities
go get github.com/zuozhehao/gowebkitThe server package provides a unified way to manage multiple servers with coordinated lifecycle:
import "github.com/zuozhehao/gowebkit/server"
// Create server manager
manager := server.New(5*time.Second, httpServer, grpcServer)
// Run servers and block until shutdown
if err := manager.Run(ctx); err != nil {
log.Fatal(err)
}The httpx package wraps Echo framework with additional convenience features:
import "github.com/zuozhehao/gowebkit/server/httpx"
// Create HTTP server
httpServer := httpx.New(":8080",
httpx.WithMiddleware(middleware.Logger(), middleware.Recover()),
httpx.WithStatic("/static", "public"),
)
// Define routes
httpServer.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})The grpcx package provides a wrapper for gRPC servers:
import "github.com/zuozhehao/gowebkit/server/grpcx"
// Create gRPC server
grpcServer := grpcx.New(":9090")
// Register services
pb.RegisterYourServiceServer(grpcServer, &yourService{})Redis cache implementation for distributed caching:
import "github.com/zuozhehao/gowebkit/cache/redis"
// Create Redis client
client, err := redis.New(ctx, "redis://localhost:6379/0")
if err != nil {
return err
}
defer client.Close()
// Set value
err := client.Set(ctx, "key", "value", 10*time.Minute)
// Get value
value, err := client.Get(ctx, "key")Database utilities with GORM integration:
import "github.com/zuozhehao/gowebkit/database"
import "gorm.io/driver/mysql"
// Initialize database
db, err := database.New(
ctx,
mysql.Open("user:password@tcp(localhost:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"),
)
if err != nil {
return err
}
defer db.Close()
// Use GORM for operations
db.AutoMigrate(&User{})Structured logging utilities:
import "github.com/zuozhehao/gowebkit/logger"
import "log/slog"
// Create logger
log := logger.New(
logger.NewTextHandler(slog.LevelInfo),
)
// Log messages
log.Info("Server started", "port", 8080)
log.Error("Failed to connect", "error", err)gowebkit/
├── cache/ # Caching utilities
│ └── redis/ # Redis cache implementation
├── database/ # Database utilities
│ ├── database.go # GORM database initialization
│ └── logger.go # GORM-compatible logger
├── logger/ # Logging utilities
│ └── logger.go # slog helpers
├── server/ # Server management
│ ├── grpcx/ # gRPC server implementation
│ ├── httpx/ # HTTP server implementation
│ │ ├── middleware.go # HTTP middleware helpers
│ │ ├── server.go # Echo server wrapper
│ │ └── template.go # Template renderer
│ └── server.go # Server manager
├── go.mod # Go module file
└── README.md # This file
- Echo v5 - HTTP framework
- Redis Go Client - Redis client
- gRPC - gRPC framework
- GORM - ORM framework
- sync - Synchronization utilities
package main
import (
"context"
"log"
"net/http"
"os/signal"
"syscall"
"time"
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
"github.com/zuozhehao/gowebkit/server"
"github.com/zuozhehao/gowebkit/server/grpcx"
"github.com/zuozhehao/gowebkit/server/httpx"
)
func main() {
// Create HTTP server
httpServer := httpx.New(":8080",
httpx.WithMiddleware(middleware.Logger(), middleware.Recover()),
)
// Define HTTP routes
httpServer.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello from HTTP!")
})
// Create gRPC server
grpcServer := grpcx.New(":9090")
// Register gRPC services here
// pb.RegisterYourServiceServer(grpcServer, &yourService{})
// Create server manager
manager := server.New(5*time.Second, httpServer, grpcServer)
// Create context with cancellation
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
// Run servers
log.Println("Starting servers...")
if err := manager.Run(ctx); err != nil {
log.Fatalf("Server error: %v", err)
}
log.Println("Servers stopped gracefully")
}MIT License
Contributions are welcome! Please feel free to submit a Pull Request.