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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -14,8 +14,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// Parse CLI flags
|
||||||
|
var configPath string
|
||||||
|
flag.StringVar(&configPath, "config", "config/config.yaml", "path to config file")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
// Load Configuration
|
// Load Configuration
|
||||||
cfg, err := config.Load("config/config.yaml", "APP")
|
cfg, err := config.Load(configPath, "APP")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
|
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@@ -23,7 +28,7 @@ func main() {
|
|||||||
|
|
||||||
// Setup Logger
|
// Setup Logger
|
||||||
opts := &slog.HandlerOptions{
|
opts := &slog.HandlerOptions{
|
||||||
Level: parseLogLevel(cfg.LogLevel),
|
Level: config.ParseLogLevel(cfg.LogLevel),
|
||||||
}
|
}
|
||||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, opts))
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, opts))
|
||||||
slog.SetDefault(logger)
|
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 {
|
func run(ctx context.Context) error {
|
||||||
slog.Info("Starting application...")
|
slog.Info("Starting application...")
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -14,6 +15,13 @@ import (
|
|||||||
// Config holds the application configuration.
|
// Config holds the application configuration.
|
||||||
type Config struct {
|
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.
|
// 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
|
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.
|
// overrideFromEnv uses reflection to traverse the struct and update fields from env vars.
|
||||||
func overrideFromEnv(cfg interface{}, prefix string) error {
|
func overrideFromEnv(cfg interface{}, prefix string) error {
|
||||||
v := reflect.ValueOf(cfg)
|
v := reflect.ValueOf(cfg)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -13,6 +14,9 @@ func TestLoad(t *testing.T) {
|
|||||||
|
|
||||||
yamlContent := []byte(`
|
yamlContent := []byte(`
|
||||||
log_level: "info"
|
log_level: "info"
|
||||||
|
server:
|
||||||
|
host: "localhost"
|
||||||
|
port: 8080
|
||||||
`)
|
`)
|
||||||
if err := os.WriteFile(configFile, yamlContent, 0644); err != nil {
|
if err := os.WriteFile(configFile, yamlContent, 0644); err != nil {
|
||||||
t.Fatalf("failed to write temp config file: %v", err)
|
t.Fatalf("failed to write temp config file: %v", err)
|
||||||
@@ -26,11 +30,23 @@ log_level: "info"
|
|||||||
if cfg.LogLevel != "info" {
|
if cfg.LogLevel != "info" {
|
||||||
t.Errorf("expected LogLevel 'info', got '%s'", cfg.LogLevel)
|
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) {
|
t.Run("Override from Env", func(t *testing.T) {
|
||||||
os.Setenv("TEST_LOG_LEVEL", "debug")
|
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")
|
cfg, err := Load(configFile, "TEST")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -39,5 +55,34 @@ log_level: "info"
|
|||||||
if cfg.LogLevel != "debug" {
|
if cfg.LogLevel != "debug" {
|
||||||
t.Errorf("expected LogLevel 'debug', got '%s'", cfg.LogLevel)
|
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