endpoint

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package endpoint provides helpers and additional functionality for github.com/go-kit/kit/endpoint .

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyMiddlewares

func ApplyMiddlewares(e endpoint.Endpoint, mws ...endpoint.Middleware) endpoint.Endpoint

Applies zero or more middlewares to an endpoint. Middlewares are applied in order, i.e. first middleware passed is applied first and therefore innermost, last middleware passed is outermost.

func ErrorLoggingMiddleware

func ErrorLoggingMiddleware(logger log.Logger) endpoint.Middleware

Logs errors contained in the endpoint response, if the response type implements the Responder interface. Errors from endpoint code should be caught/logged with transport level error handlers (see e.g. the errorHandler option to github.com/go-kit/kit/transport/http.NewServer).

func InstrumentRequestTimeMiddleware

func InstrumentRequestTimeMiddleware(duration metrics.Histogram) endpoint.Middleware

Endpoint middleware that records to the given histrogram the time it takes the endpoint to process requests.

Types

type Responder

type Responder interface {
	Response() interface{}
	Error() error
}

A type implementing Responder can be used to wrap a result and error value returned by the business logic/service/component part of an endpoint. This makes it easier to distinguish between errors originating in business logic/service/component code of an endpoint and errors originating in endpoint code itself, e.g. an endpoint middleware. An error from the business logic/service/component code will be wrapped in the Responder type and therefore returned as part of the "response" (first) return value of an endpoint function. Whereas an error from endpoint code will be returned as the error return value of an endpoint function.

The following examples demonstrate the intended usage of Responder:

func endpointFunc(ctx context.Context, request interface{}) (interface{}, error) {
	result, err := service.SomeServiceMethod(ctx, request)
	// Wrap error from service in Response, the default type implementing Responder.
	// The error return value of this endpoint function will be nil.
	return Response{
		R: result,
		Err: err,
	}, nil
}

// Errors from an endpoint middleware will be returned in the error return value of the endpoint function.
func exampleEndpointMiddleware() endpoint.Middleware {
	return func(next endpoint.Endpoint) endpoint.Endpoint {
		return func(ctx context.Context, request interface{}) (interface{}, error) {
			err := PerformSomeOperationThatMightFail(request)
			if err != nil {
				return nil, err
			}
			return next(ctx, request)
		}
	}
}

type Response

type Response struct {
	R   interface{}
	Err error
}

Default implementation of Responder.

func (Response) Error

func (r Response) Error() error

func (Response) Response

func (r Response) Response() interface{}

Jump to

Keyboard shortcuts

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