-
Notifications
You must be signed in to change notification settings - Fork 36
/
endpoints.go
76 lines (63 loc) · 1.86 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
package main
import (
"context"
"errors"
"github.com/go-kit/kit/endpoint"
"strings"
)
// CalculateEndpoint define endpoint
type ArithmeticEndpoints struct {
ArithmeticEndpoint endpoint.Endpoint
HealthCheckEndpoint endpoint.Endpoint
}
var (
ErrInvalidRequestType = errors.New("RequestType has only four type: Add,Subtract,Multiply,Divide")
)
// ArithmeticRequest define request struct
type ArithmeticRequest struct {
RequestType string `json:"request_type"`
A int `json:"a"`
B int `json:"b"`
}
// ArithmeticResponse define response struct
type ArithmeticResponse struct {
Result int `json:"result"`
Error error `json:"error"`
}
// MakeArithmeticEndpoint make endpoint
func MakeArithmeticEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(ArithmeticRequest)
var (
res, a, b int
calError error
)
a = req.A
b = req.B
if strings.EqualFold(req.RequestType, "Add") {
res = svc.Add(a, b)
} else if strings.EqualFold(req.RequestType, "Subtract") {
res = svc.Subtract(a, b)
} else if strings.EqualFold(req.RequestType, "Multiply") {
res = svc.Multiply(a, b)
} else if strings.EqualFold(req.RequestType, "Divide") {
res, calError = svc.Divide(a, b)
} else {
return nil, ErrInvalidRequestType
}
return ArithmeticResponse{Result: res, Error: calError}, 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
}
}