extension

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package extension provides the Forge extension adapter for Ledger.

It implements the forge.Extension interface to integrate Ledger into a Forge application with automatic dependency discovery, DI registration, and lifecycle management.

Configuration can be provided programmatically via Option functions or via YAML configuration files under "extensions.ledger" or "ledger" keys.

Index

Constants

View Source
const ExtensionDescription = "Composable usage-based billing engine"

ExtensionDescription is the human-readable description.

View Source
const ExtensionName = "ledger"

ExtensionName is the name registered with Forge.

View Source
const ExtensionVersion = "0.1.0"

ExtensionVersion is the semantic version.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// DisableRoutes prevents HTTP route registration.
	DisableRoutes bool `json:"disable_routes" mapstructure:"disable_routes" yaml:"disable_routes"`

	// DisableMigrate prevents auto-migration on start.
	DisableMigrate bool `json:"disable_migrate" mapstructure:"disable_migrate" yaml:"disable_migrate"`

	// BasePath is the URL prefix for ledger routes (default: "/ledger").
	BasePath string `json:"base_path" mapstructure:"base_path" yaml:"base_path"`

	// MeterBatchSize is the number of usage events to buffer before flushing
	// to the store (default: 100).
	MeterBatchSize int `json:"meter_batch_size" mapstructure:"meter_batch_size" yaml:"meter_batch_size"`

	// MeterFlushInterval is how frequently the meter buffer is flushed
	// even if the batch size has not been reached (default: 5s).
	MeterFlushInterval time.Duration `json:"meter_flush_interval" mapstructure:"meter_flush_interval" yaml:"meter_flush_interval"`

	// EntitlementCacheTTL controls how long entitlement check results are
	// cached in-process before re-evaluating against the store (default: 30s).
	EntitlementCacheTTL time.Duration `json:"entitlement_cache_ttl" mapstructure:"entitlement_cache_ttl" yaml:"entitlement_cache_ttl"`

	// LifecycleInterval is how often the lifecycle clock runs: it moves ended
	// billing periods forward, enacts scheduled cancellations, ends trials and
	// marks overdue invoices past due (default: 1m). Zero means the default,
	// as for every other duration here, and so does a negative value: neither
	// turns the clock off. Use DisableLifecycle for that.
	LifecycleInterval time.Duration `json:"lifecycle_interval" mapstructure:"lifecycle_interval" yaml:"lifecycle_interval"`

	// DisableLifecycle turns the lifecycle clock off, for a deployment that
	// calls Ledger.Advance from its own scheduler.
	DisableLifecycle bool `json:"disable_lifecycle" mapstructure:"disable_lifecycle" yaml:"disable_lifecycle"`

	// GroveDatabase is the name of a grove.DB registered in the DI container.
	// When set, the extension resolves this named database and auto-constructs
	// the appropriate store based on the driver type (pg/sqlite/mongo).
	// When empty and WithGroveDatabase was called, the default (unnamed) DB is used.
	GroveDatabase string `json:"grove_database" mapstructure:"grove_database" yaml:"grove_database"`

	// AppID scopes all operations to a specific application identifier.
	// Used by the dashboard contributor to filter data by app.
	AppID string `json:"app_id" mapstructure:"app_id" yaml:"app_id"`

	// RequireAppClaim refuses dashboard contract requests whose principal
	// carries no app_id claim, even when AppID is set. Turn it on in a
	// multi-app deployment.
	RequireAppClaim bool `json:"require_app_claim" mapstructure:"require_app_claim" yaml:"require_app_claim"`

	// RequireConfig requires config to be present in YAML files.
	// If true and no config is found, Register returns an error.
	RequireConfig bool `json:"-" yaml:"-"`
}

Config holds the Ledger extension configuration. Fields can be set programmatically via Option functions or loaded from YAML configuration files (under "extensions.ledger" or "ledger" keys).

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

type Extension

type Extension struct {
	*forge.BaseExtension
	// contains filtered or unexported fields
}

Extension adapts Ledger as a Forge extension.

func New

func New(opts ...Option) *Extension

New creates a new Ledger Forge extension with the given options.

func (*Extension) Engine

func (e *Extension) Engine() *ledger.Ledger

Engine returns the underlying Ledger instance. This is nil until Register is called.

func (*Extension) Health

func (e *Extension) Health(ctx context.Context) error

Health implements forge.Extension.

func (*Extension) Register

func (e *Extension) Register(fapp forge.App) error

Register implements forge.Extension. It loads configuration, initializes the ledger engine, and registers it in the DI container.

func (*Extension) RegisterContractContributor

func (e *Extension) RegisterContractContributor(
	disp *dispatcher.Dispatcher,
	reg contract.Registry,
	wreg contract.WardenRegistry,
) error

RegisterContractContributor registers Ledger's contract contributor with the dashboard. The dashboard calls it during its own Start.

func (*Extension) Start

func (e *Extension) Start(ctx context.Context) error

Start implements forge.Extension.

func (*Extension) Stop

func (e *Extension) Stop(_ context.Context) error

Stop implements forge.Extension.

type Option

type Option func(*Extension)

Option configures the Ledger Forge extension.

func WithAppID

func WithAppID(id string) Option

WithAppID scopes all operations to a specific application identifier.

func WithBasePath

func WithBasePath(path string) Option

WithBasePath sets the URL prefix for ledger routes.

func WithConfig

func WithConfig(cfg Config) Option

WithConfig sets the Forge extension configuration.

func WithDisableLifecycle

func WithDisableLifecycle() Option

WithDisableLifecycle turns the engine's lifecycle clock off, for a deployment that calls Ledger.Advance from its own scheduler.

func WithDisableMigrate

func WithDisableMigrate() Option

WithDisableMigrate prevents auto-migration on start.

func WithDisableRoutes

func WithDisableRoutes() Option

WithDisableRoutes prevents HTTP route registration.

func WithEntitlementCacheTTL

func WithEntitlementCacheTTL(d time.Duration) Option

WithEntitlementCacheTTL sets the entitlement check cache duration.

func WithGroveDatabase

func WithGroveDatabase(name string) Option

WithGroveDatabase sets the name of the grove.DB to resolve from the DI container. The extension will auto-construct the appropriate store backend (postgres/sqlite/mongo) based on the grove driver type. Pass an empty string to use the default (unnamed) grove.DB.

func WithLedgerOption

func WithLedgerOption(opt ledger.Option) Option

WithLedgerOption passes a ledger.Option through to the underlying engine.

func WithMeterBatchSize

func WithMeterBatchSize(size int) Option

WithMeterBatchSize sets the number of usage events to buffer before flushing.

func WithMeterFlushInterval

func WithMeterFlushInterval(d time.Duration) Option

WithMeterFlushInterval sets how frequently the meter buffer is flushed.

func WithPlugin

func WithPlugin(p plugin.Plugin) Option

WithPlugin registers a ledger plugin.

func WithRequireConfig

func WithRequireConfig(require bool) Option

WithRequireConfig requires config to be present in YAML files. If true and no config is found, Register returns an error.

func WithStore

func WithStore(s store.Store) Option

WithStore sets the store for the ledger engine.

Directories

Path Synopsis
Package contract wires Ledger into the Forge dashboard's contract path.
Package contract wires Ledger into the Forge dashboard's contract path.

Jump to

Keyboard shortcuts

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