app

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package appkit is the service runtime: one constructor that wires the infrastructure a service would otherwise hand-roll in main() — logging, database cluster, event publisher/subscriber, audit client, event signing, health checks, the standard middleware stack and graceful shutdown.

The runtime never builds a router. Handler decorates whatever http.Handler the service brings with the standard middleware and the operational endpoints, and Run serves it:

a, err := app.New("orders",
	app.WithDatabase(),
	app.WithOptionalEventPublisher(),
)
if err != nil {
	return err
}
defer a.Close()

mux := http.NewServeMux()
// ... register service routes on mux ...
a.Run(a.Handler(mux))

Schema migration is a service concern: WithMigration takes a func(*sql.DB) error, so goose, Atlas or an ORM's automigration all plug in without the runtime depending on any of them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	Name        string
	Port        string
	Signing     *eventbus.SignatureConfig
	DBManager   *pgcluster.ClusterManager
	RabbitNodes []string
	EventPub    eventbus.PublisherInterface
	EventSub    *amqpcluster.ClusterSubscriber
	Audit       *audit.AuditClient
	Health      *health.HealthChecker
	Tomb        *lifecycle.Manager
	// contains filtered or unexported fields
}

App holds the wired infrastructure for one service instance.

func New

func New(name string, opts ...Option) (*App, error)

New wires the requested infrastructure for the named service. On error the already-created resources are released; callers treat an error as fatal.

func (*App) Close

func (a *App) Close()

Close releases everything New created, in reverse order. Safe to call after a failed New and idempotent enough to defer immediately after it.

func (*App) DetailedHealthHandler

func (a *App) DetailedHealthHandler() http.HandlerFunc

DetailedHealthHandler exposes the rich health report; services typically mount it inside their authenticated API surface.

func (*App) Handler

func (a *App) Handler(routes http.Handler) http.Handler

Handler decorates routes with the standard middleware stack in the canonical order — tracing, metrics, request logging — and serves the operational endpoints every service must expose: /health, /ready, /metrics, and the profiling endpoints when the debug gate is open. Anything else falls through to routes.

CORS is deliberately absent: the allowed origins are a deployment policy, so a service that needs them wraps the result in middleware.CORS itself.

func (*App) HealthCheckers

func (a *App) HealthCheckers() []middleware.HealthChecker

HealthCheckers reports on every dependency the service touches, including ones it can run degraded without.

func (*App) ReadinessCheckers

func (a *App) ReadinessCheckers() []middleware.HealthChecker

ReadinessCheckers gates traffic on hard dependencies only: the database, and the event bus when the publisher was required rather than optional.

func (*App) Run

func (a *App) Run(handler http.Handler, cleanup ...func() error)

Run serves handler on the configured port with tomb-managed graceful shutdown. It blocks until a shutdown signal has been handled; any cleanup functions run during shutdown, before the tracked resources are released.

func (*App) SQLDB

func (a *App) SQLDB() *sql.DB

SQLDB returns the writer connection pool, or nil when the service runs without a database.

type Option

type Option func(*options)

Option configures which infrastructure New wires up.

func WithAudit

func WithAudit() Option

WithAudit wires the signed audit client; New fails if the broker is unreachable, audit being a compliance dependency.

func WithDatabase

func WithDatabase() Option

WithDatabase connects the database cluster and exposes the writer pool through SQLDB.

func WithEventPublisher

func WithEventPublisher() Option

WithEventPublisher wires a signed event publisher; New fails if the broker is unreachable.

func WithEventSubscriber

func WithEventSubscriber() Option

WithEventSubscriber wires a signed event subscriber; New fails if the broker is unreachable.

func WithHealthCheck

func WithHealthCheck(check middleware.HealthChecker) Option

WithHealthCheck adds a check to the /health report. It does not gate readiness; only the runtime's own hard dependencies do.

func WithMigration

func WithMigration(migrate func(*sql.DB) error) Option

WithMigration connects the database cluster and runs migrate against the writer pool before New returns. New fails if the migration fails.

func WithOptionalAudit

func WithOptionalAudit() Option

WithOptionalAudit wires the audit client if the broker is reachable and degrades to a nil Audit with a warning otherwise.

func WithOptionalEventPublisher

func WithOptionalEventPublisher() Option

WithOptionalEventPublisher wires the publisher if the broker is reachable and degrades to a nil EventPub with a warning otherwise.

func WithOptionalEventSubscriber

func WithOptionalEventSubscriber() Option

WithOptionalEventSubscriber wires the subscriber if the broker is reachable and degrades to a nil EventSub with a warning otherwise.

Jump to

Keyboard shortcuts

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