Configuration
Configuring Ledger for production deployment and development environments.
Overview
Ledger is configured through environment variables and a structured configuration system. The configuration controls database connections, payment provider integrations, API settings, and operational parameters.
Configuration structure
type Config struct {
// Database configuration
Database DatabaseConfig
// HTTP server configuration
Server ServerConfig
// Payment provider configuration
Payments PaymentConfig
// Metering configuration
Metering MeteringConfig
// Feature flags
Features FeatureConfig
}Database configuration
type DatabaseConfig struct {
// PostgreSQL connection string
URL string // required
// Connection pool settings
MaxOpenConns int // default: 25
MaxIdleConns int // default: 5
ConnMaxLifetime time.Duration // default: 5m
ConnMaxIdleTime time.Duration // default: 5m
// Migration settings
AutoMigrate bool // default: true in dev, false in prod
}Environment variables
# Database
DATABASE_URL=postgres://user:pass@localhost:5432/ledger?sslmode=disable
DATABASE_MAX_OPEN_CONNS=25
DATABASE_MAX_IDLE_CONNS=5
DATABASE_AUTO_MIGRATE=trueServer configuration
type ServerConfig struct {
// HTTP server settings
Host string // default: "0.0.0.0"
Port int // default: 8080
// Request limits
MaxRequestSize int64 // default: 10MB
ReadTimeout time.Duration // default: 30s
WriteTimeout time.Duration // default: 30s
IdleTimeout time.Duration // default: 120s
// CORS settings
AllowedOrigins []string
AllowedMethods []string
AllowedHeaders []string
// Rate limiting
RateLimit int // requests per minute, default: 1000
}Environment variables
# Server
SERVER_HOST=0.0.0.0
SERVER_PORT=8080
SERVER_MAX_REQUEST_SIZE=10485760
SERVER_READ_TIMEOUT=30s
SERVER_WRITE_TIMEOUT=30s
# CORS
CORS_ALLOWED_ORIGINS=https://app.example.com,https://dashboard.example.com
CORS_ALLOWED_METHODS=GET,POST,PUT,DELETE,PATCH
CORS_ALLOWED_HEADERS=Content-Type,Authorization
# Rate limiting
RATE_LIMIT_PER_MINUTE=1000Payment provider configuration
Ledger supports multiple payment providers (Stripe, Paddle, etc.).
type PaymentConfig struct {
// Active provider: "stripe", "paddle", or "mock"
Provider string // required
// Stripe configuration
Stripe StripeConfig
// Paddle configuration
Paddle PaddleConfig
// Webhook configuration
WebhookSecret string // required for production
}
type StripeConfig struct {
SecretKey string // required
PublishableKey string // optional, for client-side
WebhookSecret string // required for webhooks
// API settings
APIVersion string // default: latest
Timeout time.Duration // default: 30s
}Environment variables
# Stripe
PAYMENT_PROVIDER=stripe
STRIPE_SECRET_KEY=sk_test_...
STRIPE_PUBLISHABLE_KEY=pk_test_...
STRIPE_WEBHOOK_SECRET=whsec_...
STRIPE_API_VERSION=2024-11-20.acacia
# Paddle (alternative)
PAYMENT_PROVIDER=paddle
PADDLE_VENDOR_ID=12345
PADDLE_API_KEY=...
PADDLE_WEBHOOK_SECRET=...Metering configuration
Controls usage event ingestion and processing.
type MeteringConfig struct {
// Buffer settings for high-throughput ingestion
BufferSize int // events buffered before flush, default: 1000
FlushInterval time.Duration // max time before flush, default: 5s
MaxWorkers int // concurrent flush workers, default: 4
// Event validation
ValidateSchema bool // default: true
MaxEventSize int // bytes, default: 64KB
// Aggregation settings
AggregateInterval time.Duration // default: 1h
RetentionPeriod time.Duration // raw events, default: 90d
}Environment variables
# Metering
METERING_BUFFER_SIZE=1000
METERING_FLUSH_INTERVAL=5s
METERING_MAX_WORKERS=4
METERING_VALIDATE_SCHEMA=true
METERING_MAX_EVENT_SIZE=65536
METERING_AGGREGATE_INTERVAL=1h
METERING_RETENTION_PERIOD=2160h # 90 daysFeature flags
type FeatureConfig struct {
// Enable experimental features
EnableTrials bool // default: true
EnableMeteredBilling bool // default: true
EnableInvoicing bool // default: true
EnableWebhooks bool // default: true
// Multi-tenancy
EnableTenantIsolation bool // default: true
MaxTenantsPerOrg int // default: unlimited (0)
// Caching
EnableEntitlementCache bool // default: true
CacheTTL time.Duration // default: 1m
}Environment variables
# Features
ENABLE_TRIALS=true
ENABLE_METERED_BILLING=true
ENABLE_INVOICING=true
ENABLE_WEBHOOKS=true
ENABLE_TENANT_ISOLATION=true
ENABLE_ENTITLEMENT_CACHE=true
CACHE_TTL=1mComplete example
Development configuration
# .env.development
DATABASE_URL=postgres://ledger:ledger@localhost:5432/ledger_dev?sslmode=disable
DATABASE_AUTO_MIGRATE=true
SERVER_HOST=localhost
SERVER_PORT=8080
PAYMENT_PROVIDER=mock
ENABLE_WEBHOOKS=false
METERING_BUFFER_SIZE=100
METERING_FLUSH_INTERVAL=1sProduction configuration
# .env.production
DATABASE_URL=postgres://ledger:securepass@db.example.com:5432/ledger?sslmode=require
DATABASE_MAX_OPEN_CONNS=50
DATABASE_MAX_IDLE_CONNS=10
DATABASE_AUTO_MIGRATE=false
SERVER_HOST=0.0.0.0
SERVER_PORT=443
SERVER_READ_TIMEOUT=60s
SERVER_WRITE_TIMEOUT=60s
CORS_ALLOWED_ORIGINS=https://app.example.com
RATE_LIMIT_PER_MINUTE=5000
PAYMENT_PROVIDER=stripe
STRIPE_SECRET_KEY=sk_live_...
STRIPE_WEBHOOK_SECRET=whsec_...
METERING_BUFFER_SIZE=5000
METERING_FLUSH_INTERVAL=10s
METERING_MAX_WORKERS=8
ENABLE_ENTITLEMENT_CACHE=true
CACHE_TTL=5mLoading configuration
From environment
import "github.com/xraph/ledger/config"
func main() {
cfg, err := config.LoadFromEnv()
if err != nil {
log.Fatal(err)
}
// Use configuration
app, err := ledger.New(cfg)
if err != nil {
log.Fatal(err)
}
}From file
import "github.com/xraph/ledger/config"
func main() {
cfg, err := config.LoadFromFile("config.yaml")
if err != nil {
log.Fatal(err)
}
app, err := ledger.New(cfg)
if err != nil {
log.Fatal(err)
}
}Programmatic
cfg := &config.Config{
Database: config.DatabaseConfig{
URL: "postgres://localhost:5432/ledger",
},
Server: config.ServerConfig{
Port: 8080,
},
Payments: config.PaymentConfig{
Provider: "stripe",
Stripe: config.StripeConfig{
SecretKey: os.Getenv("STRIPE_SECRET_KEY"),
},
},
}
app, err := ledger.New(cfg)Validation
Configuration is validated on load. Common validation rules:
DATABASE_URLmust be a valid PostgreSQL connection stringPAYMENT_PROVIDERmust be one of:stripe,paddle,mock- Payment provider credentials must be present for the selected provider
- Port numbers must be in valid range (1-65535)
- Timeouts and intervals must be positive durations
- Buffer sizes and worker counts must be positive integers
Security best practices
- Never commit secrets — Use environment variables or secret management
- Rotate credentials — Regular rotation of API keys and database passwords
- Use SSL/TLS — Enable
sslmode=requirefor database in production - Validate webhooks — Always configure webhook secrets for payment providers
- Rate limiting — Enable and tune rate limits for public APIs
- Least privilege — Database user should have minimal required permissions