Services & Modules
GoForge structures business features as self-contained vertical slices inside internal/services/<name>/, wired into the application via core/di.
9-File Service Layout
Running goforge gen:service <name> scaffolds the service directory and updates internal/services/kernel.go:
| File | Responsibility |
|---|---|
module.go | Exports Module() di.Module declaring Providers, Invokes, and Models. |
model.go | GORM model structs (and any secondary models such as AuditLog). |
service.go | Business logic; injects *database.Database, *events.EventBus, *config.AllConfig. |
handler.go | Gin HTTP handlers; binds JSON, calls core/validator, delegates to service, passes errors to _ = c.Error(err). |
routes.go | Implements server.Router (Register(engine *gin.Engine)). Auto-collected by boot. |
events.go | Event topic constants and typed payload structs (created by gen:event). |
listeners.go | events.Listener[T] implementations + RegisterListeners(bus, db) invoke function. |
docs.go | Implements server.Router to serve Huma v2 OpenAPI 3.1 docs at /api/docs/<name>. |
grpc.go | Implements server.GRPCRegistry (Register(*grpc.Server)) when gen:proto is run. |
Composition Root (internal/services/kernel.go)
kernel.go exports two functions consumed by boot and database tooling:
package services
import (
"github.com/mmycin/goforge/core/di"
"github.com/mmycin/goforge/internal/services/todo"
)
func Modules() []di.Module {
return []di.Module{
todo.Module(),
}
}
func Model() []any {
return di.CollectModels(Modules()...)
}Whenever you run goforge gen:service <name> or goforge rem:service <name>, the CLI scans internal/services/*/module.go and regenerates kernel.go.
Example module.go
package todo
import "github.com/mmycin/goforge/core/di"
func Module() di.Module {
return di.Module{
Name: "todo",
Providers: []any{
NewTodoService,
NewTodoHandler,
NewTodoRoutes,
NewTodoDocs,
},
Invokes: []any{
RegisterListeners,
},
Models: []any{
&Todo{},
&AuditLog{},
},
}
}Because both *TodoRoutes and *TodoDocs have Register(engine *gin.Engine), boot/kernel.go's di.AutoCollect[server.Router]() collects both automatically during container.Build().
Cross-Module Dependency Injection
Any provider in module B can inject a type provided by module A simply by declaring it as a constructor parameter:
// In internal/services/task/routes.go
func NewTaskRoutes(handler *TaskHandler, authMW *auth.AuthMiddleware) *TaskRoutes {
return &TaskRoutes{handler: handler, authMW: authMW}
}
func (r *TaskRoutes) Register(engine *gin.Engine) {
group := engine.Group("/api/tasks")
group.Use(r.authMW.RequireAuth())
group.GET("", r.handler.List)
group.POST("", r.handler.Create)
}If auth.Module() is missing from services.Modules(), container.Validate() fails immediately with di.ErrMissingDependency.
