config

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package config provides centralized configuration management for HR System services. It supports loading configuration from YAML files and environment variables with hot reload.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsDevelopment

func IsDevelopment() bool

IsDevelopment returns true if the environment is development.

func IsProduction

func IsProduction() bool

IsProduction returns true if the environment is production.

Types

type CircuitBreakerConfig

type CircuitBreakerConfig struct {
	Enabled       bool          `yaml:"enabled" mapstructure:"enabled"`
	MaxRequests   uint32        `yaml:"max_requests" mapstructure:"max_requests"`
	Interval      time.Duration `yaml:"interval" mapstructure:"interval"`
	Timeout       time.Duration `yaml:"timeout" mapstructure:"timeout"`
	ReadyToTrip   uint32        `yaml:"ready_to_trip" mapstructure:"ready_to_trip"`
	OnStateChange bool          `yaml:"on_state_change" mapstructure:"on_state_change"`
}

CircuitBreakerConfig contains circuit breaker configuration.

type Config

type Config struct {
	Server         ServerConfig         `yaml:"server" mapstructure:"server"`
	Database       DatabaseConfig       `yaml:"database" mapstructure:"database"`
	Redis          RedisConfig          `yaml:"redis" mapstructure:"redis"`
	JWT            JWTConfig            `yaml:"jwt" mapstructure:"jwt"`
	Logging        LoggingConfig        `yaml:"logging" mapstructure:"logging"`
	Metrics        MetricsConfig        `yaml:"metrics" mapstructure:"metrics"`
	Tracing        TracingConfig        `yaml:"tracing" mapstructure:"tracing"`
	CircuitBreaker CircuitBreakerConfig `yaml:"circuit_breaker" mapstructure:"circuit_breaker"`
	Retry          RetryConfig          `yaml:"retry" mapstructure:"retry"`
	RateLimit      RateLimitConfig      `yaml:"rate_limit" mapstructure:"rate_limit"`
	Security       SecurityConfig       `yaml:"security" mapstructure:"security"`
	Profiling      ProfilingConfig      `yaml:"profiling" mapstructure:"profiling"`
	HealthCheck    HealthCheckConfig    `yaml:"health_check" mapstructure:"health_check"`
}

Config represents the complete configuration for an HR System service.

func Load

func Load(configPath string) (*Config, error)

Load loads configuration from a YAML file and environment variables. Environment variables take precedence over file values.

func LoadFromEnv

func LoadFromEnv() *Config

LoadFromEnv loads configuration only from environment variables.

type DatabaseConfig

type DatabaseConfig struct {
	Host            string        `yaml:"host" mapstructure:"host"`
	Port            string        `yaml:"port" mapstructure:"port"`
	User            string        `yaml:"user" mapstructure:"user"`
	Password        string        `yaml:"password" mapstructure:"password"`
	Name            string        `yaml:"name" mapstructure:"name"`
	SSLMode         string        `yaml:"ssl_mode" mapstructure:"ssl_mode"`
	MaxOpenConns    int           `yaml:"max_open_conns" mapstructure:"max_open_conns"`
	MaxIdleConns    int           `yaml:"max_idle_conns" mapstructure:"max_idle_conns"`
	ConnMaxLifetime time.Duration `yaml:"conn_max_lifetime" mapstructure:"conn_max_lifetime"`
	ConnMaxIdleTime time.Duration `yaml:"conn_max_idle_time" mapstructure:"conn_max_idle_time"`
}

DatabaseConfig contains database connection configuration.

func (DatabaseConfig) DSN

func (d DatabaseConfig) DSN() string

DSN returns the database connection string.

type HealthCheckConfig

type HealthCheckConfig struct {
	Enabled  bool          `yaml:"enabled" mapstructure:"enabled"`
	Path     string        `yaml:"path" mapstructure:"path"`
	Interval time.Duration `yaml:"interval" mapstructure:"interval"`
	Timeout  time.Duration `yaml:"timeout" mapstructure:"timeout"`
}

HealthCheckConfig contains health check configuration.

type JWTConfig

type JWTConfig struct {
	Secret   string        `yaml:"secret" mapstructure:"secret"`
	Expiry   time.Duration `yaml:"expiry" mapstructure:"expiry"`
	Issuer   string        `yaml:"issuer" mapstructure:"issuer"`
	Audience string        `yaml:"audience" mapstructure:"audience"`
}

JWTConfig contains JWT token configuration.

type LoggingConfig

type LoggingConfig struct {
	Level      string `yaml:"level" mapstructure:"level"`
	Format     string `yaml:"format" mapstructure:"format"`
	Output     string `yaml:"output" mapstructure:"output"`
	Filename   string `yaml:"filename" mapstructure:"filename"`
	MaxSize    int    `yaml:"max_size" mapstructure:"max_size"`
	MaxBackups int    `yaml:"max_backups" mapstructure:"max_backups"`
	MaxAge     int    `yaml:"max_age" mapstructure:"max_age"`
	Compress   bool   `yaml:"compress" mapstructure:"compress"`
}

LoggingConfig contains logging configuration.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager provides enterprise-grade configuration management with hot reload.

func GetGlobalManager

func GetGlobalManager() *Manager

GetGlobalManager returns the global configuration manager instance.

func NewManager

func NewManager(logger *zap.Logger) *Manager

NewManager creates a new configuration manager.

func (*Manager) AddWatcher

func (m *Manager) AddWatcher(watcher func(*Config))

AddWatcher adds a configuration change watcher.

func (*Manager) Close

func (m *Manager) Close() error

Close stops the configuration manager and cleans up resources.

func (*Manager) EnableHotReload

func (m *Manager) EnableHotReload() error

EnableHotReload enables hot reloading of configuration files.

func (*Manager) Export

func (m *Manager) Export() map[string]interface{}

Export exports the current configuration as a map.

func (*Manager) ExportYAML

func (m *Manager) ExportYAML() ([]byte, error)

ExportYAML exports the current configuration as YAML.

func (*Manager) GetBool

func (m *Manager) GetBool(key string, fallback ...bool) bool

GetBool gets a boolean configuration value with fallback.

func (*Manager) GetConfig

func (m *Manager) GetConfig() *Config

GetConfig returns the current configuration (thread-safe).

func (*Manager) GetDuration

func (m *Manager) GetDuration(key string, fallback ...time.Duration) time.Duration

GetDuration gets a duration configuration value with fallback.

func (*Manager) GetInt

func (m *Manager) GetInt(key string, fallback ...int) int

GetInt gets an integer configuration value with fallback.

func (*Manager) GetString

func (m *Manager) GetString(key string, fallback ...string) string

GetString gets a string configuration value with fallback.

func (*Manager) GetStringSlice

func (m *Manager) GetStringSlice(key string) []string

GetStringSlice gets a string slice configuration value.

func (*Manager) IsFeatureEnabled

func (m *Manager) IsFeatureEnabled(feature string) bool

IsFeatureEnabled checks if a feature flag is enabled.

func (*Manager) Load

func (m *Manager) Load(configPath string) (*Config, error)

Load loads configuration using the manager.

func (*Manager) LoadFromEnv

func (m *Manager) LoadFromEnv() *Config

LoadFromEnv loads configuration only from environment variables using the manager.

func (*Manager) Reload

func (m *Manager) Reload() error

Reload manually reloads the configuration.

func (*Manager) RemoveWatcher

func (m *Manager) RemoveWatcher(watcher func(*Config))

RemoveWatcher removes a configuration change watcher.

func (*Manager) Set

func (m *Manager) Set(key string, value interface{})

Set sets a configuration value (useful for testing).

type MetricsConfig

type MetricsConfig struct {
	Enabled bool   `yaml:"enabled" mapstructure:"enabled"`
	Path    string `yaml:"path" mapstructure:"path"`
	Port    string `yaml:"port" mapstructure:"port"`
}

MetricsConfig contains metrics configuration.

type ProfilingConfig

type ProfilingConfig struct {
	Enabled bool   `yaml:"enabled" mapstructure:"enabled"`
	Port    string `yaml:"port" mapstructure:"port"`
	Path    string `yaml:"path" mapstructure:"path"`
}

ProfilingConfig contains profiling configuration.

type RateLimitConfig

type RateLimitConfig struct {
	Enabled         bool          `yaml:"enabled" mapstructure:"enabled"`
	RequestsPerSec  int           `yaml:"requests_per_sec" mapstructure:"requests_per_sec"`
	Burst           int           `yaml:"burst" mapstructure:"burst"`
	WindowSize      time.Duration `yaml:"window_size" mapstructure:"window_size"`
	CleanupInterval time.Duration `yaml:"cleanup_interval" mapstructure:"cleanup_interval"`
}

RateLimitConfig contains rate limiting configuration.

type RedisConfig

type RedisConfig struct {
	Addr         string        `yaml:"addr" mapstructure:"addr"`
	Password     string        `yaml:"password" mapstructure:"password"`
	DB           int           `yaml:"db" mapstructure:"db"`
	PoolSize     int           `yaml:"pool_size" mapstructure:"pool_size"`
	MinIdleConns int           `yaml:"min_idle_conns" mapstructure:"min_idle_conns"`
	DialTimeout  time.Duration `yaml:"dial_timeout" mapstructure:"dial_timeout"`
	ReadTimeout  time.Duration `yaml:"read_timeout" mapstructure:"read_timeout"`
	WriteTimeout time.Duration `yaml:"write_timeout" mapstructure:"write_timeout"`
}

RedisConfig contains Redis connection configuration.

type RetryConfig

type RetryConfig struct {
	Enabled             bool          `yaml:"enabled" mapstructure:"enabled"`
	MaxRetries          int           `yaml:"max_retries" mapstructure:"max_retries"`
	InitialInterval     time.Duration `yaml:"initial_interval" mapstructure:"initial_interval"`
	MaxInterval         time.Duration `yaml:"max_interval" mapstructure:"max_interval"`
	Multiplier          float64       `yaml:"multiplier" mapstructure:"multiplier"`
	RandomizationFactor float64       `yaml:"randomization_factor" mapstructure:"randomization_factor"`
}

RetryConfig contains retry configuration.

type SecurityConfig

type SecurityConfig struct {
	EncryptionKey  string   `yaml:"encryption_key" mapstructure:"encryption_key"`
	AllowedOrigins []string `yaml:"allowed_origins" mapstructure:"allowed_origins"`
	TrustedProxies []string `yaml:"trusted_proxies" mapstructure:"trusted_proxies"`
	SecureHeaders  bool     `yaml:"secure_headers" mapstructure:"secure_headers"`
	CSPPolicy      string   `yaml:"csp_policy" mapstructure:"csp_policy"`
	HSTSMaxAge     int      `yaml:"hsts_max_age" mapstructure:"hsts_max_age"`
}

SecurityConfig contains security configuration.

type ServerConfig

type ServerConfig struct {
	Port         string        `yaml:"port" mapstructure:"port"`
	Host         string        `yaml:"host" mapstructure:"host"`
	ReadTimeout  time.Duration `yaml:"read_timeout" mapstructure:"read_timeout"`
	WriteTimeout time.Duration `yaml:"write_timeout" mapstructure:"write_timeout"`
	IdleTimeout  time.Duration `yaml:"idle_timeout" mapstructure:"idle_timeout"`
	TLS          TLSConfig     `yaml:"tls" mapstructure:"tls"`
}

ServerConfig contains HTTP server configuration.

func (ServerConfig) Address

func (s ServerConfig) Address() string

Address returns the server address.

type TLSConfig

type TLSConfig struct {
	Enabled  bool   `yaml:"enabled" mapstructure:"enabled"`
	CertFile string `yaml:"cert_file" mapstructure:"cert_file"`
	KeyFile  string `yaml:"key_file" mapstructure:"key_file"`
}

TLSConfig contains TLS configuration.

type TracingConfig

type TracingConfig struct {
	Enabled     bool              `yaml:"enabled" mapstructure:"enabled"`
	ServiceName string            `yaml:"service_name" mapstructure:"service_name"`
	Endpoint    string            `yaml:"endpoint" mapstructure:"endpoint"`
	SampleRate  float64           `yaml:"sample_rate" mapstructure:"sample_rate"`
	Exporter    string            `yaml:"exporter" mapstructure:"exporter"`
	Headers     map[string]string `yaml:"headers" mapstructure:"headers"`
}

TracingConfig contains distributed tracing configuration.

Jump to

Keyboard shortcuts

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