Documentation
¶
Overview ¶
Package config provides configuration management for CQI infrastructure components. It supports loading configuration from YAML files, JSON files, and environment variables with automatic validation and default value application.
Example usage:
cfg, err := config.Load("config.yaml", "CQI")
if err != nil {
log.Fatal(err)
}
// Or panic on error:
cfg := config.MustLoad("config.yaml", "CQI")
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AuthConfig ¶
type AuthConfig struct {
// APIKeys is a list of valid API keys for API key authentication.
// Each key should be a secure random string.
APIKeys []string `mapstructure:"api_keys"`
// JWTPublicKeyPath is the path to the RSA public key file (PEM format)
// used to verify JWT signatures. If empty, JWT authentication is disabled.
JWTPublicKeyPath string `mapstructure:"jwt_public_key_path"`
// JWTIssuer is the expected value of the "iss" (issuer) claim in JWT tokens.
// If empty, issuer validation is skipped.
JWTIssuer string `mapstructure:"jwt_issuer"`
// JWTAudience is the expected value of the "aud" (audience) claim in JWT tokens.
// If empty, audience validation is skipped.
JWTAudience string `mapstructure:"jwt_audience"`
}
AuthConfig contains authentication configuration.
type CacheConfig ¶
type CacheConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Password string `mapstructure:"password"`
DB int `mapstructure:"db"`
MaxRetries int `mapstructure:"max_retries"`
DialTimeout time.Duration `mapstructure:"dial_timeout"`
ReadTimeout time.Duration `mapstructure:"read_timeout"`
WriteTimeout time.Duration `mapstructure:"write_timeout"`
PoolSize int `mapstructure:"pool_size"`
MinIdleConns int `mapstructure:"min_idle_conns"`
DefaultTTL time.Duration `mapstructure:"default_ttl"`
}
CacheConfig contains Redis cache configuration.
type Config ¶
type Config struct {
Service ServiceConfig `mapstructure:"service"`
Server ServerConfig `mapstructure:"server"`
Database DatabaseConfig `mapstructure:"database"`
Cache CacheConfig `mapstructure:"cache"`
EventBus EventBusConfig `mapstructure:"eventbus"`
Log LogConfig `mapstructure:"log"`
Metrics MetricsConfig `mapstructure:"metrics"`
Tracing TracingConfig `mapstructure:"tracing"`
Auth AuthConfig `mapstructure:"auth"`
Registry RegistryConfig `mapstructure:"registry"`
Runner RunnerConfig `mapstructure:"runner"`
}
Config represents the complete configuration for a CQI-based service.
func Load ¶
Load loads configuration from a file and environment variables. The prefix parameter is used for environment variable names (e.g., "CQI" -> CQI_DATABASE_HOST). If configPath is empty, only environment variables will be used.
func LoadFromEnv ¶
LoadFromEnv loads configuration only from environment variables (no config file).
func MustLoad ¶
MustLoad loads configuration and panics on error. This is useful in main() where configuration errors should be fatal.
func MustLoadFromEnv ¶
MustLoadFromEnv loads configuration from environment variables and panics on error.
type DatabaseConfig ¶
type DatabaseConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Database string `mapstructure:"database"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
SSLMode string `mapstructure:"ssl_mode"` // disable, require, verify-ca, verify-full
MaxConns int `mapstructure:"max_conns"`
MinConns int `mapstructure:"min_conns"`
MaxConnLifetime time.Duration `mapstructure:"max_conn_lifetime"`
MaxConnIdleTime time.Duration `mapstructure:"max_conn_idle_time"`
ConnectTimeout time.Duration `mapstructure:"connect_timeout"`
QueryTimeout time.Duration `mapstructure:"query_timeout"`
}
DatabaseConfig contains PostgreSQL connection configuration.
type EventBusConfig ¶
type EventBusConfig struct {
Backend string `mapstructure:"backend"` // "jetstream" or "memory"
Servers []string `mapstructure:"servers"` // NATS server URLs
StreamName string `mapstructure:"stream_name"` // JetStream stream name
ConsumerName string `mapstructure:"consumer_name"` // Durable consumer name
MaxDeliver int `mapstructure:"max_deliver"` // Max delivery attempts
AckWait time.Duration `mapstructure:"ack_wait"` // Acknowledgment timeout
MaxAckPending int `mapstructure:"max_ack_pending"` // Max outstanding unacked messages
}
EventBusConfig contains event bus (NATS JetStream) configuration.
type LogConfig ¶
type LogConfig struct {
Level string `mapstructure:"level"` // debug, info, warn, error
Format string `mapstructure:"format"` // json, console
Output string `mapstructure:"output"` // stdout, stderr, file path
}
LogConfig contains structured logging configuration.
type MetricsConfig ¶
type MetricsConfig struct {
Enabled bool `mapstructure:"enabled"`
Port int `mapstructure:"port"`
Path string `mapstructure:"path"`
Namespace string `mapstructure:"namespace"` // Metric prefix
}
MetricsConfig contains Prometheus metrics configuration.
type RegistryConfig ¶
type RegistryConfig struct {
// Backend is the registry backend type: "local" or "redis".
// "local" uses in-memory storage (development/testing).
// "redis" uses Redis for distributed service discovery (production).
Backend string `mapstructure:"backend"`
// RedisAddr is the Redis server address (host:port) when using Redis backend.
RedisAddr string `mapstructure:"redis_addr"`
// RedisPassword is the Redis password (optional).
RedisPassword string `mapstructure:"redis_password"`
// RedisDB is the Redis database number (default: 0).
RedisDB int `mapstructure:"redis_db"`
// TTL is the time-to-live for service registrations in Redis.
// Services must send heartbeats more frequently than this to stay registered.
// Default: 30 seconds.
TTL time.Duration `mapstructure:"ttl"`
// HeartbeatInterval is how often to send heartbeat updates in Redis.
// Should be less than TTL to prevent expiration.
// Default: 10 seconds.
HeartbeatInterval time.Duration `mapstructure:"heartbeat_interval"`
}
RegistryConfig contains service registry configuration.
type RunnerConfig ¶
type RunnerConfig struct {
// RestartPolicy is the default restart policy for services: "never", "always", or "on-failure".
// Default: "on-failure".
RestartPolicy string `mapstructure:"restart_policy"`
// MaxRetries is the maximum number of restart attempts (0 = unlimited).
// Default: 5.
MaxRetries int `mapstructure:"max_retries"`
// InitialBackoff is the initial delay before first restart.
// Default: 1 second.
InitialBackoff time.Duration `mapstructure:"initial_backoff"`
// MaxBackoff is the maximum delay between restarts.
// Default: 60 seconds.
MaxBackoff time.Duration `mapstructure:"max_backoff"`
// BackoffMultiplier is the factor by which the backoff increases on each retry.
// Default: 2.0.
BackoffMultiplier float64 `mapstructure:"backoff_multiplier"`
// EnableJitter adds randomness to backoff (±25%) to prevent thundering herd.
// Default: true.
EnableJitter bool `mapstructure:"enable_jitter"`
}
RunnerConfig contains service orchestration runner configuration.
type ServerConfig ¶
type ServerConfig struct {
HTTPPort int `mapstructure:"http_port"`
GRPCPort int `mapstructure:"grpc_port"`
ReadTimeout time.Duration `mapstructure:"read_timeout"`
WriteTimeout time.Duration `mapstructure:"write_timeout"`
ShutdownTimeout time.Duration `mapstructure:"shutdown_timeout"`
MaxHeaderBytes int `mapstructure:"max_header_bytes"`
EnableReflection bool `mapstructure:"enable_reflection"` // gRPC reflection
}
ServerConfig contains HTTP/gRPC server configuration.
type ServiceConfig ¶
type ServiceConfig struct {
Name string `mapstructure:"name"`
Version string `mapstructure:"version"`
Env string `mapstructure:"env"` // development, staging, production
}
ServiceConfig contains general service information.
type TracingConfig ¶
type TracingConfig struct {
Enabled bool `mapstructure:"enabled"`
Endpoint string `mapstructure:"endpoint"` // OTLP endpoint (e.g., "localhost:4317")
SampleRate float64 `mapstructure:"sample_rate"` // 0.0 to 1.0
ServiceName string `mapstructure:"service_name"` // Override service name for traces
Environment string `mapstructure:"environment"` // Environment tag
ExportMode string `mapstructure:"export_mode"` // "grpc" or "http"
Insecure bool `mapstructure:"insecure"` // Use insecure connection
BatchTimeout time.Duration `mapstructure:"batch_timeout"` // Batch export timeout
}
TracingConfig contains OpenTelemetry tracing configuration.