From ad1f19739d3c5a79049ae42e3f1144e0c6ea714d Mon Sep 17 00:00:00 2001 From: jordan Date: Mon, 23 Feb 2026 04:25:38 -0700 Subject: [PATCH] fix: call config.MustInit() before config.Load() so Viper reads env vars Without MustInit(), viper.AutomaticEnv() is never called, so Viper cannot read environment variables injected by K8s secrets (envFrom). This caused DATABASE_URL to always appear empty in deployed services, forcing them into standalone/in-memory mode even when a database was provisioned. os.Getenv() calls like JWT_SECRET worked fine (direct syscall). Viper-backed reads like DATABASE_URL did not (require AutomaticEnv). Added pkgconfig.MustInit() call at the top of main() in both the service component template and the full-monorepo example-api. Co-Authored-By: Claude Sonnet 4.6 --- .../templates/components/service/cmd/server/main.go.tmpl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/adapter/templates/templates/components/service/cmd/server/main.go.tmpl b/internal/adapter/templates/templates/components/service/cmd/server/main.go.tmpl index 8195c85..1ef61c9 100644 --- a/internal/adapter/templates/templates/components/service/cmd/server/main.go.tmpl +++ b/internal/adapter/templates/templates/components/service/cmd/server/main.go.tmpl @@ -24,6 +24,7 @@ import ( emailpkg "{{GO_MODULE}}/pkg/email" "{{GO_MODULE}}/pkg/notify" "{{GO_MODULE}}/pkg/queue" + pkgconfig "{{GO_MODULE}}/pkg/config" "{{GO_MODULE}}/pkg/realtime" "{{GO_MODULE}}/pkg/storage" "{{GO_MODULE}}/pkg/textgen" @@ -58,6 +59,13 @@ func main() { os.Exit(0) } + // Initialize viper with AutomaticEnv so Viper reads env vars injected by + // the platform (K8s secrets, envFrom). Must happen before any config reads. + pkgconfig.MustInit(pkgconfig.Options{ + AppName: "{{COMPONENT_NAME}}", + DefaultPort: {{PORT}}, + }) + // Load config cfg := config.Load()