plugin

package
v2.8.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: Apache-2.0 Imports: 7 Imported by: 8

Documentation

Overview

Package plugin provides tools for loading and registering proxy plugins

Index

Examples

Constants

View Source
const (
	// Namespace is the namespace for the extra_config section
	Namespace = "github.com/devopsfaith/krakend/proxy/plugin"
)

Variables

This section is empty.

Functions

func Load

func Load(path, pattern string, rmf RegisterModifierFunc) (int, error)

Load scans the given path using the pattern and registers all the found modifier plugins into the rmf

func LoadWithLogger

func LoadWithLogger(path, pattern string, rmf RegisterModifierFunc, logger logging.Logger) (int, error)

LoadWithLogger scans the given path using the pattern and registers all the found modifier plugins into the rmf

func LoadWithLoggerAndContext added in v2.6.0

func LoadWithLoggerAndContext(ctx context.Context, path, pattern string, rmf RegisterModifierFunc, logger logging.Logger) (int, error)
Example
var data []byte

buf := bytes.NewBuffer(data)
logger, err := logging.NewLogger("DEBUG", buf, "")
if err != nil {
	fmt.Println(err.Error())
	return
}
total, err := LoadWithLoggerAndContext(context.Background(), "./tests", ".so", RegisterModifier, logger)
if err != nil {
	fmt.Println(err.Error())
	return
}
if total != 2 {
	fmt.Printf("unexpected number of loaded plugins!. have %d, want 2\n", total)
	return
}

modFactory, ok := GetRequestModifier("lura-request-modifier-example-request")
if !ok {
	fmt.Println("modifier factory not found in the register")
	return
}

modifier := modFactory(map[string]interface{}{})

input := requestWrapper{
	ctx:    context.WithValue(context.Background(), "myCtxKey", "some"),
	path:   "/bar",
	method: "GET",
}

tmp, err := modifier(input)
if err != nil {
	fmt.Println(err.Error())
	return
}

output, ok := tmp.(RequestWrapper)
if !ok {
	fmt.Println("unexpected result type")
	return
}

if res := output.Path(); res != "/bar/fooo" {
	fmt.Printf("unexpected result path. have %s, want /bar/fooo\n", res)
	return
}

lines := strings.Split(buf.String(), "\n")
for i := range lines[:len(lines)-1] {
	fmt.Println(lines[i][21:])
}
Output:

DEBUG: [PLUGIN: lura-error-example] Logger loaded
DEBUG: [PLUGIN: lura-request-modifier-example] Logger loaded
DEBUG: [PLUGIN: lura-request-modifier-example] Context loaded
DEBUG: [PLUGIN: lura-request-modifier-example] Request modifier injected
DEBUG: context key: some
DEBUG: params: map[]
DEBUG: headers: map[]
DEBUG: method: GET
DEBUG: url: <nil>
DEBUG: query: map[]
DEBUG: path: /bar/fooo

func RegisterModifier

func RegisterModifier(
	name string,
	modifierFactory func(map[string]interface{}) func(interface{}) (interface{}, error),
	appliesToRequest bool,
	appliesToResponse bool,
)

RegisterModifier registers the injected modifier factory with the given name at the selected namespace

Types

type ContextRegisterer added in v2.6.0

type ContextRegisterer interface {
	RegisterContext(context.Context)
}

type LoggerRegisterer

type LoggerRegisterer interface {
	RegisterLogger(interface{})
}

type ModifierFactory

type ModifierFactory func(map[string]interface{}) func(interface{}) (interface{}, error)

ModifierFactory is a function that, given a config passed as a map, returns a modifier

func GetRequestModifier

func GetRequestModifier(name string) (ModifierFactory, bool)

GetRequestModifier returns a ModifierFactory from the request namespace by name

func GetResponseModifier

func GetResponseModifier(name string) (ModifierFactory, bool)

GetResponseModifier returns a ModifierFactory from the response namespace by name

type Plugin

type Plugin interface {
	Lookup(name string) (plugin.Symbol, error)
}

Plugin is the interface of the loaded plugins

type RegisterModifierFunc

type RegisterModifierFunc func(
	name string,
	modifierFactory func(map[string]interface{}) func(interface{}) (interface{}, error),
	appliesToRequest bool,
	appliesToResponse bool,
)

RegisterModifierFunc type is the function passed to the loaded Registerers

type Registerer

type Registerer interface {
	RegisterModifiers(func(
		name string,
		modifierFactory func(map[string]interface{}) func(interface{}) (interface{}, error),
		appliesToRequest bool,
		appliesToResponse bool,
	))
}

Registerer defines the interface for the plugins to expose in order to be able to be loaded/registered

Directories

Path Synopsis
tests

Jump to

Keyboard shortcuts

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