endpoint

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2020 License: MIT Imports: 2 Imported by: 10

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain added in v0.2.0

Chain composes a single middleware from a list. Compared to endpoint.Chain, this function accepts a variadic list. Deprecated: use Combine instead.

Example
annotate := func(pos string) endpoint.Middleware {
	return func(e endpoint.Endpoint) endpoint.Endpoint {
		return func(ctx context.Context, req interface{}) (response interface{}, err error) {
			fmt.Println(pos + " pre")

			response, err = e(ctx, req)

			fmt.Println(pos + " post")

			return
		}
	}
}
e := Chain(
	annotate("first"),
	annotate("second"),
	annotate("third"),
)(
	func(context.Context, interface{}) (interface{}, error) {
		fmt.Println("endpoint")

		return nil, nil
	},
)

if _, err := e(ctx, req); err != nil {
	panic(err)
}
Output:

first pre
second pre
third pre
endpoint
third post
second post
first post

func Combine added in v0.10.0

Combine composes a single middleware from a list. Compared to endpoint.Chain, this function accepts a variadic list.

Example
annotate := func(pos string) endpoint.Middleware {
	return func(e endpoint.Endpoint) endpoint.Endpoint {
		return func(ctx context.Context, req interface{}) (response interface{}, err error) {
			fmt.Println(pos + " pre")

			response, err = e(ctx, req)

			fmt.Println(pos + " post")

			return
		}
	}
}
e := Combine(
	annotate("first"),
	annotate("second"),
	annotate("third"),
)(
	func(context.Context, interface{}) (interface{}, error) {
		fmt.Println("endpoint")

		return nil, nil
	},
)

if _, err := e(ctx, req); err != nil {
	panic(err)
}
Output:

first pre
second pre
third pre
endpoint
third post
second post
first post

func FailerMiddleware added in v0.4.0

func FailerMiddleware(errorMatcher ErrorMatcher) endpoint.Middleware

FailerMiddleware checks if a returned error matches a predicate and wraps it in a failer response if it does.

func OperationName added in v0.10.0

func OperationName(ctx context.Context) (string, bool)

OperationName fetches the endpoint operation name from the context (if any). If an endpoint name is not found or it isn't string, the second return argument is false.

func OperationNameMiddleware added in v0.10.0

func OperationNameMiddleware(name string) endpoint.Middleware

OperationNameMiddleware populates the context with a common name for the endpoint. It can be used in subsequent endpoints in the chain to identify the operation.

Types

type ErrorMatcher added in v0.4.0

type ErrorMatcher func(err error) bool

ErrorMatcher is a predicate for errors. It can be used in middleware to decide whether to take action or not.

type Factory

type Factory interface {
	// NewEndpoint returns an endpoint wrapped with preconfigured middleware.
	// It also accepts an operation name for per operation middleware (eg. logging or tracing middleware).
	NewEndpoint(name string, e endpoint.Endpoint) endpoint.Endpoint
}

Factory returns an endpoint wrapped with preconfigured middleware.

func NewFactory

func NewFactory(middlewareFactories ...MiddlewareFactory) Factory

NewFactory returns a new Factory.

type MiddlewareFactory

type MiddlewareFactory func(name string) endpoint.Middleware

MiddlewareFactory creates a middleware per operation.

func Middleware

func Middleware(middleware endpoint.Middleware) MiddlewareFactory

Middleware wraps singleton middleware and wraps them in a MiddlewareFactory.

Jump to

Keyboard shortcuts

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