Implemented all 5 stories from Epic 0: Story 0.1: Project Initialization - Initialize Go module with path git.dcentral.systems/toolz/goplt - Create complete directory structure (cmd/, internal/, pkg/, modules/, config/, etc.) - Add comprehensive .gitignore for Go projects - Create README.md with project overview and setup instructions Story 0.2: Configuration Management System - Define ConfigProvider interface in pkg/config - Implement Viper-based configuration in internal/config - Create configuration loader with environment support - Add default, development, and production YAML config files Story 0.3: Structured Logging System - Define Logger interface in pkg/logger - Implement Zap-based logger in internal/logger - Add request ID middleware for Gin - Create global logger export with convenience functions - Support context-aware logging with request/user ID extraction Story 0.4: CI/CD Pipeline - Create GitHub Actions workflow for CI (test, lint, build, fmt) - Add comprehensive Makefile with development commands - Configure golangci-lint with reasonable defaults Story 0.5: Dependency Injection and Bootstrap - Create FX-based DI container in internal/di - Implement provider functions for Config and Logger - Create application entry point in cmd/platform/main.go - Add lifecycle management with graceful shutdown All acceptance criteria met: - go build ./cmd/platform succeeds - go test ./... runs successfully - go mod verify passes - Config loads from config/default.yaml - Logger can be injected and used - Application starts and shuts down gracefully
103 lines
2.9 KiB
Go
103 lines
2.9 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
"git.dcentral.systems/toolz/goplt/pkg/config"
|
|
)
|
|
|
|
// 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
|
|
}
|