forked from microservices-demo/payment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints.go
68 lines (59 loc) · 2.14 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
package payment
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 {
AuthoriseEndpoint 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{
AuthoriseEndpoint: opentracing.TraceServer(tracer, "POST /paymentAuth")(MakeAuthoriseEndpoint(s)),
HealthEndpoint: opentracing.TraceServer(tracer, "GET /health")(MakeHealthEndpoint(s)),
}
}
// MakeListEndpoint returns an endpoint via the given service.
func MakeAuthoriseEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
var span stdopentracing.Span
span, ctx = stdopentracing.StartSpanFromContext(ctx, "authorize payment")
span.SetTag("service", "payment")
defer span.Finish()
req := request.(AuthoriseRequest)
authorisation, err := s.Authorise(req.Amount)
return AuthoriseResponse{Authorisation: authorisation, Err: err}, nil
}
}
// 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) {
var span stdopentracing.Span
span, ctx = stdopentracing.StartSpanFromContext(ctx, "health check")
span.SetTag("service", "payment")
defer span.Finish()
health := s.Health()
return healthResponse{Health: health}, nil
}
}
// AuthoriseRequest represents a request for payment authorisation.
// The Amount is the total amount of the transaction
type AuthoriseRequest struct {
Amount float32 `json:"amount"`
}
// AuthoriseResponse returns a response of type Authorisation and an error, Err.
type AuthoriseResponse struct {
Authorisation Authorisation
Err error
}
type healthRequest struct {
//
}
type healthResponse struct {
Health []Health `json:"health"`
}