Skip to content

Commit 3089e74

Browse files
committed
feat(config): add environment variable source configuration options
- Introduced new fields in AppConfig and AutoDiscoveryConfig to support loading configuration from environment variables. - Added functions to set environment variable options such as prefix, separator, and override behavior. - Updated NewApp and related functions to utilize the new environment variable settings for enhanced configuration flexibility.
1 parent 1e44402 commit 3089e74

6 files changed

Lines changed: 233 additions & 25 deletions

File tree

app.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ type AppConfig struct {
8383
ConfigBaseNames []string // Base config file names (default: ["config.yaml", "config.yml"])
8484
ConfigLocalNames []string // Local config file names (default: ["config.local.yaml", "config.local.yml"])
8585
EnableAppScopedConfig bool // Enable app-scoped config extraction for monorepos (default: true)
86+
87+
// Environment Variable Config Sources
88+
// These options control how environment variables are loaded as config sources
89+
EnableEnvConfig bool // Enable loading config from environment variables (default: true)
90+
EnvPrefix string // Prefix for environment variables (default: app name uppercase, e.g., "MYAPP_")
91+
EnvSeparator string // Separator for nested keys in env vars (default: "_")
92+
EnvOverridesFile bool // Whether env vars override file config values (default: true)
8693
}
8794

8895
// DefaultAppConfig returns a default application configuration.
@@ -113,6 +120,10 @@ func DefaultAppConfig() AppConfig {
113120
EnableAppScopedConfig: true,
114121
ConfigBaseNames: []string{"config.yaml", "config.yml"},
115122
ConfigLocalNames: []string{"config.local.yaml", "config.local.yml"},
123+
// Environment variable source defaults
124+
EnableEnvConfig: true, // Enabled by default
125+
EnvSeparator: "_", // Standard separator
126+
EnvOverridesFile: true, // Env takes precedence over files by default
116127
}
117128
}
118129

@@ -273,6 +284,37 @@ func WithEnableAppScopedConfig(enabled bool) AppOption {
273284
}
274285
}
275286

287+
// WithEnableEnvConfig enables or disables environment variable config source.
288+
func WithEnableEnvConfig(enabled bool) AppOption {
289+
return func(c *AppConfig) {
290+
c.EnableEnvConfig = enabled
291+
}
292+
}
293+
294+
// WithEnvPrefix sets the prefix for environment variables.
295+
// If not set, defaults to the app name in uppercase with trailing underscore.
296+
func WithEnvPrefix(prefix string) AppOption {
297+
return func(c *AppConfig) {
298+
c.EnvPrefix = prefix
299+
}
300+
}
301+
302+
// WithEnvSeparator sets the separator for nested keys in environment variables.
303+
// Default is "_".
304+
func WithEnvSeparator(separator string) AppOption {
305+
return func(c *AppConfig) {
306+
c.EnvSeparator = separator
307+
}
308+
}
309+
310+
// WithEnvOverridesFile controls whether environment variables override file config values.
311+
// Default is true (env vars take precedence over file config).
312+
func WithEnvOverridesFile(override bool) AppOption {
313+
return func(c *AppConfig) {
314+
c.EnvOverridesFile = override
315+
}
316+
}
317+
276318
// NewApp creates a new Forge application.
277319
func NewApp(config AppConfig) App {
278320
return newApp(config)

app_impl.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ func newApp(config AppConfig) *app {
116116
RequireLocal: false,
117117
MaxDepth: 5,
118118
Logger: logger,
119+
// Environment variable source configuration
120+
EnableEnvSource: config.EnableEnvConfig,
121+
EnvPrefix: config.EnvPrefix,
122+
EnvSeparator: config.EnvSeparator,
123+
EnvOverridesFile: config.EnvOverridesFile,
119124
}
120125

121126
// Try to auto-discover and load configs

config.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/xraph/forge/internal/config"
99
configcore "github.com/xraph/forge/internal/config/core"
10+
"github.com/xraph/forge/internal/config/sources"
1011
)
1112

1213
// =============================================================================
@@ -85,6 +86,18 @@ type (
8586
ManagerConfig = config.ManagerConfig
8687
)
8788

89+
// Environment Variable Source Types.
90+
type (
91+
EnvSourceOptions = sources.EnvSourceOptions
92+
EnvSourceConfig = sources.EnvSourceConfig
93+
)
94+
95+
// Auto-Discovery Types.
96+
type (
97+
AutoDiscoveryConfig = config.AutoDiscoveryConfig
98+
AutoDiscoveryResult = config.AutoDiscoveryResult
99+
)
100+
88101
// Watcher.
89102
type (
90103
Watcher = config.Watcher
@@ -101,6 +114,13 @@ var (
101114
NewValidator = config.NewValidator
102115
NewWatcher = config.NewWatcher
103116
NewSecretsManager = config.NewSecretsManager
117+
118+
// Environment Variable Source Constructors
119+
NewEnvSource = sources.NewEnvSource
120+
121+
// Auto-Discovery Functions
122+
DiscoverAndLoadConfigs = config.DiscoverAndLoadConfigs
123+
DefaultAutoDiscoveryConfig = config.DefaultAutoDiscoveryConfig
104124
)
105125

106126
// =============================================================================

extensions/database/extension.go

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ type Extension struct {
2121

2222
// NewExtension creates a new database extension with variadic options.
2323
func NewExtension(opts ...ConfigOption) forge.Extension {
24-
config := DefaultConfig()
24+
// Start with empty config - defaults will be applied by LoadConfig only if YAML config not found
25+
// This prevents defaults from overriding YAML configuration
26+
config := Config{}
2527
for _, opt := range opts {
2628
opt(&config)
2729
}
@@ -47,17 +49,72 @@ func (e *Extension) Register(app forge.App) error {
4749

4850
programmaticConfig := e.config
4951

50-
finalConfig := DefaultConfig()
51-
if err := e.LoadConfig("database", &finalConfig, programmaticConfig, DefaultConfig(), programmaticConfig.RequireConfig); err != nil {
52-
if err := e.LoadConfig("extensions.database", &finalConfig, programmaticConfig, DefaultConfig(), programmaticConfig.RequireConfig); err != nil {
53-
if programmaticConfig.RequireConfig {
54-
return fmt.Errorf("database: failed to load required config: %w", err)
55-
}
52+
// Determine if we have actual programmatic databases (not just defaults)
53+
hasProgrammaticDatabases := len(programmaticConfig.Databases) > 0 &&
54+
!(len(programmaticConfig.Databases) == 1 && programmaticConfig.Databases[0].Type == TypeSQLite)
55+
56+
// Use direct ConfigManager binding since LoadConfig has issues with defaults
57+
cm := e.App().Config()
58+
finalConfig := Config{}
59+
60+
// Try "extensions.database" first (namespaced pattern)
61+
configLoaded := false
62+
if cm.IsSet("extensions.database") {
63+
if err := cm.Bind("extensions.database", &finalConfig); err == nil {
64+
e.Logger().Info("database: loaded from config file",
65+
forge.F("key", "extensions.database"),
66+
forge.F("databases", len(finalConfig.Databases)),
67+
)
68+
configLoaded = true
69+
} else {
70+
e.Logger().Warn("database: failed to bind extensions.database", forge.F("error", err))
71+
}
72+
}
73+
74+
// Try legacy "database" key if not loaded yet
75+
if !configLoaded && cm.IsSet("database") {
76+
if err := cm.Bind("database", &finalConfig); err == nil {
77+
e.Logger().Info("database: loaded from config file",
78+
forge.F("key", "database"),
79+
forge.F("databases", len(finalConfig.Databases)),
80+
)
81+
configLoaded = true
82+
} else {
83+
e.Logger().Warn("database: failed to bind database", forge.F("error", err))
84+
}
85+
}
5686

57-
e.Logger().Warn("database: using default/programmatic config", forge.F("error", err.Error()))
87+
// Handle config not found
88+
if !configLoaded {
89+
if programmaticConfig.RequireConfig {
90+
return fmt.Errorf("database: configuration is required but not found in config files. " +
91+
"Ensure 'extensions.database' or 'database' key exists in your config.yaml")
92+
}
93+
94+
// Use programmatic config if provided, otherwise defaults
95+
if hasProgrammaticDatabases {
96+
e.Logger().Info("database: using programmatic configuration")
97+
finalConfig = programmaticConfig
98+
} else {
99+
e.Logger().Info("database: using default configuration")
100+
finalConfig = DefaultConfig()
101+
}
102+
} else {
103+
// Config loaded from YAML - merge with programmatic databases if provided
104+
if hasProgrammaticDatabases {
105+
e.Logger().Info("database: merging programmatic databases")
106+
finalConfig.Databases = append(finalConfig.Databases, programmaticConfig.Databases...)
107+
if programmaticConfig.Default != "" {
108+
finalConfig.Default = programmaticConfig.Default
109+
}
58110
}
59111
}
60112

113+
e.Logger().Info("database: configuration loaded",
114+
forge.F("databases", len(finalConfig.Databases)),
115+
forge.F("default", finalConfig.Default),
116+
)
117+
61118
e.config = finalConfig
62119

63120
// Validate configuration

extensions/database/types.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,34 +74,34 @@ type Database interface {
7474

7575
// DatabaseConfig is the configuration for a database connection.
7676
type DatabaseConfig struct {
77-
Name string `json:"name" yaml:"name"`
78-
Type DatabaseType `json:"type" yaml:"type"`
79-
DSN string `json:"dsn" yaml:"dsn"`
77+
Name string `json:"name" yaml:"name" mapstructure:"name"`
78+
Type DatabaseType `json:"type" yaml:"type" mapstructure:"type"`
79+
DSN string `json:"dsn" yaml:"dsn" mapstructure:"dsn"`
8080

8181
// Connection pool settings
82-
MaxOpenConns int `default:"25" json:"max_open_conns" yaml:"max_open_conns"`
83-
MaxIdleConns int `default:"5" json:"max_idle_conns" yaml:"max_idle_conns"`
84-
ConnMaxLifetime time.Duration `default:"5m" json:"conn_max_lifetime" yaml:"conn_max_lifetime"`
85-
ConnMaxIdleTime time.Duration `default:"5m" json:"conn_max_idle_time" yaml:"conn_max_idle_time"`
82+
MaxOpenConns int `default:"25" json:"max_open_conns" yaml:"max_open_conns" mapstructure:"max_open_conns"`
83+
MaxIdleConns int `default:"5" json:"max_idle_conns" yaml:"max_idle_conns" mapstructure:"max_idle_conns"`
84+
ConnMaxLifetime time.Duration `default:"5m" json:"conn_max_lifetime" yaml:"conn_max_lifetime" mapstructure:"conn_max_lifetime"`
85+
ConnMaxIdleTime time.Duration `default:"5m" json:"conn_max_idle_time" yaml:"conn_max_idle_time" mapstructure:"conn_max_idle_time"`
8686

8787
// Retry settings
88-
MaxRetries int `default:"3" json:"max_retries" yaml:"max_retries"`
89-
RetryDelay time.Duration `default:"1s" json:"retry_delay" yaml:"retry_delay"`
88+
MaxRetries int `default:"3" json:"max_retries" yaml:"max_retries" mapstructure:"max_retries"`
89+
RetryDelay time.Duration `default:"1s" json:"retry_delay" yaml:"retry_delay" mapstructure:"retry_delay"`
9090

9191
// Timeout settings
92-
ConnectionTimeout time.Duration `default:"10s" json:"connection_timeout" yaml:"connection_timeout"`
93-
QueryTimeout time.Duration `default:"30s" json:"query_timeout" yaml:"query_timeout"`
92+
ConnectionTimeout time.Duration `default:"10s" json:"connection_timeout" yaml:"connection_timeout" mapstructure:"connection_timeout"`
93+
QueryTimeout time.Duration `default:"30s" json:"query_timeout" yaml:"query_timeout" mapstructure:"query_timeout"`
9494

9595
// Observability settings
96-
SlowQueryThreshold time.Duration `default:"100ms" json:"slow_query_threshold" yaml:"slow_query_threshold"`
97-
DisableSlowQueryLogging bool `default:"false" json:"disable_slow_query_logging" yaml:"disable_slow_query_logging"`
98-
AutoExplainThreshold time.Duration `default:"0" json:"auto_explain_threshold" yaml:"auto_explain_threshold"` // 0 = disabled
96+
SlowQueryThreshold time.Duration `default:"100ms" json:"slow_query_threshold" yaml:"slow_query_threshold" mapstructure:"slow_query_threshold"`
97+
DisableSlowQueryLogging bool `default:"false" json:"disable_slow_query_logging" yaml:"disable_slow_query_logging" mapstructure:"disable_slow_query_logging"`
98+
AutoExplainThreshold time.Duration `default:"0" json:"auto_explain_threshold" yaml:"auto_explain_threshold" mapstructure:"auto_explain_threshold"` // 0 = disabled
9999

100100
// Health check
101-
HealthCheckInterval time.Duration `default:"30s" json:"health_check_interval" yaml:"health_check_interval"`
101+
HealthCheckInterval time.Duration `default:"30s" json:"health_check_interval" yaml:"health_check_interval" mapstructure:"health_check_interval"`
102102

103103
// Additional config (database-specific)
104-
Config map[string]any `json:"config" yaml:"config"`
104+
Config map[string]any `json:"config" yaml:"config" mapstructure:"config"`
105105
}
106106

107107
// DatabaseStats provides connection pool statistics.

internal/config/autodiscovery.go

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ type AutoDiscoveryConfig struct {
5353
// Defaults to true
5454
EnableAppScoping bool
5555

56+
// Environment Variable Source Configuration
57+
// EnableEnvSource enables loading config from environment variables
58+
// Defaults to true
59+
EnableEnvSource bool
60+
61+
// EnvPrefix is the prefix for environment variables
62+
// If empty, defaults to AppName uppercase with trailing underscore
63+
EnvPrefix string
64+
65+
// EnvSeparator is the separator for nested keys in env vars
66+
// Defaults to "_"
67+
EnvSeparator string
68+
69+
// EnvOverridesFile controls whether env vars override file config values
70+
// When true, env source gets higher priority than file sources
71+
// Defaults to true
72+
EnvOverridesFile bool
73+
5674
// Logger for discovery operations
5775
Logger logger.Logger
5876

@@ -87,6 +105,10 @@ func DefaultAutoDiscoveryConfig() AutoDiscoveryConfig {
87105
RequireBase: false,
88106
RequireLocal: false,
89107
EnableAppScoping: true,
108+
// Environment variable source defaults
109+
EnableEnvSource: true,
110+
EnvSeparator: "_",
111+
EnvOverridesFile: true,
90112
}
91113
}
92114

@@ -115,6 +137,11 @@ func DiscoverAndLoadConfigs(cfg AutoDiscoveryConfig) (ConfigManager, *AutoDiscov
115137
cfg.SearchPaths = []string{cwd}
116138
}
117139

140+
// Default env separator
141+
if cfg.EnvSeparator == "" {
142+
cfg.EnvSeparator = "_"
143+
}
144+
118145
// Discover config files
119146
result, err := discoverConfigFiles(cfg)
120147
if err != nil {
@@ -127,6 +154,12 @@ func DiscoverAndLoadConfigs(cfg AutoDiscoveryConfig) (ConfigManager, *AutoDiscov
127154
ErrorHandler: cfg.ErrorHandler,
128155
})
129156

157+
// Priority scheme:
158+
// - Base config: 100
159+
// - Local config: 200
160+
// - Environment (if EnvOverridesFile=true): 300
161+
// - Environment (if EnvOverridesFile=false): 50
162+
130163
// Load base config if found
131164
if result.BaseConfigPath != "" {
132165
source, err := sources.NewFileSource(result.BaseConfigPath, sources.FileSourceOptions{
@@ -179,8 +212,59 @@ func DiscoverAndLoadConfigs(cfg AutoDiscoveryConfig) (ConfigManager, *AutoDiscov
179212
return nil, nil, errors.New("local config file required but not found")
180213
}
181214

215+
// Load environment variable source if enabled
216+
if cfg.EnableEnvSource {
217+
// Determine env prefix - default to AppName uppercase with trailing underscore
218+
envPrefix := cfg.EnvPrefix
219+
if envPrefix == "" && cfg.AppName != "" {
220+
envPrefix = strings.ToUpper(cfg.AppName) + cfg.EnvSeparator
221+
}
222+
223+
// Determine priority based on EnvOverridesFile setting
224+
envPriority := 300 // Higher than file sources (default: env overrides files)
225+
if !cfg.EnvOverridesFile {
226+
envPriority = 50 // Lower than file sources (files override env)
227+
}
228+
229+
envSource, err := sources.NewEnvSource(envPrefix, sources.EnvSourceOptions{
230+
Name: "config.env",
231+
Prefix: envPrefix,
232+
Priority: envPriority,
233+
Separator: cfg.EnvSeparator,
234+
WatchEnabled: false, // Env watching is expensive, disabled by default
235+
CaseSensitive: false,
236+
IgnoreEmpty: true,
237+
TypeConversion: true,
238+
Logger: cfg.Logger,
239+
ErrorHandler: cfg.ErrorHandler,
240+
})
241+
if err != nil {
242+
if cfg.Logger != nil {
243+
cfg.Logger.Warn("failed to create env config source",
244+
F("error", err.Error()),
245+
)
246+
}
247+
} else {
248+
if err := manager.LoadFrom(envSource); err != nil {
249+
if cfg.Logger != nil {
250+
cfg.Logger.Warn("failed to load env config source",
251+
F("error", err.Error()),
252+
)
253+
}
254+
} else {
255+
if cfg.Logger != nil {
256+
cfg.Logger.Debug("loaded environment variable config source",
257+
F("prefix", envPrefix),
258+
F("priority", envPriority),
259+
F("overrides_files", cfg.EnvOverridesFile),
260+
)
261+
}
262+
}
263+
}
264+
}
265+
182266
// Extract app-scoped config if enabled and AppName is provided
183-
// We need to do this BEFORE merging sources to maintain proper priority
267+
// We need to do this AFTER loading all sources to maintain proper priority
184268
if cfg.EnableAppScoping && cfg.AppName != "" {
185269
// Get the source data before merging
186270
if mgr, ok := manager.(*Manager); ok {

0 commit comments

Comments
 (0)