-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathendpoint_example_test.go
More file actions
49 lines (41 loc) · 944 Bytes
/
endpoint_example_test.go
File metadata and controls
49 lines (41 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package endpoint_test
import (
"context"
"fmt"
"github.com/go-kit/kit/endpoint"
)
func ExampleChain() {
e := endpoint.Chain(
annotate[any, any]("first"),
annotate[any, any]("second"),
annotate[any, any]("third"),
)(myEndpoint)
if _, err := e(ctx, req); err != nil {
panic(err)
}
// Output:
// first pre
// second pre
// third pre
// my endpoint!
// third post
// second post
// first post
}
var (
ctx = context.Background()
req = struct{}{}
)
func annotate[Req any, Resp any](s string) endpoint.Middleware[Req, Resp] {
return func(next endpoint.Endpoint[Req, Resp]) endpoint.Endpoint[Req, Resp] {
return endpoint.Endpoint[Req, Resp](func(ctx context.Context, request Req) (Resp, 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
}