endpoint

package
v0.0.0-...-377bbca Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2015 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrBadCast = errors.New("bad cast")

ErrBadCast indicates an unexpected concrete request or response struct was received from an endpoint.

View Source
var ErrContextCanceled = errors.New("context canceled")

ContextCanceled indicates the request context was canceled.

Functions

This section is empty.

Types

type Endpoint

type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error)

Endpoint is the fundamental building block of servers and clients. It represents a single RPC method.

type Middleware

type Middleware func(Endpoint) Endpoint

Middleware is a chainable behavior modifier for endpoints.

func Chain

func Chain(outer Middleware, others ...Middleware) Middleware

Chain is a helper function for composing middlewares. Requests will traverse them in the order they're declared. That is, the first middleware is treated as the outermost middleware.

Example
package main

import (
	"fmt"

	"golang.org/x/net/context"

	"github.com/go-kit/kit/endpoint"
)

func main() {
	e := endpoint.Chain(
		annotate("first"),
		annotate("second"),
		annotate("third"),
	)(myEndpoint)

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

}

var (
	ctx = context.Background()
	req = struct{}{}
)

func annotate(s string) endpoint.Middleware {
	return func(next endpoint.Endpoint) endpoint.Endpoint {
		return func(ctx context.Context, request interface{}) (interface{}, error) {
			fmt.Println(s, "pre")
			defer fmt.Println(s, "post")
			return next(ctx, request)
		}
	}
}

func myEndpoint(context.Context, interface{}) (interface{}, error) {
	fmt.Println("my endpoint!")
	return struct{}{}, nil
}
Output:

first pre
second pre
third pre
my endpoint!
third post
second post
first post

Jump to

Keyboard shortcuts

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