Documentation
¶
Index ¶
- Constants
- func Get[T any](a *App) T
- func Lookup[T any](a *App) (T, bool)
- func Provide[T any](a *App, v T)
- func Run(cfg Config, modules []Module, wire Wire) error
- func RunContext(ctx context.Context, cfg Config, modules []Module, wire Wire) error
- type App
- type Config
- type HealthChecker
- type Module
- type Starter
- type Stopper
- type Wire
Constants ¶
const ( RoleAll = "all" RoleAPI = "api" RoleWorker = "worker" )
Roles a process can run in. One binary serves them all: an API instance and a worker instance differ only in APP_ROLE.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
Get returns the value of type T and panics when it is missing.
Panicking is right here: a missing dependency is a wiring mistake, and it shows up on the first run rather than in production under load.
func Provide ¶
Provide stores a value in the container under its own type, replacing any previous one.
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is the application container: logger, metrics, health checks and the values modules share with each other.
Modules find each other through the container instead of through edits in main.go, which is why adding or removing a module needs no manual changes in project code.
func NewApp ¶
NewApp creates a container without running modules. Tests and platform tooling use it when the full lifecycle is not needed. The logger may be nil.
func (*App) AddHealthCheck ¶
AddHealthCheck registers a check for /health. Modules need not call it: the platform adds a check for every module implementing HealthChecker.
func (*App) Metrics ¶
func (a *App) Metrics() *prometheus.Registry
Metrics returns the registry served at /metrics.
type Config ¶
type Config struct {
Service string // service name in logs
Role string // what this process does: all, api or worker; all by default
OpsAddr string // ops server address, :9090 by default
ShutdownTimeout time.Duration // overall shutdown timeout, 20s by default
Logger *slog.Logger
// OnStarted is called once every module and the ops server are up. Tests and local
// runs use it to learn the actual ops address.
OnStarted func(opsAddr string)
}
Config holds startup parameters. Empty fields fall back to defaults.
type HealthChecker ¶
HealthChecker adds the module to the /health response.
type Module ¶
type Module interface {
// Name identifies the module in logs, health output and errors.
Name() string
// Init prepares the module and puts whatever it shares into the container.
// Modules are initialised in the order they are listed.
Init(ctx context.Context, app *App) error
}
Module is an infrastructure part of the application: database, queues, API, admin UI.
Only Name and Init are required. Everything else is optional: the platform checks for Starter, Stopper and HealthChecker and calls them when implemented, so a module without background work stays a few lines long.
type Starter ¶
Starter runs background work: queue workers, servers, subscriptions. It is called after the domain is wired so that every handler is registered before traffic starts.