conf

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2021 License: MIT Imports: 5 Imported by: 0

README

conf

!WARNING: WIP "just for fun" pet-project, not recommended for production use

main idea

application may run many services, like grpc, http or by messaging over queues. conf let the user to retrieve services configs from different sources.

conf is abstraction instantiaite provider -> add sources -> load config data -> use config data

sources

each source has priority each source may provide configs for many services

included sources
  • environment variables
  • command line flags
  • json files
json files

let's try to load configuration from json file

example config config.json

{
    "http": {
        "host": "0.0.0.0",
        "port": "8080"
    },
}

fallback config defaults.json

{
    "http": {
        "host": "",
        "port": "8000"
    },
    "grpc": {
        "host": "auth.svc",
        "port": "3000"
    }
}

first we need to create config provider

cp := conf.NewDefaultConfigProvider()
defer cp.Close(ctx)

then add some sources

cp.AddSource(
    // `config.json` has higher priority than `defaults.json`
    sources.NewJSONFileSource(100, "config.json"),
    sources.NewJSONFileSource(90, "defaults.json"),
)

then load data from sources and get needed service config

err := cp.Load(context.Background(), "http", "grpc")
processErr(err)
// `config.json` data will override `dafaults.json` respecting the priority
httpConfig, err := cp.ServiceConfig("http")
processErr(err)
// grpc config will be loaded from `defaults.json`
grpcConfig, err := cp.ServiceConfig("grpc")
processErr(err)

now we may get config parameter using config Get method

value, ok := httpConfig.Get("host", "default.svc") // "0.0.0.0", true - from `configs.json`
value, ok := grpcConfig.Get("host", "default.svc") // "auth.svc", true - from `defaults.json`
value, ok := grpcConfig.Get("inexistingKey", "default.value") // "default.value", false - defaultValue

or format config parameters calling Fmt method

formatted, err := httpConfig.Fmt("{{.host}}:{{.port}}") // "0.0.0.0:8080"
processErr(err)

ROADMAD:

  • v0.2 - optional merge service configs from various sources
  • v0.3 - ability to subscribe for source updates

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config interface {
	// Get config value by key
	Get(key, defaultValue string) string
	// Set sets key value
	Set(key, value string)
	// Fmt renders `pattern` filling it with config values
	Fmt(pattern string) (string, error)
}

Config

type ConfigProvider

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

Provider represents config provider.

func NewConfigProvider

func NewConfigProvider(sourcesStorage SourcesStorage, configsStorage ConfigsStorage,
	loader Loader) *ConfigProvider

NewConfigProvider.

func NewDefaultConfigProvider

func NewDefaultConfigProvider() *ConfigProvider

NewDefaultConfigProvider.

func (*ConfigProvider) AddSource

func (p *ConfigProvider) AddSource(src Source) error

AddSource adds source to source storage.

func (*ConfigProvider) Close

func (p *ConfigProvider) Close(ctx context.Context)

Close.

func (*ConfigProvider) Load

func (p *ConfigProvider) Load(ctx context.Context, services ...string) (loadErrors []LoadError)

Load updates inner services config cache.

func (*ConfigProvider) ServiceConfig

func (p *ConfigProvider) ServiceConfig(serviceName string, opts ...*Option) (Config, error)

ServiceConfig provide service config from inner cache.

func (*ConfigProvider) SubscribeForServiceConfig

func (p *ConfigProvider) SubscribeForServiceConfig(ctx context.Context, serviceName string,
	opts ...*Option) (chan Config, error)

SubscribeForServiceConfig creates a subscription for service config updates. Returns channel of Configs.

type ConfigsLoader

type ConfigsLoader int

Loader represents.

func (*ConfigsLoader) Load

func (cl *ConfigsLoader) Load(ctx context.Context, sources []Source, serviceNames []string) []LoadResult

Load loads configs from each source in parallel

type ConfigsStorage

type ConfigsStorage interface {
	ByServiceName(serviceName string) (Config, error)
	Set(serviceName string, cfg Config) error
}

ConfigsStorage.

type LoadError

type LoadError struct {
	SourceID string
	Service  string
	Err      error
}

LoadError represents

func (LoadError) Error

func (e LoadError) Error() string

Error

type LoadResult

type LoadResult struct {
	SourceID string
	Config   Config
	Err      error
	Service  string
	Priority int
}

LoadResult represents.

type Loader

type Loader interface {
	Load(ctx context.Context, sources []Source, serviceNames []string) []LoadResult
}

Loader.

type MapConfig

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

MapConfig represents map based config Implements Config interface.

func NewMapConfig

func NewMapConfig(d map[string]string) *MapConfig

NewMapConfig

func (*MapConfig) Fmt

func (m *MapConfig) Fmt(pattern string) (string, error)

Fmt renders `pattern` filling it with config values.

func (*MapConfig) Get

func (m *MapConfig) Get(key, defaultValue string) string

Get returns value by key or defaultValue.

func (*MapConfig) Set

func (m *MapConfig) Set(key, value string)

Set sets key value

type Option

type Option struct {
	Name  string
	Value string
}

Option represents provision options options may affect subscription and config loading processes.

type ServiceConfigStorageError

type ServiceConfigStorageError struct {
	ServiceName string
}

func (ServiceConfigStorageError) Error

Error.

type Source

type Source interface {
	// Should return unique source identifier persistent for in all source lifetime
	ID() string
	// Load pull config for the list of service
	Load(ctx context.Context, serviceNames []string) error
	// Priority returns source priority
	Priority() int
	// ServiceConfig
	ServiceConfig(serviceName string) (Config, error)
	// Close closes connections
	Close(context.Context)
}

Source.

type SourceStorageError

type SourceStorageError struct {
	SourceID string
}

represents

func (SourceStorageError) Error

func (e SourceStorageError) Error() string

Error.

type SourceUniquenessError

type SourceUniquenessError struct {
	SourceID string
}

func (SourceUniquenessError) Error

func (e SourceUniquenessError) Error() string

Error.

type SourcesStorage

type SourcesStorage interface {
	Append(src Source) error
	ByID(sourceID string) (Source, error)
	List() []Source
}

SourceStorage

type SyncedConfigsStorage

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

ConfigsStorage represents configs map protected with mutex.

func NewSyncedConfigsStorage

func NewSyncedConfigsStorage() *SyncedConfigsStorage

func (*SyncedConfigsStorage) ByServiceName

func (c *SyncedConfigsStorage) ByServiceName(serviceName string) (Config, error)

Get receives configs by service name.

func (*SyncedConfigsStorage) Set

func (c *SyncedConfigsStorage) Set(serviceName string, cfg Config) error

Set.

type SyncedSourcesStorage

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

syncedSources represents sources map protected with mutex.

func NewSyncedSourcesStorage

func NewSyncedSourcesStorage() *SyncedSourcesStorage

NewSyncedSourcesStorage

func (*SyncedSourcesStorage) Append

func (s *SyncedSourcesStorage) Append(src Source) error

Append

func (*SyncedSourcesStorage) ByID

func (s *SyncedSourcesStorage) ByID(sourceID string) (Source, error)

ByID gets source by it's ID

func (*SyncedSourcesStorage) List

func (s *SyncedSourcesStorage) List() []Source

List returns sources as a slice.

Directories

Path Synopsis
examples
env
map
test
mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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