go-ext is a compact toolkit for building HTTP services in Go. It provides configuration
management, structured logging, a lightweight REST framework based on Gin, and
database initialization helpers for GORM (MySQL and SQLite).
-
Configuration management
- Supports multiple formats (YAML, JSON, TOML) and environment variables with the
extprefix (e.g.EXT_SERVER_BINDADDR) - Uses
mapstructurefor structured custom configuration - Built-in validation and functional options for flexible initialization
- Supports multiple formats (YAML, JSON, TOML) and environment variables with the
-
Structured logging
- Built on Go's
log/slogwith optional file rotation vialumberjack - Automatically extracts OpenTelemetry trace/span IDs when available
- Supports multiple levels (Debug, Info, Warn, Error, Fatal)
- Configurable output format (
textorjson) and output target (stdout or file)
- Built on Go's
-
RESTful utilities
- Built with Gin for high-performance HTTP APIs
- Middleware support for tracing and metrics
-
Storage helpers
- Provides a GORM-based
NewDBinitializer (returns(*gorm.DB, error)) for MySQL and SQLite - Connection pool configuration (MaxIdleConns, MaxOpenConns)
- Provides a GORM-based
go get github.com/fize/go-extUse the configuration helpers in the config package and create a GORM *gorm.DB via the storage package:
import (
"github.com/fize/go-ext/config"
"github.com/fize/go-ext/storage"
)
cfg, err := config.NewSQLConfig(
config.WithType("mysql"),
config.WithHost("localhost:3306"),
config.WithUser("root"),
config.WithPassword("password"),
config.WithDB("myapp"),
)
if err != nil {
panic(err)
}
db, err := storage.NewDB(cfg)
if err != nil {
panic(err)
}
// Use db.WithContext(ctx).Create(&model) etc.import "github.com/fize/go-ext/config"
cfg := config.NewConfig()
if err := cfg.Load("config.yaml", false); err != nil {
panic(err)
}import "github.com/fize/go-ext/ginserver"
cfg := config.NewConfig()
cfg.Server.BindAddr = ":8080"
server, err := ginserver.NewServer(cfg)
if err != nil {
panic(err)
}
server.Engine.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "pong"})
})
// Run with graceful shutdown
ctx, cancel, err := server.RunWithContext()
if err != nil {
panic(err)
}
defer cancel()Use the logging helpers provided by the log package (package-level helpers and a default logger):
import "github.com/fize/go-ext/log"
log.Info("Starting application...")
log.Debug("Connected to database", "db", "myapp")Run unit tests with:
go test ./...- The
storagepackage exposes aNewDBinitializer that returns(*gorm.DB, error)for better error handling. - The
configpackage'sLoad()method now returnserrorinstead of panicking. - The
ginserverpackage introduces a newServerinstance mode withNewServer()for better multi-service support. - Response/request helpers previously provided by the
ginserverpackage have been removed; projects should define their own API request/response formats.
config.BaseConfig.Load()- now returnserrorstorage.NewDB()- now returns(*gorm.DB, error)ginserver.InitGinServer()- now returns(*gin.Engine, *log.Logger, error)
If you want help migrating code, check the _example directory for updated examples.
A set of small runnable examples is provided under the _example directory:
./_example/config— demonstrates loading configuration fromtestdata/config.yamland parsing custom sections../_example/log— shows basic usage of the default logger and context-aware logging../_example/ginserver— minimalRestControllerexample registered with theRestfulAPI../_example/middleware— demonstrates attachingGinLoggerandGinRecoverymiddleware to a Gin engine../_example/storage— creates an in-memory SQLite*gorm.DBviastorage.NewDB../_example/server— demonstrates the newServerinstance mode with graceful shutdown.
Run an example with go run, for example:
go run ./_example/ginserver
go run ./_example/serverNote: some examples start HTTP servers and will block the terminal. Use separate terminals or build binaries with go build if needed.