Add configuration management with Viper

- Implement pkg/config package with Viper integration
- Support environment variables with DLC_ prefix
- Add configurable server host, port, and shutdown timeout
- Update server to use configuration values
- Add config.example.yaml template file
- Update AGENTS.md with configuration documentation
- Maintain backward compatibility with default values

Configuration options:
- DLC_SERVER_HOST (default: 0.0.0.0)
- DLC_SERVER_PORT (default: 8080)
- DLC_SHUTDOWN_TIMEOUT (default: 30s)
This commit is contained in:
Gabriel Radureau
2026-04-03 13:53:57 +02:00
parent e52870480d
commit 8103f64db9
6 changed files with 199 additions and 6 deletions

57
pkg/config/config.go Normal file
View File

@@ -0,0 +1,57 @@
package config
import (
"fmt"
"time"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)
// Config represents the application configuration
type Config struct {
Server struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
}
Shutdown struct {
Timeout time.Duration `mapstructure:"timeout"`
}
}
// LoadConfig loads configuration from environment variables and defaults
func LoadConfig() (*Config, error) {
v := viper.New()
// Set default values
v.SetDefault("server.host", "0.0.0.0")
v.SetDefault("server.port", 8080)
v.SetDefault("shutdown.timeout", 30*time.Second)
// Bind environment variables
v.AutomaticEnv()
v.SetEnvPrefix("DLC") // DanceLessonsCoach prefix
v.BindEnv("server.host", "DLC_SERVER_HOST")
v.BindEnv("server.port", "DLC_SERVER_PORT")
v.BindEnv("shutdown.timeout", "DLC_SHUTDOWN_TIMEOUT")
// Unmarshal into Config struct
var config Config
if err := v.Unmarshal(&config); err != nil {
log.Error().Err(err).Msg("Failed to unmarshal config")
return nil, fmt.Errorf("config unmarshal error: %w", err)
}
log.Info().
Str("host", config.Server.Host).
Int("port", config.Server.Port).
Dur("shutdown_timeout", config.Shutdown.Timeout).
Msg("Configuration loaded")
return &config, nil
}
// GetServerAddress returns the formatted server address (host:port)
func (c *Config) GetServerAddress() string {
return fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port)
}