feat(config): add yaml config with generic env override
All checks were successful
CI / Test (pull_request) Successful in 23s
CI / Build (pull_request) Successful in 7s
CI / Docker Build (pull_request) Successful in 21s
CI / Lint (pull_request) Successful in 7s

Adds internal/config package using gopkg.in/yaml.v3 and reflection for generic environment variable overrides. Updates main.go to load config and set log level. Adds tests and example config.
This commit is contained in:
Gemini CLI
2026-01-15 18:28:17 +00:00
parent f932c0ff65
commit 6623ced227
6 changed files with 223 additions and 1 deletions

View File

@@ -2,15 +2,30 @@ package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/placeholder/golang-template/internal/config"
)
func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
// Load Configuration
cfg, err := config.Load("config/config.yaml", "APP")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
os.Exit(1)
}
// Setup Logger
opts := &slog.HandlerOptions{
Level: parseLogLevel(cfg.LogLevel),
}
logger := slog.New(slog.NewJSONHandler(os.Stdout, opts))
slog.SetDefault(logger)
ctx, cancel := context.WithCancel(context.Background())
@@ -31,6 +46,21 @@ 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...")