plugin

package
v2.0.0-dev Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2021 License: Apache-2.0 Imports: 11 Imported by: 4

Documentation

Overview

Package plugin provides plugin register interfaces for building http client plugins.

Usage example:

package main

import (
	"context"
	"errors"
	"fmt"
	"html"
	"net/http"
)

// ClientRegisterer is the symbol the plugin loader will try to load. It must implement the RegisterClient interface
var ClientRegisterer = registerer("lura-example")

type registerer string

func (r registerer) RegisterClients(f func(
	name string,
	handler func(context.Context, map[string]interface{}) (http.Handler, error),
)) {
	f(string(r), r.registerClients)
}

func (r registerer) registerClients(ctx context.Context, extra map[string]interface{}) (http.Handler, error) {
	// check the passed configuration and initialize the plugin
	name, ok := extra["name"].(string)
	if !ok {
		return nil, errors.New("wrong config")
	}
	if name != string(r) {
		return nil, fmt.Errorf("unknown register %s", name)
	}
	// return the actual handler wrapping or your custom logic so it can be used as a replacement for the default http client
	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Hello, %q", html.EscapeString(req.URL.Path))
	}), nil
}

func init() {
	fmt.Println("lura-example client plugin loaded!!!")
}

func main() {}

SPDX-License-Identifier: Apache-2.0

SPDX-License-Identifier: Apache-2.0

SPDX-License-Identifier: Apache-2.0

Index

Constants

View Source
const Namespace = "github.com/devopsfaith/krakend/transport/http/client/executor"

Variables

This section is empty.

Functions

func HTTPRequestExecutor

func HTTPRequestExecutor(
	logger logging.Logger,
	next func(*config.Backend) client.HTTPRequestExecutor,
) func(*config.Backend) client.HTTPRequestExecutor

func Load

func Load(path, pattern string, rcf RegisterClientFunc) (int, error)

func LoadWithLogger

func LoadWithLogger(path, pattern string, rcf RegisterClientFunc, logger logging.Logger) (int, error)

func RegisterClient

func RegisterClient(
	name string,
	handler func(context.Context, map[string]interface{}) (http.Handler, error),
)

Types

type LoggerRegisterer

type LoggerRegisterer interface {
	RegisterLogger(interface{})
}

type Plugin

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

Plugin is the interface of the loaded plugins

type RegisterClientFunc

type RegisterClientFunc func(
	name string,
	handler func(context.Context, map[string]interface{}) (http.Handler, error),
)

type Registerer

type Registerer interface {
	RegisterClients(func(
		name string,
		handler func(context.Context, map[string]interface{}) (http.Handler, error),
	))
}

Directories

Path Synopsis
SPDX-License-Identifier: Apache-2.0
SPDX-License-Identifier: Apache-2.0

Jump to

Keyboard shortcuts

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