Documentation
¶
Overview ¶
Package confx provides configs loaded by github.com/uchaloop/confmaker to an Uber Fx application.
fx.New(
confx.Module(),
confx.Provide[postgres.Config](),
confx.ProvideNamed[postgres.Config]("replica", confmaker.WithPrefix("REPLICA_POSTGRES_")),
)
Module creates one confmaker.Loader per application and is required. Provide registers a config and provides it untagged; ProvideNamed provides it under the Fx tag name:"<name>". Every provided config is loaded when the application starts, whether or not anything consumes it, and the start fails with one error listing every problem.
Declaring configs, the options and the loading rules are documented in package confmaker.
Example ¶
Provide makes the config available to constructors. WithEnv stands in for the process environment here; an application calls confx.Module() without it.
package main
import (
"fmt"
"time"
"github.com/uchaloop/confmaker"
"github.com/uchaloop/confx"
"go.uber.org/fx"
)
// StoreConfig is the kind of config a library declares: plain fields, env tags,
// and no knowledge of where the values come from.
type StoreConfig struct {
Host string `env:"HOST,notEmpty"`
Timeout time.Duration `env:"TIMEOUT"`
}
func (StoreConfig) ConfigName() string { return "store" }
func (c *StoreConfig) SetDefaults() { c.Timeout = 30 * time.Second }
func main() {
env := confmaker.WithEnv(map[string]string{"STORE_HOST": "db:5432"})
app := fx.New(
fx.NopLogger,
confx.Module(env),
confx.Provide[StoreConfig](),
fx.Invoke(func(cfg StoreConfig) {
fmt.Println(cfg.Host, cfg.Timeout)
}),
)
if err := app.Err(); err != nil {
fmt.Println(err)
}
}
Output: db:5432 30s
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Module ¶
Module loads the configs registered by Provide and ProvideNamed through one confmaker.Loader per application, configured by opts. Loading happens once, when the application starts or when a constructor first needs a config, and its error lists the problems of every config, whatever order the options are given in.
An application that calls Provide or ProvideNamed must include Module exactly once.
func Provide ¶
func Provide[T any](opts ...confmaker.ConfigOption) fx.Option
Provide registers a config of type T with Module and provides it without an Fx tag. The instance name comes from confmaker.WithName or T's ConfigName; WithName adds no Fx tag. The config is loaded at start whether or not anything consumes it.
func ProvideNamed ¶
ProvideNamed registers a config of type T with Module and provides it with the Fx tag name:"<name>". name is also the instance name, and gives the prefix unless confmaker.WithPrefix changes it. ConfigName is not used, and a further WithName is reported as an error.
Example ¶
ProvideNamed provides a second instance of the same type under an Fx tag, with its own prefix.
package main
import (
"fmt"
"time"
"github.com/uchaloop/confmaker"
"github.com/uchaloop/confx"
"go.uber.org/fx"
)
// StoreConfig is the kind of config a library declares: plain fields, env tags,
// and no knowledge of where the values come from.
type StoreConfig struct {
Host string `env:"HOST,notEmpty"`
Timeout time.Duration `env:"TIMEOUT"`
}
func (StoreConfig) ConfigName() string { return "store" }
func (c *StoreConfig) SetDefaults() { c.Timeout = 30 * time.Second }
func main() {
env := confmaker.WithEnv(map[string]string{
"STORE_HOST": "db:5432",
"REPLICA_STORE_HOST": "replica:5432",
})
type params struct {
fx.In
Primary StoreConfig
Replica StoreConfig `name:"replica"`
}
app := fx.New(
fx.NopLogger,
confx.Module(env),
confx.Provide[StoreConfig](),
confx.ProvideNamed[StoreConfig]("replica", confmaker.WithPrefix("REPLICA_STORE_")),
fx.Invoke(func(p params) {
fmt.Println(p.Primary.Host, p.Replica.Host)
}),
)
if err := app.Err(); err != nil {
fmt.Println(err)
}
}
Output: db:5432 replica:5432
Types ¶
This section is empty.