app

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package app provides the core runtime lifecycle harness for services.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	// contains filtered or unexported fields
}

App is the central runtime coordinator for HTTP servers, background runners, and graceful shutdown.

func New

func New(name string, cfg Config) (*App, error)

New creates and initializes a new App instance.

func (*App) Attach

func (a *App) Attach(name string, target any)

Attach registers a component implementing Pinger, Metered, Runner, or io.Closer.

func (*App) AttachPeriodic

func (a *App) AttachPeriodic(name string, interval time.Duration, fn func(context.Context) error)

AttachPeriodic runs fn periodically at the given interval until the application context is canceled.

func (*App) AttachRunner

func (a *App) AttachRunner(name string, fn func(context.Context) error)

AttachRunner registers a named background runner.

func (*App) DefaultRouter

func (a *App) DefaultRouter() *chi.Mux

DefaultRouter returns a Chi router preconfigured with observability middleware, recoverer, and system routes.

func (*App) FatalIf

func (a *App) FatalIf(err error, msg string)

FatalIf logs an error and terminates the process if err is not nil.

func (*App) Healthz

func (a *App) Healthz(w http.ResponseWriter, r *http.Request)

Healthz handles liveness probes.

func (*App) Logger

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

Logger returns the application logger.

func (*App) Meter

func (a *App) Meter() metric.Meter

Meter returns the application OpenTelemetry meter.

func (*App) Readyz

func (a *App) Readyz(w http.ResponseWriter, r *http.Request)

Readyz handles readiness probes by checking all registered pingers.

func (*App) Run

func (a *App) Run(ctx context.Context) error

Run starts all servers and runners, blocking until ctx is canceled or an error occurs.

func (*App) ServeHTTP

func (a *App) ServeHTTP(target any, handler http.Handler)

ServeHTTP registers an HTTP server to be managed by the application. target can be a port string (e.g. "8080", ":8080"), an httpx.Config, or an *httpx.Config.

func (*App) ServeHTTPConfig

func (a *App) ServeHTTPConfig(cfg httpx.Config, handler http.Handler)

ServeHTTPConfig registers an HTTP server configured via httpx.Config.

func (*App) Telemetry

func (a *App) Telemetry() *telemetry.Telemetry

Telemetry returns the application telemetry subsystem.

type Config

type Config struct {
	ServiceName     string           `toml:"service_name" env:"SERVICE_NAME" env-default:"app"`
	Environment     string           `toml:"environment" env:"APP_ENV" env-default:"local"`
	ShutdownTimeout time.Duration    `toml:"shutdown_timeout" env:"SHUTDOWN_TIMEOUT" env-default:"10s" comment:"Graceful shutdown timeout"`
	Logger          logger.Config    `toml:"logger" env-prefix:"LOG_"`
	Telemetry       telemetry.Config `toml:"telemetry"`
	Profiling       ProfilingConfig  `toml:"profiling"`
}

Config defines runtime application settings.

type Metered

type Metered interface {
	RegisterMetrics(meter *telemetry.Meter) error
}

Metered defines a component that registers custom telemetry metrics.

type Pinger

type Pinger interface {
	Ping(ctx context.Context) error
}

Pinger defines a component health check contract.

type ProfilingConfig

type ProfilingConfig struct {
	Enabled       bool `toml:"enabled" env:"PPROF_ENABLED" env-default:"false" comment:"Enable /debug/pprof"`
	BlockRate     int  `toml:"block_rate" env:"PPROF_BLOCK_RATE" env-default:"0" comment:"Block profiling rate (0: disabled, 1: all)"`
	MutexFraction int  `toml:"mutex_fraction" env:"PPROF_MUTEX_FRACTION" env-default:"0" comment:"Mutex profiling sample fraction (0: disabled)"`
}

ProfilingConfig defines pprof profiling settings.

type Runner

type Runner interface {
	Run(ctx context.Context) error
}

Runner defines a long-running background component.

Jump to

Keyboard shortcuts

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