1.3 KiB
1.3 KiB
Adding a New Module
Use the built-in generator to create a new module following Clean Architecture.
The Fast Way
make create-module type=app name=projects
This command:
- Creates
src/app/projects/folder structure. - Generates boilerplate files (Service, Handler, Repository).
- Wires it up to the dependency injection container.
The Manual Way (Understanding the Files)
If you were to do it manually, here is what you would build:
1. Define the Entity (domain/projects.go)
package domain
type Project struct {
ID int32
Name string
OrganizationID int32
}
2. Define the Interface (domain/repository.go)
type ProjectRepository interface {
Create(ctx context.Context, p *Project) error
}
3. Implement Repository (infra/repository.go)
type Repository struct {
db *gorm.DB
}
func (r *Repository) Create(ctx context.Context, p *domain.Project) error {
return r.db.Create(p).Error
}
4. Wire it all up (cmd/init.go)
This is the dependency injection step.
func InitModule(container *dig.Container) {
container.Provide(NewRepository)
container.Provide(NewService)
container.Provide(NewHandler)
}