This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
endpoints.go
123 lines (104 loc) · 3.54 KB
/
endpoints.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package catalogue
// endpoints.go contains the endpoint definitions, including per-method request
// and response structs. Endpoints are the binding between the service and
// transport.
import (
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/tracing/opentracing"
stdopentracing "github.com/opentracing/opentracing-go"
"golang.org/x/net/context"
)
// Endpoints collects the endpoints that comprise the Service.
type Endpoints struct {
ListEndpoint endpoint.Endpoint
CountEndpoint endpoint.Endpoint
GetEndpoint endpoint.Endpoint
TagsEndpoint endpoint.Endpoint
HealthEndpoint endpoint.Endpoint
}
// MakeEndpoints returns an Endpoints structure, where each endpoint is
// backed by the given service.
func MakeEndpoints(s Service, tracer stdopentracing.Tracer) Endpoints {
return Endpoints{
ListEndpoint: opentracing.TraceServer(tracer, "GET /catalogue")(MakeListEndpoint(s)),
CountEndpoint: opentracing.TraceServer(tracer, "GET /catalogue/size")(MakeCountEndpoint(s)),
GetEndpoint: opentracing.TraceServer(tracer, "GET /catalogue/{id}")(MakeGetEndpoint(s)),
TagsEndpoint: opentracing.TraceServer(tracer, "GET /tags")(MakeTagsEndpoint(s)),
HealthEndpoint: opentracing.TraceServer(tracer, "GET /health")(MakeHealthEndpoint(s)),
}
}
// MakeListEndpoint returns an endpoint via the given service.
func MakeListEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(listRequest)
socks, err := s.List(req.Tags, req.Order, req.PageNum, req.PageSize)
return listResponse{Socks: socks, Err: err}, err
}
}
// MakeCountEndpoint returns an endpoint via the given service.
func MakeCountEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(countRequest)
n, err := s.Count(req.Tags)
return countResponse{N: n, Err: err}, err
}
}
// MakeGetEndpoint returns an endpoint via the given service.
func MakeGetEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(getRequest)
sock, err := s.Get(req.ID)
return getResponse{Sock: sock, Err: err}, err
}
}
// MakeTagsEndpoint returns an endpoint via the given service.
func MakeTagsEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
tags, err := s.Tags()
return tagsResponse{Tags: tags, Err: err}, err
}
}
// MakeHealthEndpoint returns current health of the given service.
func MakeHealthEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
health := s.Health()
return healthResponse{Health: health}, nil
}
}
type listRequest struct {
Tags []string `json:"tags"`
Order string `json:"order"`
PageNum int `json:"pageNum"`
PageSize int `json:"pageSize"`
}
type listResponse struct {
Socks []Sock `json:"sock"`
Err error `json:"err"`
}
type countRequest struct {
Tags []string `json:"tags"`
}
type countResponse struct {
N int `json:"size"` // to match original
Err error `json:"err"`
}
type getRequest struct {
ID string `json:"id"`
}
type getResponse struct {
Sock Sock `json:"sock"`
Err error `json:"err"`
}
type tagsRequest struct {
//
}
type tagsResponse struct {
Tags []string `json:"tags"`
Err error `json:"err"`
}
type healthRequest struct {
//
}
type healthResponse struct {
Health []Health `json:"health"`
}