/
endpoints.go
72 lines (59 loc) · 1.63 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
package main
import (
"context"
"errors"
"github.com/go-kit/kit/endpoint"
"strings"
)
// CalculateEndpoint define endpoint
type StringEndpoints struct {
StringEndpoint endpoint.Endpoint
HealthCheckEndpoint endpoint.Endpoint
}
var (
ErrInvalidRequestType = errors.New("RequestType has only two type: Concat, Diff")
)
// StringRequest define request struct
type StringRequest struct {
RequestType string `json:"request_type"`
A string `json:"a"`
B string `json:"b"`
}
// StringResponse define response struct
type StringResponse struct {
Result string `json:"result"`
Error error `json:"error"`
}
// MakeStringEndpoint make endpoint
func MakeStringEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(StringRequest)
var (
res, a, b string
opError error
)
a = req.A
b = req.B
if strings.EqualFold(req.RequestType, "Concat") {
res, _ = svc.Concat(a, b)
} else if strings.EqualFold(req.RequestType, "Diff") {
res, _ = svc.Diff(a, b)
} else {
return nil, ErrInvalidRequestType
}
return StringResponse{Result: res, Error: opError}, nil
}
}
// HealthRequest 健康检查请求结构
type HealthRequest struct{}
// HealthResponse 健康检查响应结构
type HealthResponse struct {
Status bool `json:"status"`
}
// MakeHealthCheckEndpoint 创建健康检查Endpoint
func MakeHealthCheckEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
status := svc.HealthCheck()
return HealthResponse{status}, nil
}
}