feat(config): support cli config path and nested config
Adds -config CLI flag to main.go. Moves ParseLogLevel to internal/config. Adds ServerConfig nested struct to Config and corresponding tests for yaml/env overrides.
This commit is contained in:
@@ -2,11 +2,11 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -14,8 +14,13 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Parse CLI flags
|
||||
var configPath string
|
||||
flag.StringVar(&configPath, "config", "config/config.yaml", "path to config file")
|
||||
flag.Parse()
|
||||
|
||||
// Load Configuration
|
||||
cfg, err := config.Load("config/config.yaml", "APP")
|
||||
cfg, err := config.Load(configPath, "APP")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
|
||||
os.Exit(1)
|
||||
@@ -23,7 +28,7 @@ func main() {
|
||||
|
||||
// Setup Logger
|
||||
opts := &slog.HandlerOptions{
|
||||
Level: parseLogLevel(cfg.LogLevel),
|
||||
Level: config.ParseLogLevel(cfg.LogLevel),
|
||||
}
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, opts))
|
||||
slog.SetDefault(logger)
|
||||
@@ -46,21 +51,6 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func parseLogLevel(level string) slog.Level {
|
||||
switch strings.ToLower(level) {
|
||||
case "debug":
|
||||
return slog.LevelDebug
|
||||
case "info":
|
||||
return slog.LevelInfo
|
||||
case "warn":
|
||||
return slog.LevelWarn
|
||||
case "error":
|
||||
return slog.LevelError
|
||||
default:
|
||||
return slog.LevelInfo
|
||||
}
|
||||
}
|
||||
|
||||
func run(ctx context.Context) error {
|
||||
slog.Info("Starting application...")
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
@@ -13,7 +14,14 @@ import (
|
||||
|
||||
// Config holds the application configuration.
|
||||
type Config struct {
|
||||
LogLevel string `yaml:"log_level"`
|
||||
LogLevel string `yaml:"log_level"`
|
||||
Server ServerConfig `yaml:"server"`
|
||||
}
|
||||
|
||||
// ServerConfig holds server-specific configuration.
|
||||
type ServerConfig struct {
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
}
|
||||
|
||||
// Load reads configuration from a YAML file and overrides it with environment variables.
|
||||
@@ -42,6 +50,22 @@ func Load(configPath string, envPrefix string) (*Config, error) {
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// ParseLogLevel converts a string log level to a slog.Level.
|
||||
func ParseLogLevel(level string) slog.Level {
|
||||
switch strings.ToLower(level) {
|
||||
case "debug":
|
||||
return slog.LevelDebug
|
||||
case "info":
|
||||
return slog.LevelInfo
|
||||
case "warn":
|
||||
return slog.LevelWarn
|
||||
case "error":
|
||||
return slog.LevelError
|
||||
default:
|
||||
return slog.LevelInfo
|
||||
}
|
||||
}
|
||||
|
||||
// overrideFromEnv uses reflection to traverse the struct and update fields from env vars.
|
||||
func overrideFromEnv(cfg interface{}, prefix string) error {
|
||||
v := reflect.ValueOf(cfg)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -13,6 +14,9 @@ func TestLoad(t *testing.T) {
|
||||
|
||||
yamlContent := []byte(`
|
||||
log_level: "info"
|
||||
server:
|
||||
host: "localhost"
|
||||
port: 8080
|
||||
`)
|
||||
if err := os.WriteFile(configFile, yamlContent, 0644); err != nil {
|
||||
t.Fatalf("failed to write temp config file: %v", err)
|
||||
@@ -26,11 +30,23 @@ log_level: "info"
|
||||
if cfg.LogLevel != "info" {
|
||||
t.Errorf("expected LogLevel 'info', got '%s'", cfg.LogLevel)
|
||||
}
|
||||
if cfg.Server.Host != "localhost" {
|
||||
t.Errorf("expected Server.Host 'localhost', got '%s'", cfg.Server.Host)
|
||||
}
|
||||
if cfg.Server.Port != 8080 {
|
||||
t.Errorf("expected Server.Port 8080, got %d", cfg.Server.Port)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Override from Env", func(t *testing.T) {
|
||||
os.Setenv("TEST_LOG_LEVEL", "debug")
|
||||
defer os.Unsetenv("TEST_LOG_LEVEL")
|
||||
os.Setenv("TEST_SERVER_PORT", "9090")
|
||||
os.Setenv("TEST_SERVER_HOST", "0.0.0.0")
|
||||
defer func() {
|
||||
os.Unsetenv("TEST_LOG_LEVEL")
|
||||
os.Unsetenv("TEST_SERVER_PORT")
|
||||
os.Unsetenv("TEST_SERVER_HOST")
|
||||
}()
|
||||
|
||||
cfg, err := Load(configFile, "TEST")
|
||||
if err != nil {
|
||||
@@ -39,5 +55,34 @@ log_level: "info"
|
||||
if cfg.LogLevel != "debug" {
|
||||
t.Errorf("expected LogLevel 'debug', got '%s'", cfg.LogLevel)
|
||||
}
|
||||
if cfg.Server.Port != 9090 {
|
||||
t.Errorf("expected Server.Port 9090, got %d", cfg.Server.Port)
|
||||
}
|
||||
if cfg.Server.Host != "0.0.0.0" {
|
||||
t.Errorf("expected Server.Host '0.0.0.0', got '%s'", cfg.Server.Host)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestParseLogLevel(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected slog.Level
|
||||
}{
|
||||
{"debug", slog.LevelDebug},
|
||||
{"DEBUG", slog.LevelDebug},
|
||||
{"info", slog.LevelInfo},
|
||||
{"warn", slog.LevelWarn},
|
||||
{"error", slog.LevelError},
|
||||
{"unknown", slog.LevelInfo},
|
||||
{"", slog.LevelInfo},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
if got := ParseLogLevel(tt.input); got != tt.expected {
|
||||
t.Errorf("ParseLogLevel(%q) = %v, want %v", tt.input, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user