Skip to content

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:

FileResponsibility
module.goExports Module() di.Module declaring Providers, Invokes, and Models.
model.goGORM model structs (and any secondary models such as AuditLog).
service.goBusiness logic; injects *database.Database, *events.EventBus, *config.AllConfig.
handler.goGin HTTP handlers; binds JSON, calls core/validator, delegates to service, passes errors to _ = c.Error(err).
routes.goImplements server.Router (Register(engine *gin.Engine)). Auto-collected by boot.
events.goEvent topic constants and typed payload structs (created by gen:event).
listeners.goevents.Listener[T] implementations + RegisterListeners(bus, db) invoke function.
docs.goImplements server.Router to serve Huma v2 OpenAPI 3.1 docs at /api/docs/<name>.
grpc.goImplements 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:

go
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 ​

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:

go
// 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.

Released under the Apache-2.0 License.