commit e706f7d907ac4e80aeb3e83039cd066654d17f9d Author: matst80 Date: Sat Jun 27 21:48:09 2026 +0200 mirgation docs and inital migration diff --git a/config/env.go b/config/env.go new file mode 100644 index 0000000..cb4b4fd --- /dev/null +++ b/config/env.go @@ -0,0 +1,78 @@ +// Package config loads platform configuration from the environment. +// +// The Env* helpers all follow the same contract: an unset (or empty) variable +// returns def silently; a variable that is set but unparseable — or rejected by +// the optional valid predicate — logs once and falls back to def. This keeps the +// "parse env, validate, log, default" boilerplate out of every cmd/* main. +package config + +import ( + "log" + "os" + "strconv" + "time" +) + +// EnvString returns the value of key, or def when unset/empty. +func EnvString(key, def string) string { + if v, ok := os.LookupEnv(key); ok && v != "" { + return v + } + return def +} + +// EnvInt parses key as an int, validating with valid (nil = accept any). +func EnvInt(key string, def int, valid func(int) bool) int { + v, ok := os.LookupEnv(key) + if !ok || v == "" { + return def + } + n, err := strconv.Atoi(v) + if err != nil || (valid != nil && !valid(n)) { + log.Printf("invalid %s %q, using %d", key, v, def) + return def + } + return n +} + +// EnvFloat64 parses key as a float64, validating with valid (nil = accept any). +func EnvFloat64(key string, def float64, valid func(float64) bool) float64 { + v, ok := os.LookupEnv(key) + if !ok || v == "" { + return def + } + f, err := strconv.ParseFloat(v, 64) + if err != nil || (valid != nil && !valid(f)) { + log.Printf("invalid %s %q, using %g", key, v, def) + return def + } + return f +} + +// EnvFloat32 parses key as a float32, validating with valid (nil = accept any). +func EnvFloat32(key string, def float32, valid func(float32) bool) float32 { + v, ok := os.LookupEnv(key) + if !ok || v == "" { + return def + } + f, err := strconv.ParseFloat(v, 32) + if err != nil || (valid != nil && !valid(float32(f))) { + log.Printf("invalid %s %q, using %g", key, v, def) + return def + } + return float32(f) +} + +// EnvDuration parses key as a time.Duration (e.g. "2m", "30s"). +func EnvDuration(key string, def time.Duration) time.Duration { + v, ok := os.LookupEnv(key) + if !ok || v == "" { + return def + } + d, err := time.ParseDuration(v) + if err != nil || d <= 0 { + log.Printf("invalid %s %q, using %s", key, v, def) + return def + } + return d +} diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..7a8e10b --- /dev/null +++ b/doc.go @@ -0,0 +1,13 @@ +// Package platform is the shared seam library for the e-commerce platform. +// +// It holds the small, stable interfaces and helpers that every module may +// import WITHOUT importing another module's internals: env/config loading, +// the AMQP and HTTP client factories, OpenTelemetry setup, graceful shutdown, +// and the cross-module event vocabulary. +// +// Dependency rule: packages under platform/ import only third-party libraries +// and other platform/ packages — never an application module. Application +// modules depend downward onto platform. This is what keeps it a stable seam. +// +// See docs/platform-uniforming-plan.md for the layout and the migration order. +package platform diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4c9529a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.k6n.net/mats/platform + +go 1.26.2