platform

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
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

func Get[T any](a *App) T

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 Lookup

func Lookup[T any](a *App) (T, bool)

Lookup returns the value of type T and reports whether it is present.

func Provide

func Provide[T any](a *App, v T)

Provide stores a value in the container under its own type, replacing any previous one.

func Run

func Run(cfg Config, modules []Module, wire Wire) error

Run starts the modules and blocks until SIGINT or SIGTERM.

func RunContext

func RunContext(ctx context.Context, cfg Config, modules []Module, wire Wire) error

RunContext behaves like Run but stops when ctx is done. Tests use it.

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

func NewApp(log *slog.Logger) *App

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

func (a *App) AddHealthCheck(name string, check func(context.Context) error)

AddHealthCheck registers a check for /health. Modules need not call it: the platform adds a check for every module implementing HealthChecker.

func (*App) Logger

func (a *App) Logger() *slog.Logger

Logger returns the application logger.

func (*App) Metrics

func (a *App) Metrics() *prometheus.Registry

Metrics returns the registry served at /metrics.

func (*App) Role added in v0.2.2

func (a *App) Role() string

Role returns what this process does: all, api or worker.

func (*App) Serves added in v0.2.2

func (a *App) Serves(role string) bool

Serves reports whether this process takes the given role: a process in role all takes every role. A module that serves requests checks api, one that works jobs checks worker.

func (*App) Service

func (a *App) Service() string

Service returns the service name. Modules put it in page titles and messages.

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

type HealthChecker interface {
	Health(ctx context.Context) error
}

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

type Starter interface {
	Start(ctx context.Context) error
}

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.

type Stopper

type Stopper interface {
	Stop(ctx context.Context) error
}

Stopper releases resources. Modules are stopped in reverse order of initialisation, sharing one shutdown timeout.

type Wire

type Wire func(app *App) error

Wire builds the project domain: use cases, handlers, workers. It runs between Init and Start of every module, so queues and servers begin work with handlers in place.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL