minor polish

This commit is contained in:
djmil 2026-04-01 19:18:23 +00:00
parent 3403a8e168
commit 1ebdf085e5
2 changed files with 15 additions and 7 deletions

View File

@ -6,6 +6,7 @@ package main
import (
"fmt"
"os"
"path/filepath"
"gitea.djmil.dev/djmil/go-template/internal/config"
"gitea.djmil.dev/djmil/go-template/internal/greeter"
@ -38,7 +39,7 @@ func run() (err error) {
}
log.WithFields(map[string]any{
"app": cfg.App.Name,
"app": filepath.Base(os.Args[0]),
"env": cfg.App.Env,
}).Info("starting up")
@ -46,7 +47,7 @@ func run() (err error) {
var greetSvc greeter.Greeter = greeter.New(log)
// ── Example usage ─────────────────────────────────────────────────────────
msg := greetSvc.Greet("").Expect("greeting")
msg := greetSvc.Greet(cfg.Greeter.Name).Expect("greeting")
log.WithField("message", msg).Info("greeting complete")

View File

@ -15,13 +15,13 @@ import (
// Config is the root configuration object. Add sub-structs as the app grows.
type Config struct {
App AppConfig
Logger LoggerConfig
App AppConfig
Logger LoggerConfig
Greeter GreeterConfig
}
// AppConfig holds generic application settings.
type AppConfig struct {
Name string
Port int
Env string // dev | staging | prod
}
@ -31,10 +31,15 @@ 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", "go-template", "application name")
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")
@ -43,12 +48,14 @@ func Load() *Config {
return &Config{
App: AppConfig{
Name: *name,
Port: *port,
Env: *env,
},
Logger: LoggerConfig{
Level: *level,
},
Greeter: GreeterConfig{
Name: *name,
},
}
}