provider

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package provider holds ready-made resource constructors for the dependencies applications need most often, so an app's deps layer declares WHAT to build and reuses these for HOW. Each returns a dim factory — func(ctx) (value, dim.CleanupFunc, error) — ready for dim.NewResource, built on top of the SDK's own primitives (dbx, broker/rabbitmq, otel).

Usage

// in an app initializer (app.Initializer[Deps]):
c.DB, cleanup = dim.NewResource("DB", provider.Postgres(dbx.Config{
	User: opts.DB.User, Password: opts.DB.Password,
	Host: opts.DB.Host, Name: opts.DB.Name, DisableTLS: opts.DB.DisableTLS,
}))
return cleanup, nil

Each provider takes an SDK config (dbx.Config, rabbitmq.Config, otel.Config), so the app maps its own options to the SDK config once and the connect / status-check / cleanup boilerplate lives here.

Included

  • Postgres: pgx pool via dbx.Open + StatusCheck; cleanup closes the pool.
  • RabbitMQ: a *rabbitmq.Conn via rabbitmq.Dial; cleanup closes it.
  • Redis: a *redis.Client (go-redis) verified with PING; cleanup closes it.
  • Sentry: initializes the Sentry SDK and returns the Hub; cleanup flushes.
  • Tracer: an OTLP/gRPC trace.Tracer via otel.InitTracing; cleanup flushes and shuts down (gate it on your own enabled flag — see Tracer).
  • Translator: an i18n.Translator built from caller-supplied message files; language-agnostic (the app passes its default language + embed.FS).

A SQLite provider is intentionally NOT here yet: dbx is Postgres-only (pgx), so it would pull a new SQLite driver dependency into the core SDK. Add it once that trade-off is decided (possibly as a sub-module).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Postgres

func Postgres(cfg dbx.Config) func(ctx context.Context) (*sqlx.DB, dim.CleanupFunc, error)

Postgres returns a dim factory that opens a pgx-backed connection pool from cfg and verifies connectivity (dbx.StatusCheck). The cleanup closes the pool. Wire it with dim.NewResource:

c.DB, cleanup = dim.NewResource("DB", provider.Postgres(dbx.Config{
	User: opts.DB.User, Password: opts.DB.Password, Host: opts.DB.Host,
	Name: opts.DB.Name, DisableTLS: opts.DB.DisableTLS,
}))

func RabbitMQ

func RabbitMQ(log *logger.Logger, cfg rabbitmq.Config) func(ctx context.Context) (*rabbitmq.Conn, dim.CleanupFunc, error)

RabbitMQ returns a dim factory that dials RabbitMQ with cfg. The cleanup closes the connection (safe to register as a closer cleanup). Build a Publisher/Consumer on the returned *rabbitmq.Conn separately.

c.BrokerConn, cleanup = dim.NewResource("BrokerConn",
	provider.RabbitMQ(log, rabbitmq.Config{
		User: opts.Broker.User, Password: opts.Broker.Password,
		Host: opts.Broker.Host, Port: opts.Broker.Port,
	}))

func Redis

func Redis(cfg RedisConfig) func(ctx context.Context) (*redis.Client, dim.CleanupFunc, error)

Redis returns a dim factory that opens a go-redis client from cfg and verifies it with a PING. The cleanup closes the client.

func Sentry

func Sentry(cfg SentryConfig) func(ctx context.Context) (*sentry.Hub, dim.CleanupFunc, error)

Sentry returns a dim factory that initializes the Sentry SDK from cfg and returns the current Hub. The cleanup flushes buffered events, bounded by FlushTimeout.

func Tracer

func Tracer(cfg otel.Config) func(ctx context.Context) (trace.Tracer, dim.CleanupFunc, error)

Tracer returns a dim factory that bootstraps OTLP/gRPC tracing from cfg and returns the named Tracer; the cleanup flushes pending spans and shuts the exporter down.

This always initializes the exporter. Gate it on your own "tracing enabled" flag — when disabled, assign a no-op tracer instead of calling this:

if !opts.OTEL.Enabled {
	c.Tracer = func(context.Context) trace.Tracer {
		return noop.NewTracerProvider().Tracer(opts.Service)
	}
	return nil, nil
}
c.Tracer, cleanup = dim.NewResource("Tracer", provider.Tracer(otel.Config{...}))

func Translator

func Translator(defaultLang string, fsys fs.FS, files ...string) func(ctx context.Context) (*i18n.Translator, dim.CleanupFunc, error)

Translator returns a dim factory that builds an i18n.Translator from the message files in fsys. It is language-agnostic: the caller supplies the default language and the message files (typically an embed.FS of the app's own translations) instead of the provider hard-coding a fixed set of languages.

//go:embed locales/*.json
var localesFS embed.FS

c.Translator, cleanup = dim.NewResource("Translator",
	provider.Translator("ru", localesFS, "locales/ru.json", "locales/kk.json"))

It holds no external resource, so the cleanup is nil.

Types

type RedisConfig

type RedisConfig struct {
	// Addr is the host:port, e.g. "localhost:6379".
	Addr string
	// Password is the auth password (empty for none).
	Password string
	// DB is the database index.
	DB int
}

RedisConfig configures the Redis client.

type SentryConfig

type SentryConfig struct {
	// DSN is the Sentry project DSN. An empty DSN disables transport (the SDK
	// still initializes; events are dropped) — gate on it in your initializer if
	// you prefer not to init at all.
	DSN string
	// Environment tags events (e.g. "production").
	Environment string
	// Debug enables the Sentry SDK's debug logging.
	Debug bool
	// IgnoreErrors is appended to the default ignore list ("context canceled").
	IgnoreErrors []string
	// FlushTimeout bounds the flush performed on cleanup (default 2s).
	FlushTimeout time.Duration
}

SentryConfig configures Sentry initialization.

Jump to

Keyboard shortcuts

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