A scalable, modular Golang application built with Echo framework, GORM ORM, and Domain-Driven Design principles. This project features a dynamic module registration system that allows adding new functionality without modifying the core application.
- Modular Architecture: Each feature is contained in its own module with clear boundaries
- Dynamic Module Binding: Modules are registered at runtime and loaded automatically
- Domain-Driven Design: Clean separation of domain, application, and infrastructure layers
- RESTful API: Built with Echo framework for high performance
- Database Support: MySQL integration with GORM
- Docker Support: Ready for containerized deployment
- Comprehensive Logging: Module-aware logging system
The application currently includes the following modules for examples:
- User Module:
- User management functionality
- CRUD operations for user accounts
- Go 1.20 or higher
- MySQL 8.0 or higher
- Docker and Docker Compose (for containerized setup)
The easiest way to run the application is using Docker Compose:
-
Clone the repository:
git clone https://github.com/zakirkun/go-modular-boilerplate.git cd go-modular-boilerplate
-
Make the helper scripts executable:
chmod +x run.sh cleanup.sh
-
Start the application:
./run.sh
-
The API will be available at http://localhost:8080
-
To stop the application:
docker-compose down
-
Clone the repository:
git clone https://github.com/zakirkun/go-modular-boilerplate.git cd go-modular-boilerplate
-
Install dependencies:
go mod download
-
Set up your environment variables (copy from .env.example):
config.toml # Edit config.toml file with your local configuration
-
Run the application:
go run main.go
GET /api/users
: Get all usersGET /api/users/:id
: Get a user by IDPOST /api/users
: Create a new userPUT /api/users/:id
: Update a userDELETE /api/users/:id
: Delete a user
LOG_LEVEL
: Logging level (DEBUG, INFO, WARN, ERROR, OFF) (default: "INFO")
To create a new module:
- Create a new directory under
modules
- Implement the module interface defined in
internal/app/module.go
- Register the module in
main.go
Example of minimal module implementation:
package mymodule
import (
"github.com/labstack/echo/v4"
"go-modular-boilerplate/your-project/pkg/logger"
"gorm.io/gorm"
)
type Module struct {
db *gorm.DB
logger *logger.Logger
}
func (m *Module) Name() string {
return "mymodule"
}
func (m *Module) Initialize(db *gorm.DB, log *logger.Logger) error {
m.db = db
m.logger = log
m.logger.Info("My module initialized")
return nil
}
func (m *Module) RegisterRoutes(e *echo.Echo, basePath string) {
// Register your routes here
}
func (m *Module) Migrations() []interface{} {
return []interface{}{
// Your entity structs for migration
}
}
func (m *Module) Logger() *logger.Logger {
return m.logger
}
func NewModule() *Module {
return &Module{}
}
The application includes:
Dockerfile
: Multi-stage build for the Go applicationdocker-compose.yml
: Configuration for the app and MySQLinit.sql
: Database initialization script- Helper scripts:
run.sh
: Start the application with Docker Composecleanup.sh
: Clean up Docker resources
The application uses a custom logging system that:
- Supports multiple log levels (DEBUG, INFO, WARN, ERROR, OFF)
- Includes timestamps and module names in log entries
- Creates module-specific loggers
- Can be configured globally via environment variables
Example log output:
2025-03-17 14:30:05.123 [app] INFO: Registered module: user
2025-03-17 14:30:05.125 [user] INFO: Initializing user module
2025-03-17 14:30:05.130 [user] INFO: User module initialized successfully
This project is licensed under the MIT License - see the LICENSE file for details.