fix(config): Fix environment variable mapping for Docker
- Add SetEnvKeyReplacer to convert underscores to dots - Explicitly bind DATABASE_DSN, REGISTRY_CONSUL_ADDRESS, REGISTRY_TYPE - Fixes database connection issues in Docker where services couldn't read DATABASE_DSN environment variable - Services in Docker can now connect to postgres:5432 instead of localhost
This commit is contained in:
@@ -3,6 +3,7 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.dcentral.systems/toolz/goplt/pkg/config"
|
"git.dcentral.systems/toolz/goplt/pkg/config"
|
||||||
@@ -95,9 +96,13 @@ func LoadConfig(env string) (config.ConfigProvider, error) {
|
|||||||
|
|
||||||
// Enable environment variable support
|
// Enable environment variable support
|
||||||
v.AutomaticEnv()
|
v.AutomaticEnv()
|
||||||
// Environment variables can be set in UPPER_SNAKE_CASE format
|
// Set env key replacer to convert UPPER_SNAKE_CASE to nested keys
|
||||||
// and will automatically map to nested keys (e.g., SERVER_PORT -> server.port)
|
// e.g., DATABASE_DSN -> database.dsn, SERVER_PORT -> server.port
|
||||||
// Viper handles this automatically with AutomaticEnv()
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||||
|
// Bind specific environment variables to config keys
|
||||||
|
v.BindEnv("database.dsn", "DATABASE_DSN")
|
||||||
|
v.BindEnv("registry.consul.address", "REGISTRY_CONSUL_ADDRESS")
|
||||||
|
v.BindEnv("registry.type", "REGISTRY_TYPE")
|
||||||
|
|
||||||
return NewViperConfig(v), nil
|
return NewViperConfig(v), nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user