A collection of common modules and utilities for building Go applications with Uber FX dependency injection framework. This library provides ready-to-use FX modules for configuration, logging, database connections, HTTP servers, and AMQP messaging.
This library requires Go 1.24 or later.
go get github.com/CodeLieutenant/uberfx-common/v3
The configfx
module provides configuration management using Viper, integrated with Uber FX.
Features:
- Type-safe configuration with generics
- Automatic loading from standard locations
- Easy integration with FX dependency injection
Example:
type AppConfig struct {
Port int `mapstructure:"port" yaml:"port" default:"8080"`
LogLevel string `mapstructure:"log_level" yaml:"log_level" default:"info"`
}
func main() {
app := fx.New(
// Load configuration
fx.Provide(func() (AppConfig, error) {
return configfx.New[AppConfig]("myapp")
}),
// Create a module with the loaded configuration
fx.Invoke(func(cfg AppConfig) {
// Use configuration
}),
)
app.Run()
}
The loggerfx
module provides logging functionality using zerolog, integrated with Uber FX.
Features:
- Multiple output sinks (stdout, stderr, file, buffered I/O)
- Pretty printing option for development
- Proper lifecycle management
Example:
func main() {
app := fx.New(
// Configure logging
loggerfx.ZerologModule(loggerfx.Sink{
Level: "info",
Type: loggerfx.Stdout,
PrettyPrint: true,
}),
// Use the logger
fx.Invoke(func(log zerolog.Logger) {
log.Info().Msg("Application started")
}),
)
app.Run()
}
The databasesfx
module provides PostgreSQL database integration using pgx, with support for migrations via golang-migrate.
Features:
- Connection pooling
- Configuration via struct
- Database migrations
- Proper lifecycle management
Example:
func main() {
app := fx.New(
// Configure and provide PostgreSQL connection
databasesfx.PostgresModule(databasesfx.PostgresConfig{
ApplicationName: "myapp",
DBName: "mydatabase",
Host: "localhost",
Port: 5432,
Username: "postgres",
Password: "password",
MaxOpenConnections: 10,
// ... other configuration options
}),
// Use the database connection
fx.Invoke(func(pool *pgxpool.Pool) {
// Use the connection pool
}),
)
app.Run()
}
The http/fiber/fiberfx
module provides integration with the Fiber web framework.
Features:
- Easy route definition
- Middleware support
- Proper lifecycle management
- Type-safe handler registration
Example:
func HelloHandler(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
}
func main() {
app := fx.New(
// Create the Fiber app
fiberfx.App(
"myapp",
fiberfx.Routes(
[]fiberfx.RouteFx{
fiberfx.Get("/hello", HelloHandler),
fiberfx.Get("/users/:id", UserHandler),
},
),
),
// Run the app
fiberfx.RunApp(":3000", "myapp", 5*time.Second),
)
app.Run()
}
The amqpfx
module provides integration with AMQP (RabbitMQ) for messaging, using go-amqp.
Features:
- Consumer and publisher support
- Type-safe message handling with generics
- Proper lifecycle management
- Multiple consumer types (function-based, interface-based, raw)
Example:
type Message struct {
Content string `json:"content"`
}
func handleMessage(ctx context.Context, msg Message) error {
// Process the message
return nil
}
func main() {
app := fx.New(
// Configure and provide AMQP consumer
amqpfx.ConsumerModuleFunc(
handleMessage,
consumer.QueueDeclare{
QueueName: "my-queue",
Durable: true,
},
connection.Config{
ConnectionName: "my-connection",
URI: "amqp://guest:guest@localhost:5672/",
},
),
// Configure and provide AMQP publisher
amqpfx.PublisherModule[Message](
connection.Config{
ConnectionName: "my-connection",
URI: "amqp://guest:guest@localhost:5672/",
},
"my-exchange",
),
// Use the publisher
fx.Invoke(func(pub *publisher.Publisher[Message]) {
// Publish messages
}),
)
app.Run()
}
The repository includes several examples demonstrating how to use the various modules:
- Basic Fiber Example: A simple HTTP server using Fiber
- Middleware Example: Using middleware with Fiber
- Route Middleware Example: Applying middleware to specific routes
- Middleware with Dependencies Example: Using middleware with FX dependencies
This project is licensed under the Apache License 2.0.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request