Production-ready infrastructure components for Go applications.
go-infrastructure provides battle-tested, modular components to build robust Go services: dependency injection with context support, live-reloading configuration, structured logging, comprehensive error handling, type-safe events, HTTP/HTTPS servers, database persistence, and more.
Type-safe - Leverages Go generics for compile-time safety
Modular - Use only what you need, no forced dependencies
Production-ready - Battle-tested patterns and error handling
Context-aware - First-class support for context.Context
Well-documented - Comprehensive docs and examples
Actively maintained - Regular updates and security patches
| Module | Description | Key Features |
|---|---|---|
| Dependency Injection | Type-safe DI container | Context support, lifetimes (Singleton/Scoped/Transient), generics, multi-tenant |
| Configuration | Dynamic configuration | Live reload, file-based, change events, freeze/restore |
| Logging | Structured logging | Multiple levels, console/file output, daily rotation |
| Error Handling | Centralized error management | Error catching, validation, try-catch patterns |
| Events Manager | Type-safe pub/sub | Generic-based, parallel/sequential, no reflection |
| Server | HTTP/HTTPS helpers | Listener builder, SPA support, graceful shutdown |
| Persistence | Database abstraction | GORM integration, typed access, MySQL/PostgreSQL/SQLite/SQL Server |
| Crypto | Encryption utilities | AES-256, secure key management |
| Disk | File system utilities | File watching, change notifications, path helpers |
go get github.com/janmbaco/go-infrastructure/v2Requirements:
- Go 1.24 or higher
- Go modules enabled
A minimal example using a DI container and file-based configuration with automatic reload:
package main import ( "fmt" di "github.com/janmbaco/go-infrastructure/v2/dependencyinjection" cfgioc "github.com/janmbaco/go-infrastructure/v2/configuration/fileconfig/ioc" cfgresolver "github.com/janmbaco/go-infrastructure/v2/configuration/fileconfig/ioc/resolver" logsioc "github.com/janmbaco/go-infrastructure/v2/logs/ioc" errsioc "github.com/janmbaco/go-infrastructure/v2/errors/ioc" eventsioc "github.com/janmbaco/go-infrastructure/v2/eventsmanager/ioc" diskioc "github.com/janmbaco/go-infrastructure/v2/disk/ioc" ) type Config struct { Database string `json:"database"` Port int `json:"port"` } func main() { container := di.NewBuilder(). AddModule(logsioc.NewLogsModule()). AddModule(errsioc.NewErrorsModule()). AddModule(eventsioc.NewEventsModule()). AddModule(diskioc.NewDiskModule()). AddModule(cfgioc.NewConfigurationModule()). MustBuild() resolver := container.Resolver() handler := cfgresolver.GetFileConfigHandler( resolver, "config.json", &Config{Port: 8080}, // defaults if file is missing ) cfg := handler.GetConfig().(*Config) fmt.Printf("App running on port %d\n", cfg.Port) // The process keeps running and picks up changes to config.json automatically. select {} }config.json:
{ "database": "postgres://localhost/myapp", "port": 8080 }Full-featured DI container with Go generics and context support.
container := di.NewBuilder(). Register(func(r di.Register) { // Context-aware provider r.AsScope(new(*Service), func(ctx context.Context, logger logs.Logger) *Service { return &Service{ Logger: logger, RequestID: ctx.Value("requestID").(string), } }, nil) }). MustBuild() // Resolve with context ctx := context.WithValue(context.Background(), "requestID", "req-123") service := di.ResolveCtx[*Service](ctx, container.Resolver())Features: Lifetimes (Type/Scoped/Singleton/Tenant), context propagation, automatic cancellation, generic helpers
Full Documentation | Context Example
File-based configuration with live reload and zero downtime.
type Config struct { Database string `json:"database"` Port int `json:"port"` } handler := cfgresolver.GetFileConfigHandler(resolver, "config.json", &Config{Port: 8080}) cfg := handler.GetConfig().(*Config) // Changes to config.json are automatically detected and appliedFeatures: Auto-reload, change events, freeze/restore, validation
Full Documentation
Structured logging with multiple outputs and daily rotation.
logger := logs.NewLogger() logger.SetConsoleLevel(logs.InfoLevel) logger.SetFileLogLevel(logs.TraceLevel) logger.Info("Application started", "version", "2.0.0") logger.WithFields(map[string]interface{}{ "userID": "123", "action": "login", }).Info("User action")Features: Multiple levels, console/file output, daily rotation, structured fields
Full Documentation
Comprehensive error handling with try-catch patterns.
errorCatcher := errors.NewErrorCatcher(logger) err := errorCatcher.TryCatchError( func() error { return riskyOperation() }, func(err error) error { logger.Error("Operation failed", err) return fmt.Errorf("wrapped: %w", err) }, )Features: Error catching, validation, finally blocks, error wrapping
Full Documentation
Type-safe publish-subscribe with generics.
type UserCreatedEvent struct { UserID string Email string } eventManager := eventsmanager.NewEventManager() publisher := eventsmanager.NewPublisher[UserCreatedEvent](eventManager) subscriptions := eventsmanager.NewSubscriptions[UserCreatedEvent](errorHandler) subscriptions.Subscribe(func(event UserCreatedEvent) { fmt.Printf("User created: %s\n", event.Email) }) publisher.Publish(UserCreatedEvent{UserID: "123", Email: "user@example.com"})Features: Type-safe, parallel/sequential execution, no reflection overhead
Full Documentation
HTTP/HTTPS server with SPA support and graceful shutdown.
listener := server.NewListenerBuilder(). SetPort(":8080"). SetTLSConfig(tlsConfig). SetHandler(myHandler). Build() if err := listener.ListenAndServe(); err != nil { log.Fatal(err) }Features: TLS support, SPA routing, graceful shutdown, port conflict recovery
Full Documentation
Type-safe database access with GORM.
db := persistence.NewDB(dbInfo, errorCatcher) dataAccess := dataaccess.NewDataAccess[User](db) users, err := dataAccess.SelectRows(&User{Active: true}) err = dataAccess.InsertRow(&User{Name: "John"}) err = dataAccess.UpdateRow(&User{ID: 1}, map[string]interface{}{"Name": "Jane"})Features: Typed operations, multiple DB backends, preloading, associations
Full Documentation
Complete example demonstrating all modules working together.
Features:
- Serves static files with SPA routing
- Live configuration reload (no restarts needed)
- Structured logging with rotation
- Graceful shutdown and error recovery
- Docker support for containerized deployments
# Run with Go go run ./cmd/singlepageapp -port :8080 -static ./dist -index index.html # Or with Docker docker build -f server/facades/Dockerfile -t myapp . docker run -p 8080:8080 myappView Full Example | Dockerfile
We take security seriously. If you discover a security vulnerability:
- DO NOT open a public issue
- Report it via GitHub Security Advisories
- See our Security Policy for details
We welcome contributions! Whether it's:
- Bug reports
- Feature requests
- Documentation improvements
- Code contributions
Getting Started:
- Read the Contributing Guide
- Check the Style Guide
- Review the Code of Conduct
# Fork and clone git clone https://github.com/YOUR-USERNAME/go-infrastructure.git # Create a feature branch git checkout -b feature/amazing-feature # Make changes and run tests go test ./... # Submit a PR- CHANGELOG - Version history and changes
- UPGRADE GUIDE (v2.0) - Migration from v1.x to v2.x
- SECURITY - Security policy and best practices
- STYLE GUIDE - Coding standards and conventions
- CONTRIBUTING - How to contribute
- CODE OF CONDUCT - Community guidelines
If go-infrastructure helps you build better Go services, consider supporting its development:
Your sponsorship helps:
- Maintain and improve the project
- Create more documentation and examples
- Fix bugs and security issues faster
- Develop new features
Sponsors will be recognized here and in release notes.
- Version: v2.0.0+
- Go Version: 1.24+
- License: Apache 2.0
- Test Coverage: 80%+
- Actively Maintained: Yes
Built with care for the Go community.
Special thanks to all contributors who have helped improve this project.
This project is licensed under the Apache License 2.0 – see the LICENSE file for details.
- GitHub Discussions - Ask questions, share ideas
- Issue Tracker - Report bugs, request features
- Contact maintainers through GitHub
Made with Go | Star us on GitHub | Become a Sponsor