setup

package module
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: May 28, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package setup wires the full standard stack in one call with correct ordering, eliminating the "register everything in the right order" footgun.

Usage:

mgr := gscore.New(gscore.Config{})
result, err := setup.New().
    WithLogger(logcore.Options{Service: "my-service"}).
    WithOTel(ctx).
    WithGORM(db).
    Build(mgr)
if err != nil {
    log.Fatal(err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

Builder accumulates configuration via functional options before wiring everything in Build. The zero value is valid; calling Build with no options registers nothing and returns an empty Result.

func New

func New() *Builder

New returns an empty Builder.

func (*Builder) Build

func (b *Builder) Build(mgr *gscore.Manager) (*Result, error)

Build wires all configured components onto mgr in the required order:

  1. gsfiber.RegisterApp / gsfiberv3.RegisterApp (if WithFiberV2 / WithFiberV3) 0a. liveness + readiness + startup probe routes (if WithHealthProbesV2 / WithHealthProbesV3)
  2. apmcore.SetupOTelSDK (if WithOTel)
  3. logcore.New + logcore.SetGlobal (if WithLogger)
  4. apmcore.NewGormPlugin via db.Use (if WithGORM + WithOTel)
  5. mgr.RegisterDB (if WithGORM)
  6. txcore.RegisterWithManager (if WithGORM)
  7. apmcore.RegisterDBPoolMetricsWithManager (if WithGORM + WithOTel)
  8. autobatch.RegisterWithManager (if WithAutobatch or WithAutobatchConfig) 7b. gormcache plugin via db.Use (if WithGORMCache or WithGORMCacheConfig)
  9. go-redis closers + optional OTel hooks (if WithRedis)
  10. rueidis closers + optional OTel wrapping (if WithRueidis)
  11. apmcore.RegisterWithManager (if WithOTel)
  12. logcore.InstallHTTPClientHook (if WithHTTPClientLogging)
  13. startupFn() + mgr.MarkStarted() (if WithStartupFn)

Build returns an error if apmcore.SetupOTelSDK, logcore.New, db.Use(apmcore.NewGormPlugin()), or the startup function fails, or if WithHealthProbesV2/V3 is set without a corresponding WithFiberV2/V3.

func (*Builder) WithAutobatch

func (b *Builder) WithAutobatch(p *autobatch.Plugin) *Builder

WithAutobatch configures the builder to register p.Close() as a PhasePostDrain closer on the Manager, draining batched writes before the DB pool closes.

func (*Builder) WithAutobatchConfig

func (b *Builder) WithAutobatchConfig(cfg autobatch.Config) *Builder

WithAutobatchConfig configures the builder to create an autobatch plugin from cfg and register it with the Manager. When WithOTel is also set, Build automatically injects cfg.SpanEmitter = apmcore.BatchSpanEmitter() before creating the plugin so batched writes appear in APM traces.

func (*Builder) WithFiberV2

func (b *Builder) WithFiberV2(app *fiberv2.App) *Builder

WithFiberV2 registers a Fiber v2 app on the Manager so it is drained during the drain phase of graceful shutdown. Multiple calls are not supported; only the last supplied app is registered.

func (*Builder) WithFiberV3

func (b *Builder) WithFiberV3(app *fiberv3.App) *Builder

WithFiberV3 registers a Fiber v3 app on the Manager so it is drained during the drain phase of graceful shutdown. Multiple calls are not supported; only the last supplied app is registered.

func (*Builder) WithGORM

func (b *Builder) WithGORM(db *gorm.DB) *Builder

WithGORM configures the builder to register txcore with the Manager so that in-flight transactions are drained before the DB pool is closed.

func (*Builder) WithGORMCache added in v0.8.0

func (b *Builder) WithGORMCache(p *caches.Caches) *Builder

WithGORMCache configures the builder to register a pre-built gormcache plugin via db.Use during Build. Use WithGORMCacheConfig instead when you want Build to construct the plugin and wire OTel instrumentation automatically.

func (*Builder) WithGORMCacheConfig added in v0.8.0

func (b *Builder) WithGORMCacheConfig(cfg caches.Config) *Builder

WithGORMCacheConfig configures the builder to create a gormcache plugin from cfg and register it with GORM during Build. When WithOTel is also set, Build automatically wraps cfg.Cacher with apmcore.InstrumentCacher so cache operations appear as OTel spans.

func (*Builder) WithHTTPClientLogging

func (b *Builder) WithHTTPClientLogging() *Builder

WithHTTPClientLogging configures the builder to install the global logcore HTTP client hook via logcore.InstallHTTPClientHook. The hook is installed after the logger is set up so it inherits the configured global logger.

func (*Builder) WithHealthProbesV2 added in v0.7.1

func (b *Builder) WithHealthProbesV2(cfg HealthProbesConfig) *Builder

WithHealthProbesV2 registers the three Kubernetes probe handlers (liveness, readiness, startup) on the Fiber v2 app. Must be called after WithFiberV2. cfg may be zero-valued to use the default paths:

GET /healthz/live    → always 200 (liveness)
GET /healthz/ready   → 200 while ready, 503 during shutdown (readiness)
GET /healthz/startup → 503 until mgr.MarkStarted(), then 200 (startup)

func (*Builder) WithHealthProbesV3 added in v0.7.1

func (b *Builder) WithHealthProbesV3(cfg HealthProbesConfig) *Builder

WithHealthProbesV3 registers the three Kubernetes probe handlers (liveness, readiness, startup) on the Fiber v3 app. Must be called after WithFiberV3. cfg may be zero-valued to use the default paths:

GET /healthz/live    → always 200 (liveness)
GET /healthz/ready   → 200 while ready, 503 during shutdown (readiness)
GET /healthz/startup → 503 until mgr.MarkStarted(), then 200 (startup)

func (*Builder) WithLogger

func (b *Builder) WithLogger(opts logcore.Options) *Builder

WithLogger configures the builder to create a logcore.Logger using opts and set it as the process-global logger.

func (*Builder) WithOTel

func (b *Builder) WithOTel(ctx context.Context) *Builder

WithOTel configures the builder to call apmcore.SetupOTelSDK(ctx) during Build. ctx is typically the application root context or context.Background().

func (*Builder) WithRedis

func (b *Builder) WithRedis(client redis.UniversalClient, name string) *Builder

WithRedis registers a go-redis UniversalClient for graceful shutdown at PhasePostDB and, if WithOTel was also called, instruments it with OTel tracing and metrics. Multiple calls accumulate; each client is registered independently. name is used in shutdown log lines.

func (*Builder) WithRueidis

func (b *Builder) WithRueidis(client rueidis.Client, name string) *Builder

WithRueidis registers a rueidis Client for graceful shutdown at PhasePostDB and, if WithOTel was also called, wraps it with OTel tracing and metrics. Multiple calls accumulate; each client is registered independently. name is used in shutdown log lines.

func (*Builder) WithStartupFn added in v0.7.1

func (b *Builder) WithStartupFn(fn func() error) *Builder

WithStartupFn registers a boot function that runs at the end of Build, after all components are wired. If fn returns nil, mgr.MarkStarted() is called automatically, flipping the startup probe to 200. If fn returns an error, Build returns that error and MarkStarted is never called.

This is the recommended way to run migrations and warm-up logic, as it makes the relationship between boot completion and startup probe explicit:

setup.New().
    WithFiberV2(app).
    WithHealthProbesV2(setup.HealthProbesConfig{}).
    WithGORM(db).
    WithStartupFn(func() error {
        if err := runMigrations(db); err != nil {
            return err
        }
        return warmUpCache(ctx)
    }).
    Build(mgr)
// mgr.MarkStarted() was called automatically — startup probe is now 200.

type HealthProbesConfig added in v0.7.1

type HealthProbesConfig struct {
	// LivenessPath is the path for the liveness probe (default: /healthz/live).
	LivenessPath string
	// ReadinessPath is the path for the readiness probe (default: /healthz/ready).
	ReadinessPath string
	// StartupPath is the path for the startup probe (default: /healthz/startup).
	StartupPath string
}

HealthProbesConfig holds the HTTP paths for the three Kubernetes probe endpoints. Zero values fall back to the defaults below.

type Result

type Result struct {
	// Logger is the logcore.Logger created by WithLogger; nil if not configured.
	Logger *logcore.Logger
	// Shutdown is the OTel/APM shutdown function registered by WithOTel; nil
	// if not configured.
	Shutdown apmcore.ShutdownFunc
}

Result is returned by Build and carries handles to the resources that were created. Fields are nil/zero when the corresponding With-option was not set.

Jump to

Keyboard shortcuts

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