This repository has been archived by the owner on May 18, 2022. It is now read-only.
/
endpoints.go
116 lines (97 loc) · 3.05 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
package api
import (
"context"
"github.com/lamassuiot/device-manufacturing-system/pkg/enroller/models/csr"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/tracing/opentracing"
stdopentracing "github.com/opentracing/opentracing-go"
)
type Endpoints struct {
HealthEndpoint endpoint.Endpoint
GetCSRsEndpoint endpoint.Endpoint
GetCSRStatusEndpoint endpoint.Endpoint
GetCRTEndpoint endpoint.Endpoint
}
func MakeServerEndpoints(s Service, otTracer stdopentracing.Tracer) Endpoints {
var healthEndpoint endpoint.Endpoint
{
healthEndpoint = MakeHealthEndpoint(s)
healthEndpoint = opentracing.TraceServer(otTracer, "Health")(healthEndpoint)
}
var getCSRsEndpoint endpoint.Endpoint
{
getCSRsEndpoint = MakeGetCSRsEndpoint(s)
getCSRsEndpoint = opentracing.TraceServer(otTracer, "GetPendingCSRs")(getCSRsEndpoint)
}
var getCSRStatusEndpoint endpoint.Endpoint
{
getCSRStatusEndpoint = MakeGetCSRStatusEndpoint(s)
getCSRStatusEndpoint = opentracing.TraceServer(otTracer, "GetPendingCSRDB")(getCSRStatusEndpoint)
}
var getCRTEndpoint endpoint.Endpoint
{
getCRTEndpoint = MakeGetCRTEndpoint(s)
getCRTEndpoint = opentracing.TraceServer(otTracer, "GetCRT")(getCRTEndpoint)
}
return Endpoints{
HealthEndpoint: healthEndpoint,
GetCSRsEndpoint: getCSRsEndpoint,
GetCSRStatusEndpoint: getCSRStatusEndpoint,
GetCRTEndpoint: getCRTEndpoint,
}
}
func MakeHealthEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
healthy := s.Health(ctx)
return healthResponse{Healthy: healthy}, nil
}
}
func MakeGetCSRsEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
_ = request.(getCSRsRequest)
csrs := s.GetCSRs(ctx)
return getCSRsResponse{CSRs: csrs}, nil
}
}
func MakeGetCSRStatusEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(getCSRStatusRequest)
csr, err := s.GetCSRStatus(ctx, req.ID)
return getCSRStatusResponse{CSR: csr, Err: err}, nil
}
}
func MakeGetCRTEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(getCRTRequest)
data, err := s.GetCRT(ctx, req.ID)
return getCRTResponse{Data: data, Err: err}, nil
}
}
type healthRequest struct{}
type healthResponse struct {
Healthy bool `json:"healthy,omitempty"`
Err error `json:"err,omitempty"`
}
type getCSRsRequest struct{}
type getCSRsResponse struct {
CSRs csr.CSRs `json:"csr"`
}
type getCSRsEmbeddedResponse struct {
CSRs csr.Data `json:"_embedded"`
}
type getCSRStatusRequest struct {
ID int
}
type getCSRStatusResponse struct {
CSR csr.CSR `json:"-"`
Err error `json:"err,omitempty"`
}
func (r getCSRStatusResponse) error() error { return r.Err }
type getCRTRequest struct {
ID int
}
type getCRTResponse struct {
Data []byte
Err error
}
func (r getCRTResponse) error() error { return r.Err }