Multi-Environment Strategies
Managing configuration across environments (Dev, Staging, Prod) is one of the hardest parts of application deployment. cfgx offers flexible strategies to handle this, depending on whether you prefer build-time or runtime configuration.
Strategy 1: Runtime Configuration (Recommended for Containers)
This is the standard "12-Factor App" approach. You build a single binary (or Docker image) and configure it via environment variables when you run it.
Best for: Kubernetes, Docker, Open Source tools.
-
Generate in Getter Mode:
cfgx generate --in config.toml --out config.go --mode getter -
Build Once: Create your Docker image. The
config.tomlvalues serve as safe defaults. -
Run Anywhere: Override specific values using environment variables.
# Production docker run -e CONFIG_DATABASE_URL="postgres://prod-db..." myapp # Staging docker run -e CONFIG_DATABASE_URL="postgres://staging-db..." myapp
Strategy 2: Build-Time Injection (Recommended for CI/CD)
If you use Static Mode for performance but need to inject secrets (like API keys) that shouldn't be in your source code, you can use build-time environment overrides.
Best for: CI/CD pipelines, Serverless functions.
-
Define Placeholders: Put dummy values in your
config.tomlfor secrets.[stripe] key = "dummy_key" -
Inject during Generation: In your CI pipeline (GitHub Actions, GitLab CI), export the real secrets as environment variables before running
cfgx.# In CI pipeline export CONFIG_STRIPE_KEY=${{ secrets.STRIPE_KEY }} cfgx generate --in config.toml --out config.go --mode static -
Build: The generated Go code now contains the production secret hardcoded. The binary is self-contained and ready to deploy.
Strategy 3: Multiple Config Files
Sometimes environments differ significantly (e.g., Dev uses SQLite, Prod uses Postgres; or Dev has debug flags enabled). In this case, maintaining separate TOML files is cleaner.
Best for: Radical configuration differences between environments.
-
Create Separate Configs:
config.dev.tomlconfig.prod.toml
-
Select at Build Time:
# For local dev cfgx generate --in config.dev.toml --out config.go # For production build cfgx generate --in config.prod.toml --out config.go
Tip: Comparing Environments
Use cfgx diff to ensure you haven't missed any keys between your environments.
cfgx diff config.dev.toml config.prod.toml