config

package
v0.0.0-...-4c2dc58 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2016 License: MIT Imports: 7 Imported by: 0

README

Golang.hr Platform configuration package

Base configuration package for Golang.hr Platform. Package is designed to be used with etcd.

NOTICE: Package under active development. No ETA atm...

Why Etcd?

etcd we will just copy out what they said without much of complications...

etcd is a distributed, consistent key-value store for shared configuration and service discovery, with a focus on being:

Simple: curl'able user facing API (HTTP+JSON)
Secure: optional SSL client cert authentication
Fast: benchmarked 1000s of writes/s per instance
Reliable: properly distributed using Raft

You can see more at etcd github

Key Functionalities
  • Configuration manager based on environment and etcd directory.
  • Saving and updating configuration key/values directly on etcd instance
  • Auto reload configuration once it's changed (watching)

NOTICE: This package wrapper is designed to manage configuration. If you wish to manage roles or anything else you will need to use custom logic. You can retrieve Etcd client by manager.Etcd()

Prerequisites

You will need to have fully functional etcd service setup and accessible by www.

Installation

First you'll have to fetch platform

go get -u github.com/golanghr/platform

than you'll have to include it in your go script

import (
	"github.com/golanghr/platform/config"
)

and than checkout examples :)

Examples

Bellow you can find some useful examples of how to use this package

Example 1 - Initializing struct
manager, err := config.New(map[string]interface{}{
	 // Useful for if you wish to have "sandbox", "production" or any other
	"env": "environment_name",

	// We use it as project name. Or so, golanghr
	"folder": "folder_name",

	// Do we want to auto sync existing configuration from the etcd or not
	"auto_sync": true,

	// The recommended sync interval is 10 seconds to 1 minute, which does
	// not bring too much overhead to server and makes client catch up the
	// cluster change in time.
	"auto_sync_interval": 10 * time.Second,

	"etcd": map[string]interface{}{
		// Etcd API version
		"version":  								  "v2",
		// list of etcd endpoints separated by comma
		"endpoints":                  []string{"127.0.0.1:2379"},
		// Transport is used by the Client to drive HTTP requests. If not
		// provided, DefaultTransport will be used.
		"transport":                  etcdc.DefaultTransport,
		// Username specifies the user credential to add as an authorization header
		"username":                   "",
		// Password is the password for the specified user to add as an authorization header
		// to the request.
		"password":                   "",
		// set timeout per request to fail fast when the target endpoint is unavailable
		"header_timeout_per_request": time.Second,
	},
})

if err != nil {
  panic(err)
}
Example 2 - Enable cURL debugging

This is useful if there are issues with setting or getting keys from Etcd

import (
		etcd "github.com/coreos/etcd/client"
)

etcd.EnablecURLDebug()
License
The MIT License (MIT)

Copyright (c) 2015 Golang Croatia

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Overview

Package config ...

Package config ...

Package config ...

Package config ...

Package config ...

Package config ...

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrorInvalidEnv -
	ErrorInvalidEnv = "Could not find (key: env) within (config: %q). Plase make sure to read package documentation."
	// ErrorInvalidFolder -
	ErrorInvalidFolder = "Could not find (key: folder) within (config: %q). Plase make sure to read package documentation."
	// ErrorInvalidEtcdConfig -
	ErrorInvalidEtcdConfig = "Could not find (key: etcd) within (config: %q). Plase make sure to read package documentation."
)

Functions

This section is empty.

Types

type Config

type Config interface {
	Get(key string) (*Value, error)
	GetOrSet(key, defaults string) (*Value, error)

	GetBool(key string) (bool, error)
	GetString(key string) (string, error)
	GetDuration(key string) (time.Duration, error)

	Set(key, value string) (*Value, error)
	SetTTL(key, value string, ttl time.Duration) (*Value, error)

	EnsureSet(key, defaults string) error
	EnsureSetMany(map[string]string) error

	Exists(key string) error

	Delete(key string) (*Value, error)
}

Config - Basic CRUD operations against Etcd configuration path

type Manager

type Manager interface {
	Config

	Etcd() etcdc.Client
	ShouldAutoSyncNodes() bool
	SyncNodes(interval time.Duration) error
	Init() error
}

Manager -

func New

func New(cnf map[string]interface{}) (Manager, error)

New - Return instance of configuration manager. Will return erorr in case of issues

type ManagerInstance

type ManagerInstance struct {
	AutoSync   bool
	Env        string
	EtcdFolder string
	Version    string

	Client etcdc.Client
	Kapi   etcdc.KeysAPI
}

ManagerInstance - Instance that manages configurations

func (*ManagerInstance) Delete

func (mi *ManagerInstance) Delete(key string) (*Value, error)

Delete - Will delete etcd key. If there are no keys, will return error

func (*ManagerInstance) EnsureSet

func (mi *ManagerInstance) EnsureSet(key, defaults string) error

EnsureSet - Will ensure that key is set. If it's not set, it will set key with provided defaults.

func (*ManagerInstance) EnsureSetMany

func (mi *ManagerInstance) EnsureSetMany(cnf map[string]string) error

EnsureSetMany - Will recursively go over map of strings and ensure each of them is set.

func (*ManagerInstance) Etcd

func (mi *ManagerInstance) Etcd() etcdc.Client

Etcd - Will return instance of CoreOS Etcd

func (*ManagerInstance) Exists

func (mi *ManagerInstance) Exists(key string) error

Exists - Will check if key exists or not

func (*ManagerInstance) Get

func (mi *ManagerInstance) Get(key string) (*Value, error)

Get - Will return etcd response about specified key. If there are no keys, will return error

func (*ManagerInstance) GetBasePrefix

func (mi *ManagerInstance) GetBasePrefix() string

GetBasePrefix - Base prefix that will be used with new client api

func (*ManagerInstance) GetBool

func (mi *ManagerInstance) GetBool(key string) (bool, error)

GetBool - Helper function that will try to fetch key from etcd and parse it as bool In case of any issues it will return error

func (*ManagerInstance) GetDuration

func (mi *ManagerInstance) GetDuration(key string) (time.Duration, error)

GetDuration - Helper function that will try to fetch key from etcd and parse it as time.Duration In case of any issues it will return error

func (*ManagerInstance) GetOrSet

func (mi *ManagerInstance) GetOrSet(key, defaults string) (*Value, error)

GetOrSet - Will attempt to retreive key from etcd and if one is not there, will attempt to create it

func (*ManagerInstance) GetString

func (mi *ManagerInstance) GetString(key string) (string, error)

GetString - Helper function that will try to fetch key from etcd and parse it as string In case of any issues it will return error

func (*ManagerInstance) Init

func (mi *ManagerInstance) Init() (err error)

Init - Will initialize important parts of the package such as etcd api

func (*ManagerInstance) Set

func (mi *ManagerInstance) Set(key, value string) (*Value, error)

Set - Will set new etcd key. If there are no keys, will return error

func (*ManagerInstance) SetTTL

func (mi *ManagerInstance) SetTTL(key, value string, ttl time.Duration) (*Value, error)

SetTTL - Will set new expiration etcd key. If there are no keys, will return error

func (*ManagerInstance) ShouldAutoSyncNodes

func (mi *ManagerInstance) ShouldAutoSyncNodes() bool

ShouldAutoSyncNodes - Basically return if we have permission to auto synchronize nodes or not. Used on package startup ...

func (*ManagerInstance) SyncNodes

func (mi *ManagerInstance) SyncNodes(interval time.Duration) (err error)

SyncNodes - Will initiate syncronization for client if configuration allows it...

type Value

type Value struct {
	*etcdc.Response
}

Value - Etcd response proxy struct designed to help out normalize how we access data

func (*Value) Key

func (v *Value) Key() string

Key - Helper for etcd Node.Key

func (*Value) Value

func (v *Value) Value() string

Value - Helper for etcd Node.Value

Jump to

Keyboard shortcuts

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