klhttp

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: May 13, 2020 License: MIT Imports: 8 Imported by: 0

README

HTTP Loader

Loads config from a source over HTTP

Usage

Basic usage with a json source and a poll watcher

httpLoader := klhttp.New(&klhttp.Config{
	Sources: []Source{
		{
			URL: "https://konfig.io/config.json",
			Method: "GET",
			Parser: kpjson.Parser,
		},
	},
	Watch: true,
	Rater: kwpoll.Time(10 * time.Second), // Rater is the rater for the poll watcher
})

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrNoSources is the error thrown when creating an Loader without sources
	ErrNoSources = errors.New("No sources provided")
)

Functions

This section is empty.

Types

type Client

type Client interface {
	Do(*http.Request) (*http.Response, error)
}

Client is the interface used to send the HTTP request. It is implemented by http.Client.

type Config

type Config struct {
	// Name is the name of the loader
	Name string
	// StopOnFailure tells whether a failure to load configs should closed the config and all registered closers
	StopOnFailure bool
	// Sources is a list of remote sources
	Sources []Source
	// Client is the client used to fetch the file, default is http.DefaultClient
	Client Client
	// MaxRetry is the maximum number of retries when an error occurs
	MaxRetry int
	// RetryDelay is the delay between each retry
	RetryDelay time.Duration
	// Watch sets the whether changes should be watched
	Watch bool
	// Rater is the rater to pass to the poll write
	Rater kwpoll.Rater
	// Debug sets the debug mode
	Debug bool
}

Config is the configuration of the Loader

type Loader

type Loader struct {
	*kwpoll.PollWatcher
	// contains filtered or unexported fields
}

Loader loads a configuration remotely

func New

func New(cfg *Config) *Loader

New returns a new Loader with the given Config.

func (*Loader) Load

func (r *Loader) Load(s konfig.Values) error

Load loads the config from sources and parses the response

func (*Loader) MaxRetry

func (r *Loader) MaxRetry() int

MaxRetry returns the MaxRetry config property, it implements the konfig.Loader interface

func (*Loader) Name

func (r *Loader) Name() string

Name returns the name of the loader

func (*Loader) RetryDelay

func (r *Loader) RetryDelay() time.Duration

RetryDelay returns the RetryDelay config property, it implements the konfig.Loader interface

func (*Loader) StopOnFailure added in v0.2.0

func (r *Loader) StopOnFailure() bool

StopOnFailure returns whether a load failure should stop the config and the registered closers

type Source

type Source struct {
	URL    string
	Method string
	Body   io.Reader
	Parser parser.Parser
	// Prepare is a function to modify request before sending it
	Prepare func(*http.Request)
	// StatusCode is the status code expected from this source
	// If the status code of the response is different, an error is returned.
	// Default is 200.
	StatusCode int
}

Source is an HTTP source and a Parser

func (Source) Do

func (s Source) Do(c Client) (io.Reader, error)

Do makes an http request and sends the body to the parser

Jump to

Keyboard shortcuts

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