fig

package
v0.0.0-...-fad2219 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2019 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package fig implements the Golang client for fig

The typical use of fig to fetch configuration is something like this;

import "github.com/rameshvk/fig/pkg/fig"
...
var cfg := fig.Config(url, key, secret, time.Second)
...
val, err := cfg.Get("my.entry", map[string]string{"user": 42})

The arg provided to Get is typically a map or a struct whose fields can be accessed by the setting definition to provide the specific cofiguration value. The setting is a simple expression which is evaluated for the matching arguments.

Package fig implements the Golang client for fig

Example
package main

import (
	"net/http"
	"net/http/httptest"
	"time"

	"github.com/alicebob/miniredis"
	"github.com/rameshvk/fig/pkg/fig"
	"github.com/rameshvk/fig/pkg/server"
)

func main() {
	store, url, key, secret, cleanup := getStoreAndInfo()
	defer cleanup()

	cfg := fig.Config(url, key, secret, time.Second)

	store.Set("my.setting", `if(it.user == "boo", "hoo", "woo")`)

	// now get the setting and provide user = boo as arg
	v, err := cfg.Get("my.setting", map[interface{}]interface{}{"user": "boo"})

	if v != "hoo" || err != nil {
		panic("unexpected result")
	}

}

func getStoreAndInfo() (server.Store, string, string, string, func()) {
	s, err := miniredis.Run()
	if err != nil {
		panic(err)
	}

	store := server.NewRedisStore(s.Addr(), "test-cfg")
	authStore := server.NewRedisStore(s.Addr(), "auth-store")
	unauthorized := func(r *http.Request) server.Store {
		return nil
	}
	authorized := func(r *http.Request) server.Store {
		return store
	}

	ts := httptest.NewServer(server.Handler(server.BasicAuth(authStore, authorized, unauthorized)))
	server.SetBasicAuthInfo(authStore, "mykey", "mysecret")
	cleanup := func() {
		ts.Close()
		s.Close()
	}

	return store, ts.URL, "mykey", "mysecret", cleanup
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrConfigNotFound = errors.New("config not found")

ErrConfigNotFound is returned by GetConfig if config is not found

Functions

This section is empty.

Types

type Client

type Client struct {
	*http.Client
	URL         string
	AddAuthInfo func(r *http.Request) *http.Request
}

Client implements the raw fig client API.

This is typically not used by services. For fetching configuration, the Config() function is a lot simpler.

func New

func New(url string) *Client

New creates a new client based on the provided URL prefix.

This implemention is not cached. Use Config() for a cached configuration fetcher

func (*Client) GetSince

func (c *Client) GetSince(version int) (int, map[string]string)

func (*Client) History

func (c *Client) History(key, epoch string) (string, []string)

func (*Client) Set

func (c *Client) Set(key, val string)

func (*Client) WithKey

func (c *Client) WithKey(key, secret string) *Client

WithKey sets up the client to make calls with the provided API key

type Getter

type Getter interface {
	Get(key string, arg interface{}) (interface{}, error)
}

Getter allows fetching configuration entries

func Config

func Config(url, key, secret string, cacheFor time.Duration) Getter

Config creates a new config getter which can be used to evaluate the configuration.

func ConfigWithClient

func ConfigWithClient(c *Client, cacheFor time.Duration) Getter

ConfigWithClient returns a getter than can be used to efficiently access configuration entries.

Jump to

Keyboard shortcuts

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