fxconfig

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 3 Imported by: 1

README

Fx Config Module

ci go report codecov Deps PkgGoDev

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()
}

Documentation

Index

Constants

View Source
const ModuleName = "config"

ModuleName is the module name.

Variables

FxConfigModule is the Fx config module.

Functions

func NewFxConfig

func NewFxConfig(p FxConfigParam) (*config.Config, error)

NewFxConfig returns a config.Config.

Types

type FxConfigParam

type FxConfigParam struct {
	fx.In
	Factory config.ConfigFactory
}

FxConfigParam allows injection of the required dependencies in NewFxConfig.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL