package endpoint
import "github.com/go-kit/kit/endpoint"
Package endpoint defines an abstraction for RPCs.
Endpoints are a fundamental building block for many Go kit components. Endpoints are implemented by servers, and called by clients.
Index ¶
- func Nop(context.Context, interface{}) (interface{}, error)
- type Endpoint
- type Failer
- type Middleware
Examples ¶
Functions ¶
func Nop ¶
Nop is an endpoint that does nothing and returns a nil error. Useful for tests.
Types ¶
type Endpoint ¶
Endpoint is the fundamental building block of servers and clients. It represents a single RPC method.
type Failer ¶
type Failer interface { Failed() error }
Failer may be implemented by Go kit response types that contain business logic error details. If Failed returns a non-nil error, the Go kit transport layer may interpret this as a business logic error, and may encode it differently than a regular, successful response.
It's not necessary for your response types to implement Failer, but it may help for more sophisticated use cases. The addsvc example shows how Failer should be used by a complete application.
type Middleware ¶
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.
Code:play
Output:Example¶
package main
import (
"context"
"fmt"
"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
}
first pre
second pre
third pre
my endpoint!
third post
second post
first post
Source Files ¶
- Version
- v0.13.0 (latest)
- Published
- May 29, 2023
- Platform
- linux/amd64
- Imports
- 1 packages
- Last checked
- 4 hours ago –
Tools for package owners.