feat(config): add yaml config with generic env override
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:
@@ -2,15 +2,30 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/placeholder/golang-template/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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)
|
slog.SetDefault(logger)
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
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 {
|
func run(ctx context.Context) error {
|
||||||
slog.Info("Starting application...")
|
slog.Info("Starting application...")
|
||||||
|
|
||||||
|
|||||||
7
config/config.yaml
Normal file
7
config/config.yaml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Application Configuration
|
||||||
|
# This file contains the default configuration for the application.
|
||||||
|
# Values can be overridden by environment variables with the prefix "APP_".
|
||||||
|
# Example: APP_LOG_LEVEL=debug
|
||||||
|
|
||||||
|
# Log Level: debug, info, warn, error
|
||||||
|
log_level: "info"
|
||||||
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
|||||||
module github.com/placeholder/golang-template
|
module github.com/placeholder/golang-template
|
||||||
|
|
||||||
go 1.23
|
go 1.23
|
||||||
|
|
||||||
|
require gopkg.in/yaml.v3 v3.0.1
|
||||||
|
|||||||
4
go.sum
Normal file
4
go.sum
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
136
internal/config/config.go
Normal file
136
internal/config/config.go
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config holds the application configuration.
|
||||||
|
type Config struct {
|
||||||
|
LogLevel string `yaml:"log_level"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load reads configuration from a YAML file and overrides it with environment variables.
|
||||||
|
// The envPrefix is used to namespace environment variables (e.g. "APP").
|
||||||
|
// Env vars should be in the format: PREFIX_FIELD_NAME (uppercase).
|
||||||
|
func Load(configPath string, envPrefix string) (*Config, error) {
|
||||||
|
cfg := &Config{}
|
||||||
|
|
||||||
|
// 1. Read YAML file
|
||||||
|
file, err := os.Open(configPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to open config file: %w", err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
decoder := yaml.NewDecoder(file)
|
||||||
|
if err := decoder.Decode(cfg); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to decode config file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Override from Environment Variables
|
||||||
|
if err := overrideFromEnv(cfg, envPrefix); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to process environment variables: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// overrideFromEnv uses reflection to traverse the struct and update fields from env vars.
|
||||||
|
func overrideFromEnv(cfg interface{}, prefix string) error {
|
||||||
|
v := reflect.ValueOf(cfg)
|
||||||
|
if v.Kind() == reflect.Ptr {
|
||||||
|
v = v.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
|
return setFromEnv(v, prefix)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setFromEnv(v reflect.Value, prefix string) error {
|
||||||
|
t := v.Type()
|
||||||
|
|
||||||
|
for i := 0; i < v.NumField(); i++ {
|
||||||
|
field := t.Field(i)
|
||||||
|
fieldVal := v.Field(i)
|
||||||
|
|
||||||
|
// Determine the key for this field
|
||||||
|
// We use the yaml tag if available to guess the name, or just the field name
|
||||||
|
keyName := field.Name
|
||||||
|
if tag := field.Tag.Get("yaml"); tag != "" {
|
||||||
|
parts := strings.Split(tag, ",")
|
||||||
|
if parts[0] != "" {
|
||||||
|
keyName = parts[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sanitize key name for Env (uppercase, replace non-alphanum with _)
|
||||||
|
envKey := strings.ToUpper(fmt.Sprintf("%s_%s", prefix, keyName))
|
||||||
|
// Clean up common separators like hyphens in yaml keys
|
||||||
|
envKey = strings.ReplaceAll(envKey, "-", "_")
|
||||||
|
|
||||||
|
if fieldVal.Kind() == reflect.Struct {
|
||||||
|
// Recursive call for nested structs
|
||||||
|
if err := setFromEnv(fieldVal, envKey); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !fieldVal.CanSet() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
val, present := os.LookupEnv(envKey)
|
||||||
|
if present {
|
||||||
|
if err := setField(fieldVal, val); err != nil {
|
||||||
|
return fmt.Errorf("failed to set field %s from env %s: %w", field.Name, envKey, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setField(field reflect.Value, value string) error {
|
||||||
|
switch field.Kind() {
|
||||||
|
case reflect.String:
|
||||||
|
field.SetString(value)
|
||||||
|
case reflect.Bool:
|
||||||
|
b, err := strconv.ParseBool(value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
field.SetBool(b)
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
// Handle Duration specially if it's an int64 underlying type?
|
||||||
|
// For now, let's assume standard ints.
|
||||||
|
// Real generic implementations often handle time.Duration
|
||||||
|
if field.Type() == reflect.TypeOf(time.Duration(0)) {
|
||||||
|
d, err := time.ParseDuration(value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
field.SetInt(int64(d))
|
||||||
|
} else {
|
||||||
|
i, err := strconv.ParseInt(value, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
field.SetInt(i)
|
||||||
|
}
|
||||||
|
case reflect.Float32, reflect.Float64:
|
||||||
|
f, err := strconv.ParseFloat(value, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
field.SetFloat(f)
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported type: %s", field.Kind())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
43
internal/config/config_test.go
Normal file
43
internal/config/config_test.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLoad(t *testing.T) {
|
||||||
|
// Create a temporary config file
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
configFile := filepath.Join(tmpDir, "config.yaml")
|
||||||
|
|
||||||
|
yamlContent := []byte(`
|
||||||
|
log_level: "info"
|
||||||
|
`)
|
||||||
|
if err := os.WriteFile(configFile, yamlContent, 0644); err != nil {
|
||||||
|
t.Fatalf("failed to write temp config file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("Load from YAML", func(t *testing.T) {
|
||||||
|
cfg, err := Load(configFile, "TEST")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Load() error = %v", err)
|
||||||
|
}
|
||||||
|
if cfg.LogLevel != "info" {
|
||||||
|
t.Errorf("expected LogLevel 'info', got '%s'", cfg.LogLevel)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Override from Env", func(t *testing.T) {
|
||||||
|
os.Setenv("TEST_LOG_LEVEL", "debug")
|
||||||
|
defer os.Unsetenv("TEST_LOG_LEVEL")
|
||||||
|
|
||||||
|
cfg, err := Load(configFile, "TEST")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Load() error = %v", err)
|
||||||
|
}
|
||||||
|
if cfg.LogLevel != "debug" {
|
||||||
|
t.Errorf("expected LogLevel 'debug', got '%s'", cfg.LogLevel)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user