discovery

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package discovery provides a common interface for all providers and Match to transform source to destination URL. Run func starts event loop checking all providers and retrieving lists of rules. All lists combined into a merged one.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains added in v0.3.0

func Contains(e string, s []string) bool

Contains checks if the input string (e) in the given slice

func ParseOnlyFrom added in v1.1.0

func ParseOnlyFrom(s string) (res []string)

ParseOnlyFrom parses comma separated list of IPs

Types

type MatchType added in v0.3.0

type MatchType int

MatchType defines the type of mapper (rule)

const (
	MTProxy MatchType = iota
	MTStatic
)

enum of all match types

func (MatchType) String added in v0.3.0

func (m MatchType) String() string

type MatchedRoute added in v0.6.0

type MatchedRoute struct {
	Destination string
	Alive       bool
	Mapper      URLMapper
}

MatchedRoute contains a single match used to produce multi-matched Matches

type Matches added in v0.6.0

type Matches struct {
	MatchType MatchType
	Routes    []MatchedRoute
}

Matches returns result of url mapping. May have multiple routes. Lack of any routes means no match was wound

type Provider

type Provider interface {
	Events(ctx context.Context) (res <-chan ProviderID)
	List() (res []URLMapper, err error)
}

Provider defines sources of mappers

type ProviderID

type ProviderID string

ProviderID holds provider identifier to emulate enum of them

const (
	PIDocker        ProviderID = "docker"
	PIStatic        ProviderID = "static"
	PIFile          ProviderID = "file"
	PIConsulCatalog ProviderID = "consul-catalog"
)

enum of all provider ids

type ProviderMock

type ProviderMock struct {
	// EventsFunc mocks the Events method.
	EventsFunc func(ctx context.Context) <-chan ProviderID

	// ListFunc mocks the List method.
	ListFunc func() ([]URLMapper, error)
	// contains filtered or unexported fields
}

ProviderMock is a mock implementation of Provider.

func TestSomethingThatUsesProvider(t *testing.T) {

	// make and configure a mocked Provider
	mockedProvider := &ProviderMock{
		EventsFunc: func(ctx context.Context) <-chan ProviderID {
			panic("mock out the Events method")
		},
		ListFunc: func() ([]URLMapper, error) {
			panic("mock out the List method")
		},
	}

	// use mockedProvider in code that requires Provider
	// and then make assertions.

}

func (*ProviderMock) Events

func (mock *ProviderMock) Events(ctx context.Context) <-chan ProviderID

Events calls EventsFunc.

func (*ProviderMock) EventsCalls

func (mock *ProviderMock) EventsCalls() []struct {
	Ctx context.Context
}

EventsCalls gets all the calls that were made to Events. Check the length with:

len(mockedProvider.EventsCalls())

func (*ProviderMock) List

func (mock *ProviderMock) List() ([]URLMapper, error)

List calls ListFunc.

func (*ProviderMock) ListCalls

func (mock *ProviderMock) ListCalls() []struct {
}

ListCalls gets all the calls that were made to List. Check the length with:

len(mockedProvider.ListCalls())

type RedirectType added in v0.8.0

type RedirectType int

RedirectType defines types of redirects

const (
	RTNone RedirectType = 0
	RTPerm RedirectType = 301
	RTTemp RedirectType = 302
)

enum of all redirect types

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service implements discovery with multiple providers and url matcher

func NewService

func NewService(providers []Provider, interval time.Duration) *Service

NewService makes service with given providers

func (*Service) CheckHealth added in v0.6.0

func (s *Service) CheckHealth() (pingResult map[string]error)

CheckHealth starts health-check for service's mappers

func (*Service) Mappers

func (s *Service) Mappers() (mappers []URLMapper)

Mappers return list of all mappers

func (*Service) Match

func (s *Service) Match(srv, src string) (res Matches)

Match url to all mappers. Returns Matches with potentially multiple destinations for MTProxy. For MTStatic always a single match because fail-over doesn't supported for assets

func (*Service) Run

func (s *Service) Run(ctx context.Context) error

Run runs blocking loop getting events from all providers and updating all mappers on each event

func (*Service) ScheduleHealthCheck added in v0.6.0

func (s *Service) ScheduleHealthCheck(ctx context.Context, interval time.Duration)

ScheduleHealthCheck starts background loop with health-check

func (*Service) Servers

func (s *Service) Servers() (servers []string)

Servers return list of all servers, skips "*" (catch-all/default)

type URLMapper

type URLMapper struct {
	Server       string
	SrcMatch     regexp.Regexp
	Dst          string
	ProviderID   ProviderID
	PingURL      string
	MatchType    MatchType
	RedirectType RedirectType
	KeepHost     *bool
	OnlyFromIPs  []string

	AssetsLocation string // local FS root location
	AssetsWebRoot  string // web root location
	AssetsSPA      bool   // spa mode, redirect to webroot/index.html on not found
	// contains filtered or unexported fields
}

URLMapper contains all info about source and destination routes

func (URLMapper) IsAlive added in v0.6.0

func (m URLMapper) IsAlive() bool

IsAlive indicates whether mapper destination is alive

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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