config

package
v2.3.3+incompatible Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2019 License: OSL-3.0 Imports: 14 Imported by: 0

README

Config module

This module provides features to read and merge flamingo configurations. Also it provides templatefunctions to access configurations in your template.

Configurations are defined and used by the individual modules. The modules should come with a documentation which configurations/featureflags they support.

Basics

Configurations are yml files located in config folder.

The configuration syntax is to specify areas either with . or as yaml maps:

foo:
  bar: x

is the same as

foo.bar: x

Configuration can be used:

  • either by the config() templatefunction in your template
  • or via dependency injection from dingo

Loaded Configuration files

The following configuration files will be loaded from the given config folder:

  • config.yml
  • routes.yml
  • optional: config_($CONTEXT).yml
  • optional: routes_($CONTEXT).yml
  • config_local.yml
  • routes_local.yml

You can set different CONTEXT with the environment variable CONTEXT and this will cause flamingo to load additional configuration files.

e.g. starting flamingo with

export CONTEXT="dev" && go run project.go serve

Will cause flamingo to additionaly load the configfile "config/config_dev.yml"

Configuration values can also be read from environment variables with the syntax:

auth.secret: '%%ENV:KEYCLOAK_SECRET%%'
Injecting configurations

Asking for either a concrete value via e.g. foo.bar is possible, as well as getting a whole config.Map instance by a partially-selector, e.g. foo. This would be a Map with element bar.

For example:

Example struct {
  Title           string      `inject:"config:mymodul.title"
  CompleteConfig  config.Map  `inject:"config:mymodul"
  Amount          float64     `inject:"config:mymodul.amount"
  Flag            bool        `inject:"config:mymodul.flag"
}

Deeply nested config maps can be marshaled into structs for convenience.

The result struct must match exactly the structure and types of the config map and all fields must be exported.

err := m.MarshalTo(&result)

Documentation

Overview

Package config provides supporting code for multi-tenant setups

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(root *Area, basedir string) error

Load configuration in basedir

func LoadConfigFile

func LoadConfigFile(area *Area, file string) error

LoadConfigFile loads a config

Types

type Area

type Area struct {
	Name string

	Parent   *Area
	Childs   []*Area
	Modules  []dingo.Module
	Injector *dingo.Injector

	Routes        []Route
	Configuration Map
	LoadedConfig  Map
}

Area defines a configuration area for multi-site setups it is initialized by project main package and partly loaded by config files

func MergeFrom

func MergeFrom(baseContext, incomingContext Area) *Area

MergeFrom merges two Contexts into a new one We do not merge config, as we use the DI to handle it

func NewArea

func NewArea(name string, modules []dingo.Module, childs ...*Area) *Area

NewArea creates a new Area with optional childs

func (*Area) Config

func (area *Area) Config(key string) (interface{}, bool)

Config get a config value (recursive thru all parents if possible)

func (*Area) Flat

func (area *Area) Flat() map[string]*Area

Flat returns a map of name->*Area of contexts, were all values have been inherited (yet overriden) of the parent context tree.

func (*Area) GetFlatContexts

func (area *Area) GetFlatContexts() []*Area

GetFlatContexts returns a map of context-relative-name->*Area, which has been flatted to inherit all parent's tree settings such as DI & co, and filtered to only list tree nodes specified by Contexts of area.

func (*Area) GetInitializedInjector

func (area *Area) GetInitializedInjector() *dingo.Injector

GetInitializedInjector returns initialized container based on the configuration we derive our injector from our parent

func (*Area) HasConfigKey

func (area *Area) HasConfigKey(key string) bool

HasConfigKey checks recursive if the config has a given key

type DefaultConfigModule

type DefaultConfigModule interface {
	DefaultConfig() Map
}

DefaultConfigModule is used to get a module's default configuration

type Map

type Map map[string]interface{}

Map contains configuration

func (Map) Add

func (m Map) Add(cfg Map)

Add to the map (deep merge)

func (Map) Flat

func (m Map) Flat() Map

Flat map

func (Map) Get

func (m Map) Get(key string) (interface{}, bool)

Get a value by it's path

func (Map) MapInto

func (m Map) MapInto(out interface{}) error

MapInto tries to map the configuration map into a given interface

type Module

type Module struct {
	Map
}

Module defines a dingo module which automatically binds available config

func (*Module) Configure

func (m *Module) Configure(injector *dingo.Injector)

Configure the Module

type OverrideConfigModule

type OverrideConfigModule interface {
	OverrideConfig(current Map) Map
}

OverrideConfigModule allows to override config dynamically

type Route

type Route struct {
	Path       string
	Controller string
	Name       string
}

Route defines the yaml structure for a route, consisting of a path and a controller, as well as optional args

type Slice

type Slice []interface{}

Slice contains a list of configuration options

func (Slice) MapInto

func (s Slice) MapInto(out interface{}) error

MapInto tries to map the configuration map into a given interface

type TemplateFunc

type TemplateFunc struct {
	// contains filtered or unexported fields
}

TemplateFunc allows to retrieve config variables

func (*TemplateFunc) Func

func (c *TemplateFunc) Func() interface{}

Func returns the template function

func (*TemplateFunc) Inject

func (c *TemplateFunc) Inject(area *Area)

Inject dependencies

Jump to

Keyboard shortcuts

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