config

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2022 License: Apache-2.0 Imports: 6 Imported by: 3

Documentation

Overview

Package config provides a single way to manage the configuration of your application. The configuration can be a yaml file and/or a list of environment variable. To set the config using the environment, this package is using the package github.com/nexucis/lamenv, which is able to determinate what is the environment variable that matched the different attribute tof the struct. By default it is based on the yaml tag provided.

The main entry point of this package is the struct Resolver. This struct will allow you to set the path to your config file if you have one and to give the prefix of all of your environment variable. Note:

  1. A good practice is to prefix your environment variable by the name of your application.
  2. The config file is not mandatory, you can manage all you configuration using the environment variable.
  3. The config by environment is always overriding the config by file.

The Resolver at the end returns an object that implements the interface Validator. Each config/struct can implement this interface in order to provide a single way to verify the configuration and to set the default value. The object returned by the Resolver will loop other different structs that are parts of the config and execute the method Verify if implemented.

Example:

  import (
          "fmt"

          "github.com/perses/common/config"
  )

   type Config struct {
	    Etcd *EtcdConfig `yaml:"etcd"`
   }

   func (c *Config) Verify() error {
     if c.EtcdConfig == nil {
       return fmt.Errorf("etcd config cannot be empty")
     }
   }

   func Resolve(configFile string) (Config, error) {
	    c := Config{}
	    return c, config.NewResolver().
		  SetConfigFile(configFile).
		  SetEnvPrefix("PERSES").
		  Resolve(&c).
		  Verify()
   }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	Host string `yaml:"host"`
	Port uint64 `yaml:"port,omitempty"`
}

Connection is a configuration of an etcd host and port

func (*Connection) Verify

func (c *Connection) Verify() error

type EtcdConfig

type EtcdConfig struct {
	Connections           []Connection `yaml:"connections"`
	Protocol              EtcdProtocol `yaml:"protocol,omitempty"`
	User                  string       `yaml:"user,omitempty"`
	Password              string       `yaml:"password,omitempty"`
	RequestTimeoutSeconds uint64       `yaml:"request_timeout"`
}

EtcdConfig defines the way to configure the connection to the etcd database

func (*EtcdConfig) BuildEndpoints

func (c *EtcdConfig) BuildEndpoints() []string

BuildEndpoints returns a slice of URLs that can be used in the method clientv3.New

func (*EtcdConfig) Verify

func (c *EtcdConfig) Verify() error

type EtcdProtocol

type EtcdProtocol string
const (
	EtcdAsHTTPProtocol  EtcdProtocol = "http"
	EtcdAsHTTPSProtocol EtcdProtocol = "https"
)

func (*EtcdProtocol) Verify

func (c *EtcdProtocol) Verify() error

type Resolver

type Resolver interface {
	SetEnvPrefix(prefix string) Resolver
	SetConfigFile(filename string) Resolver
	Resolve(config interface{}) Validator
}

func NewResolver

func NewResolver() Resolver

type Validator

type Validator interface {
	Verify() error
}

Jump to

Keyboard shortcuts

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