Fx Config Module
Fx module for config.
Installation
go get github.com/ankorstore/yokai/fxconfig
Documentation
Loading
To load the module in your Fx application:
package main
import (
"fmt"
"github.com/ankorstore/yokai/config"
"github.com/ankorstore/yokai/fxconfig"
"go.uber.org/fx"
)
func main() {
fx.New(
fxconfig.FxConfigModule, // load the module
fx.Invoke(func(cfg *config.Config) { // invoke the config
fmt.Printf("app name: %s", cfg.AppName())
}),
).Run()
}
Configuration files
The module expects configuration files to be present:
- in
.
(project root)
- or in the
./configs
directory
- or any directory referenced in the
APP_CONFIG_PATH
env var
Check the configuration files documentation for more details.
Configuration usage
This module offers several features, such as:
- config helpers and typed accessors
- config dynamic environment overrides
- config env vars placeholders and runtime substitution
Check the configuration usage documentation for more details.
Override
By default, the config.Config
is created by the DefaultConfigFactory.
If needed, you can provide your own factory and override the module:
package main
import (
"fmt"
"github.com/ankorstore/yokai/config"
"github.com/ankorstore/yokai/fxconfig"
"go.uber.org/fx"
)
type CustomConfigFactory struct{}
func NewCustomConfigFactory() config.ConfigFactory {
return &CustomConfigFactory{}
}
func (f *CustomConfigFactory) Create(options ...config.ConfigOption) (*config.Config, error) {
return &config.Config{...}, nil
}
func main() {
fx.New(
fxconfig.FxConfigModule, // load the module
fx.Decorate(NewCustomConfigFactory), // decorate the module with a custom factory
fx.Invoke(func(cfg *config.Config) { // invoke the custom config
fmt.Printf("custom app name: %s", cfg.AppName())
}),
).Run()
}