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

View File

@@ -7,13 +7,19 @@ import (
"os"
"os/signal"
"syscall"
"time"
"DanceLessonsCoach/pkg/config"
"DanceLessonsCoach/pkg/server"
"github.com/rs/zerolog/log"
)
func main() {
// Load configuration
cfg, err := config.LoadConfig()
if err != nil {
log.Fatal().Err(err).Msg("Failed to load configuration")
}
// Create root context with cancellation
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
@@ -27,11 +33,11 @@ func main() {
serverCtx, serverStop := context.WithCancel(ctx)
go func() {
fmt.Println("Server running on :8080")
log.Info().Msg("Starting HTTP server on :8080")
fmt.Printf("Server running on %s\n", cfg.GetServerAddress())
log.Info().Str("address", cfg.GetServerAddress()).Msg("Starting HTTP server")
srv := &http.Server{
Addr: ":8080",
Addr: cfg.GetServerAddress(),
Handler: server.Router(),
}
@@ -40,8 +46,8 @@ func main() {
<-sigChan
log.Info().Msg("Shutdown signal received")
// Create shutdown context with timeout
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
// Create shutdown context with timeout from config
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), cfg.Shutdown.Timeout)
defer shutdownCancel()
if err := srv.Shutdown(shutdownCtx); err != nil {