Add flexible configuration with custom file path support

\n- Add DLC_CONFIG_FILE environment variable for custom config paths\n- Update config.go to support both default and custom config file locations\n- Update README.md and AGENTS.md with configuration documentation\n- Remove /Users/gabrielradureau/.dancelessonscoach/ directory approach\n- Add os import to config.go\n- Test all configuration scenarios
This commit is contained in:
Gabriel Radureau
2026-04-03 15:50:18 +02:00
parent f50de95f05
commit eaaa63d74e
4 changed files with 142 additions and 10 deletions

View File

@@ -2,6 +2,7 @@ package config
import (
"fmt"
"os"
"time"
"github.com/rs/zerolog/log"
@@ -19,7 +20,9 @@ type Config struct {
}
}
// LoadConfig loads configuration from environment variables and defaults
// LoadConfig loads configuration from file, environment variables, and defaults
// Configuration priority: file > environment variables > defaults
// To specify a custom config file path, set DLC_CONFIG_FILE environment variable
func LoadConfig() (*Config, error) {
v := viper.New()
@@ -28,6 +31,28 @@ func LoadConfig() (*Config, error) {
v.SetDefault("server.port", 8080)
v.SetDefault("shutdown.timeout", 30*time.Second)
// Check for custom config file path via environment variable
if configFile := os.Getenv("DLC_CONFIG_FILE"); configFile != "" {
v.SetConfigFile(configFile)
log.Info().Str("config_file", configFile).Msg("Using custom config file path")
} else {
// Default: look for config.yaml in current directory
v.SetConfigName("config")
v.SetConfigType("yaml")
v.AddConfigPath(".")
}
// Read config file if it exists
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
// Config file was found but there was an error reading it
log.Warn().Err(err).Msg("Error reading config file, using defaults")
}
// Config file not found, continue with environment variables and defaults
} else {
log.Info().Str("config_file", v.ConfigFileUsed()).Msg("Config file loaded")
}
// Bind environment variables
v.AutomaticEnv()
v.SetEnvPrefix("DLC") // DanceLessonsCoach prefix