- Add tests for internal/config package (90.9% coverage) - Test all viperConfig getter methods - Test LoadConfig with default and environment-specific configs - Test error handling for missing config files - Add tests for internal/di package (88.1% coverage) - Test Container lifecycle (NewContainer, Start, Stop) - Test providers (ProvideConfig, ProvideLogger, CoreModule) - Test lifecycle hooks registration - Include mock implementations for testing - Add tests for internal/logger package (96.5% coverage) - Test zapLogger with JSON and console formats - Test all logging levels and methods - Test middleware (RequestIDMiddleware, LoggingMiddleware) - Test context helper functions - Include benchmark tests - Update CI workflow to skip tests when no test files exist - Add conditional test execution based on test file presence - Add timeout for test execution - Verify build when no tests are present All tests follow Go best practices with table-driven patterns, parallel execution where safe, and comprehensive coverage.
103 lines
2.9 KiB
Go
103 lines
2.9 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"git.dcentral.systems/toolz/goplt/pkg/config"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
// viperConfig implements the ConfigProvider interface using Viper.
|
|
type viperConfig struct {
|
|
v *viper.Viper
|
|
}
|
|
|
|
// NewViperConfig creates a new Viper-based configuration provider.
|
|
func NewViperConfig(v *viper.Viper) config.ConfigProvider {
|
|
return &viperConfig{v: v}
|
|
}
|
|
|
|
// Get retrieves a configuration value by key.
|
|
func (vc *viperConfig) Get(key string) any {
|
|
return vc.v.Get(key)
|
|
}
|
|
|
|
// Unmarshal unmarshals the entire configuration into the provided struct.
|
|
func (vc *viperConfig) Unmarshal(v any) error {
|
|
return vc.v.Unmarshal(v)
|
|
}
|
|
|
|
// GetString retrieves a string value by key.
|
|
func (vc *viperConfig) GetString(key string) string {
|
|
return vc.v.GetString(key)
|
|
}
|
|
|
|
// GetInt retrieves an integer value by key.
|
|
func (vc *viperConfig) GetInt(key string) int {
|
|
return vc.v.GetInt(key)
|
|
}
|
|
|
|
// GetBool retrieves a boolean value by key.
|
|
func (vc *viperConfig) GetBool(key string) bool {
|
|
return vc.v.GetBool(key)
|
|
}
|
|
|
|
// GetStringSlice retrieves a string slice value by key.
|
|
func (vc *viperConfig) GetStringSlice(key string) []string {
|
|
return vc.v.GetStringSlice(key)
|
|
}
|
|
|
|
// GetDuration retrieves a duration value by key.
|
|
func (vc *viperConfig) GetDuration(key string) time.Duration {
|
|
return vc.v.GetDuration(key)
|
|
}
|
|
|
|
// IsSet checks if a configuration key is set.
|
|
func (vc *viperConfig) IsSet(key string) bool {
|
|
return vc.v.IsSet(key)
|
|
}
|
|
|
|
// LoadConfig loads configuration from files and environment variables.
|
|
// It follows this precedence order (highest to lowest):
|
|
// 1. Environment variables
|
|
// 2. Environment-specific YAML files (development.yaml, production.yaml)
|
|
// 3. Default YAML file (default.yaml)
|
|
//
|
|
// The env parameter determines which environment-specific file to load.
|
|
// Supported values: "development", "production", or empty string for default only.
|
|
func LoadConfig(env string) (config.ConfigProvider, error) {
|
|
v := viper.New()
|
|
|
|
// Set default configuration file name
|
|
v.SetConfigName("default")
|
|
v.SetConfigType("yaml")
|
|
v.AddConfigPath("config")
|
|
|
|
// Read default configuration
|
|
if err := v.ReadInConfig(); err != nil {
|
|
return nil, fmt.Errorf("failed to read default config: %w", err)
|
|
}
|
|
|
|
// Load environment-specific configuration if specified
|
|
if env != "" {
|
|
v.SetConfigName(env)
|
|
// Merge environment-specific config (if it exists)
|
|
if err := v.MergeInConfig(); err != nil {
|
|
// Environment-specific file is optional, so we only warn
|
|
// but don't fail if it doesn't exist
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
|
return nil, fmt.Errorf("failed to merge environment config: %w", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Enable environment variable support
|
|
v.AutomaticEnv()
|
|
// Environment variables can be set in UPPER_SNAKE_CASE format
|
|
// and will automatically map to nested keys (e.g., SERVER_PORT -> server.port)
|
|
// Viper handles this automatically with AutomaticEnv()
|
|
|
|
return NewViperConfig(v), nil
|
|
}
|