Extension Configuration Example
This example demonstrates how to use ConfigManager with Forge v2 extensions.
Features Demonstrated
-
Dual-key configuration pattern
- Extensions try
extensions.{name} first (preferred)
- Fall back to
{name} for v1 compatibility
-
Programmatic overrides
- Options passed to
NewExtension() override config file values
-
Required configuration
- Use
WithRequireConfig(true) to fail if config not found
Configuration Files
See config.yaml for examples of:
- Namespaced pattern:
extensions.cache, extensions.mcp
- Top-level pattern:
cache, mcp
Running the Examples
# Example 1: Load from config file
go run main.go
# Test the endpoints
curl http://localhost:8080/test
curl http://localhost:8080/_/mcp/info
Code Examples
Load from ConfigManager (default)
app := forge.NewApp(forge.AppConfig{
Extensions: []forge.Extension{
cache.NewExtension(), // Loads from config.yaml
mcp.NewExtension(), // Loads from config.yaml
},
})
Programmatic overrides
app := forge.NewApp(forge.AppConfig{
Extensions: []forge.Extension{
cache.NewExtension(
cache.WithDriver("redis"),
cache.WithURL("redis://localhost:6379"),
),
},
})
Require configuration
app := forge.NewApp(forge.AppConfig{
Extensions: []forge.Extension{
cache.NewExtension(
cache.WithRequireConfig(true), // Fails without config
),
},
})
Config Resolution Order
For the cache extension:
- Try
extensions.cache in ConfigManager
- Try
cache in ConfigManager (v1 compatibility)
- Use programmatic config passed to constructor
- Use default config
- If
RequireConfig=true, fail instead of using defaults
Programmatic options always override config file values.