conf

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2021 License: MIT Imports: 6 Imported by: 0

README

conf

WARNING! just for fun pet-project, not recommend for production use

conf simplify loading of application settings from different source. It may be specialized service like consul or etcd or simple json file, cli flags and environment variables.

linter tests

Table of contents

General info

While you develop, debug or testing your application, you may need to redeclare some of the settings, like database connection parameters, or some domain logic options. conf let you organize application configuration process flexible.

How does it work

Application config represented as a set of services configs provided or used by application. First you need to instantiate conf.Provider then add some sources like json-file, environment variables of cli flags. When sources added you should Load configs you need. conf.Provider will try to load services configs from each source you've added concurrently. Then you may take service config calling ServiceConfig method. You will get service config loaded from most prioritized source.

See usage example.

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

conf includes components to load data from 3 source types:

  • environment variables
  • command line flags
  • json files
How to use

instantiaite provider -> add sources -> load config data -> use config data

You may see detailed example

Dependencies

conf has only one third-party dependency

  • github.com/google/uuid

Requirements

go version >= 1.16

Setup

go get github.com/hsqds/conf

ROADMAP

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OptAutoload added in v0.2.0

func OptAutoload(enabled bool) scOption

"Opt" prefix is deprecated, will be removed at v0.3 TODO: remove "Opt" prefixed options

func OptLoadTimeout added in v0.2.0

func OptLoadTimeout(timeout time.Duration) scOption

func WithAutoload added in v0.2.1

func WithAutoload(enabled bool) scOption

WithAutoload provide ability to enable or disable autoload

func WithLoadTimeout added in v0.2.1

func WithLoadTimeout(timeout time.Duration) scOption

WithLoadTimeout provide ability to set load timeout

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 ...scOption) (Config, error)

ServiceConfig provide service config

func (*ConfigProvider) SubscribeForServiceConfig

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

TODO: 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
	Has(serviceName string) bool
}

ConfigsStorage.

type ErrServiceConfigNotFound added in v0.2.0

type ErrServiceConfigNotFound struct {
	ServiceName string
}

func (ErrServiceConfigNotFound) Error added in v0.2.0

func (e ErrServiceConfigNotFound) Error() string

Error.

type ErrSourceIsNotUnique added in v0.2.0

type ErrSourceIsNotUnique struct {
	SourceID string
}

func (ErrSourceIsNotUnique) Error added in v0.2.0

func (e ErrSourceIsNotUnique) Error() string

Error.

type ErrSourceNotFound added in v0.2.0

type ErrSourceNotFound struct {
	SourceID string
}

represents

func (ErrSourceNotFound) Error added in v0.2.0

func (e ErrSourceNotFound) Error() string

Error.

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 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 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) Has added in v0.2.0

func (c *SyncedConfigsStorage) Has(serviceName string) bool

Has checks service config exist

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