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
- type Config
- type Extension
- func (e *Extension) Engine() *ledger.Ledger
- func (e *Extension) Health(ctx context.Context) error
- func (e *Extension) Register(fapp forge.App) error
- func (e *Extension) RegisterContractContributor(disp *dispatcher.Dispatcher, reg contract.Registry, ...) error
- func (e *Extension) Start(ctx context.Context) error
- func (e *Extension) Stop(_ context.Context) error
- type Option
- func WithAppID(id string) Option
- func WithBasePath(path string) Option
- func WithConfig(cfg Config) Option
- func WithDisableLifecycle() Option
- func WithDisableMigrate() Option
- func WithDisableRoutes() Option
- func WithEntitlementCacheTTL(d time.Duration) Option
- func WithGroveDatabase(name string) Option
- func WithLedgerOption(opt ledger.Option) Option
- func WithMeterBatchSize(size int) Option
- func WithMeterFlushInterval(d time.Duration) Option
- func WithPlugin(p plugin.Plugin) Option
- func WithRequireConfig(require bool) Option
- func WithStore(s store.Store) Option
Constants ¶
const ExtensionDescription = "Composable usage-based billing engine"
ExtensionDescription is the human-readable description.
const ExtensionName = "ledger"
ExtensionName is the name registered with Forge.
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 (*Extension) Engine ¶
Engine returns the underlying Ledger instance. This is nil until Register is called.
func (*Extension) Health ¶
Health implements forge.Extension.
func (*Extension) Register ¶
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 ¶
Start implements forge.Extension.
type Option ¶
type Option func(*Extension)
Option configures the Ledger Forge extension.
func WithBasePath ¶
WithBasePath sets the URL prefix for ledger routes.
func WithConfig ¶
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 ¶
WithEntitlementCacheTTL sets the entitlement check cache duration.
func WithGroveDatabase ¶
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 ¶
WithLedgerOption passes a ledger.Option through to the underlying engine.
func WithMeterBatchSize ¶
WithMeterBatchSize sets the number of usage events to buffer before flushing.
func WithMeterFlushInterval ¶
WithMeterFlushInterval sets how frequently the meter buffer is flushed.
func WithRequireConfig ¶
WithRequireConfig requires config to be present in YAML files. If true and no config is found, Register returns an error.