app

package
v0.1.36 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 10 Imported by: 2

Documentation

Overview

app/app.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run[C any, D any](ctx context.Context, hooks Hooks[C, D]) error

Run executes the standard Waffle startup sequence:

  1. Bootstrap logger
  2. Load core + app config (Hooks.LoadConfig)
  3. Validate the loaded config (Hooks.ValidateConfig, if provided)
  4. Build final logger based on core config
  5. Wire shutdown signals to context (so all subsequent steps respect SIGINT/SIGTERM)
  6. Register default metrics
  7. Connect DB/backends (Hooks.ConnectDB)
  8. Ensure schema/indexes (Hooks.EnsureSchema, if provided)
  9. Startup (Hooks.Startup, if provided)
  10. Build the HTTP handler (Hooks.BuildHandler)
  11. Start the HTTP(S) server and block until shutdown
  12. Run the optional shutdown hook (Hooks.Shutdown) to clean up resources

Types

type Hooks

type Hooks[C any, D any] struct {
	// Name is used only for logging/diagnostics.
	Name string

	// LoadConfig must return both the core config (WAFFLE-level) and
	// the app-specific config. It typically calls waffle/config.Load
	// internally, plus any app-level config loading/validation.
	LoadConfig func(logger *zap.Logger) (*config.CoreConfig, C, error)

	// ValidateConfig can perform app-specific validation on the loaded
	// core and app config before any backends are connected. It may be
	// nil if the app doesn’t require extra validation. Returning an
	// error here will abort startup before any external resources are used.
	ValidateConfig func(core *config.CoreConfig, appCfg C, logger *zap.Logger) error

	// ConnectDB is responsible for connecting to any databases or backends
	// the app needs, using the core + app config. It should respect
	// cfg.DBConnectTimeout for its own timeouts.
	ConnectDB func(ctx context.Context, core *config.CoreConfig, appCfg C, logger *zap.Logger) (D, error)

	// EnsureSchema can run validators/index creation or other startup tasks
	// that depend on the DB being connected. It may be nil if the app
	// doesn’t need any schema bootstrapping.
	EnsureSchema func(ctx context.Context, core *config.CoreConfig, appCfg C, db D, logger *zap.Logger) error

	// Startup runs one-time application initialization after DBs and schemas
	// are ready, but before the HTTP handler is built and requests are served.
	// It may be nil if the app doesn’t need any extra initialization
	// beyond config, DB, and schema setup.
	Startup func(ctx context.Context, core *config.CoreConfig, appCfg C, db D, logger *zap.Logger) error

	// BuildHandler must construct the final http.Handler for the app:
	// this includes routers, Waffle middleware, app middleware, and routes.
	BuildHandler func(core *config.CoreConfig, appCfg C, db D, logger *zap.Logger) (http.Handler, error)

	// OnReady is called after the HTTP server starts listening but before
	// the main goroutine blocks. This is useful for signaling to load balancers
	// or orchestrators that the application is ready to accept traffic.
	// It may be nil if the app doesn't need readiness signaling.
	// Example uses:
	//   - Write a "ready" file for Kubernetes
	//   - Log "server ready" message
	//   - Start background workers
	OnReady func(core *config.CoreConfig, appCfg C, db D, logger *zap.Logger)

	// Shutdown is called after the HTTP server has stopped and the
	// shutdown context has been canceled. It is the app's opportunity
	// to gracefully tear down any resources created in ConnectDB
	// (databases, caches, external clients, etc.). It may be nil if
	// the app doesn't need explicit shutdown logic.
	Shutdown func(context.Context, *config.CoreConfig, C, D, *zap.Logger) error
}

Hooks defines the integration points an application must provide for WAFFLE to run it.

Jump to

Keyboard shortcuts

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