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...")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user