Common solution collection for quickly setting up Golang web applications. This library provides a modular plugin architecture with factory functions and config-driven initialization for common web application components.
Full API Documentation on pkg.go.dev β
go get github.com/poly-workshop/go-webmodspackage main
import (
"log/slog"
"github.com/poly-workshop/go-webmods/app"
gormclient "github.com/poly-workshop/go-webmods/gormclient"
)
func main() {
// Initialize application with configuration
app.SetCMDName("myapp")
app.Init(".") // Loads config from ./configs/
// Initialize database
db := gormclient.NewDB(gormclient.Config{
Driver: "postgres",
Host: "localhost",
Port: 5432,
Username: "user",
Password: "password",
DbName: "mydb",
SSLMode: "disable",
})
slog.Info("Application started successfully")
_ = db
}Core application utilities including:
- Layered configuration management (Viper)
- Structured logging with context propagation (slog)
- Application initialization helpers
Database client factory supporting:
- PostgreSQL
- MySQL
- SQLite
- Connection pooling configuration
MongoDB client factory using the v2 driver:
- MongoDB Atlas support
- Connection pooling and timeouts
- Ping verification on startup
Redis client with:
- Single-node and cluster mode support
- Two-level caching (local + distributed)
- Automatic cache invalidation via pub/sub
Kafka client helpers with:
- Reader factory (consumer group or partition)
- Writer factory with configurable batching and acknowledgements
Unified object storage interface supporting:
- Local filesystem
- MinIO / S3-compatible storage
- Volcengine TOS
gRPC server interceptors for:
- Structured logging
- Request ID generation and propagation
- Context-aware tracing
SMTP email client with:
- TLS support
- HTML and plain text emails
- Multiple recipients support
Every component uses a Config struct and factory function:
component := package.NewComponent(package.Config{...})Multi-backend support with unified interfaces:
storage, err := objectstorage.NewObjectStorage(objectstorage.Config{
ProviderType: objectstorage.ProviderLocal,
ProviderConfig: objectstorage.ProviderConfig{
BasePath: "/data",
},
})Structured logging with automatic context propagation:
ctx = app.WithLogAttrs(ctx, slog.String("user_id", "123"))
slog.InfoContext(ctx, "User logged in") // Includes user_id automaticallyConfiguration uses Viper with layered loading:
configs/default.yaml- Base configurationconfigs/{MODE}.yaml- Environment-specific overrides- Environment variables - Final overrides (using
__separator)
Example configs/default.yaml:
log:
level: info
format: tint
database:
driver: postgres
host: localhost
port: 5432
username: user
password: pass
name: mydb
sslmode: disableSet environment mode:
export MODE=production # Loads configs/production.yamlOverride with environment variables:
export LOG__LEVEL=debug
export DATABASE__HOST=prod-dbSee the examples in pkg.go.dev for detailed usage examples of each package.
go test ./...make fmtmake lintThis repo is granted under the MIT License. Feel free to use it in your projects.