refactor: consolidate Require API and split lifecycle files

Add RequireOpts, RequireWithOpts, and GetModule. Move helpers to
lifecycle_internal.go and errors to errors.go. Remove unused GenericModule.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-06-29 17:58:05 -07:00
parent 4cd218f658
commit a07d9d06ed
4 changed files with 69 additions and 17 deletions
+2 -12
View File
@@ -1,25 +1,14 @@
package app
import (
"errors"
"fmt"
"log/slog"
"strings"
)
// ErrModuleNotFound is returned when a module is not found in the lifecycle.
var ErrModuleNotFound = errors.New("module not found")
// ModuleFn is a function type for module setup and teardown functions.
type ModuleFn func(*Module) error
// GenericModule is an interface that allows modules to be extended with custom
// functionality, as the module can return a pointer to the underlying Module.
type GenericModule interface {
// Module returns the underlying Module instance.
Module() *Module
}
// Module represents a sub-system of the application, with its setup and
// teardown functions and dependencies.
type Module struct {
@@ -60,7 +49,8 @@ func (s *Module) Lifecycle() *Lifecycle {
}
// Logger returns the logger for the module. Uses the lifecycle's logger unless
// a specific logger has been set during module load.
// a specific logger has been set during module load. Panics if the module is
// not associated with a lifecycle and no override logger was set.
func (s *Module) Logger() *slog.Logger {
if s.logger != nil {
return s.logger