template/internal/config/config.go
2026-04-07 20:32:27 +00:00

62 lines
1.3 KiB
Go

// Package config parses application configuration from command-line flags.
// Defaults are defined here; override at runtime with flags:
//
// ./app -port 9090 -env prod -log-level warn
//
// Usage:
//
// cfg := config.Load()
// fmt.Println(cfg.App.Port)
package config
import (
"flag"
)
// Config is the root configuration object. Add sub-structs as the app grows.
type Config struct {
App AppConfig
Logger LoggerConfig
Greeter GreeterConfig
}
// AppConfig holds generic application settings.
type AppConfig struct {
Port int
Env string // dev | staging | prod
}
// LoggerConfig controls logging behavior.
type LoggerConfig struct {
Level string // debug | info | warn | error
}
// Greeter config for internal/greeter/Service.
type GreeterConfig struct {
Name string
}
// Load parses command-line flags and returns a Config.
// Call this once at startup before any other flag parsing.
func Load() *Config {
name := flag.String("name", "Gopher", "application name")
port := flag.Int("port", 8080, "listen port")
env := flag.String("env", "dev", "environment: dev | staging | prod")
level := flag.String("log-level", "info", "log level: debug | info | warn | error")
flag.Parse()
return &Config{
App: AppConfig{
Port: *port,
Env: *env,
},
Logger: LoggerConfig{
Level: *level,
},
Greeter: GreeterConfig{
Name: *name,
},
}
}