-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoints.go
69 lines (58 loc) · 1.89 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
package innsecure
import (
"context"
"errors"
"github.com/go-kit/kit/endpoint"
)
const UserContextKey = "user"
// Endpoints collects all of the service's endpoints.
type Endpoints struct {
ListBookings endpoint.Endpoint
CreateBooking endpoint.Endpoint
GetBookingByID endpoint.Endpoint
}
func contextToUser(ctx context.Context) *User {
u, ok := ctx.Value(UserContextKey).(*User)
if !ok {
return nil
}
return u
}
// MakeServerEndpoints returns an Endpoints struct where each endpoint invokes
// the corresponding method on the provided service.
func MakeServerEndpoints(s Service, jwtmw endpoint.Middleware) Endpoints {
return Endpoints{
ListBookings: jwtmw(MakeListBookingsEndpoint(s)),
CreateBooking: jwtmw(MakeCreateBookingEndpoint(s)),
GetBookingByID: jwtmw(MakeGetBookingByIDEndpoint(s)),
}
}
// MakeListBookingsEndpoint returns an endpoint wrapping the given server.
func MakeListBookingsEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, _ interface{}) (response interface{}, err error) {
u := contextToUser(ctx)
return s.ListBookings(ctx, u)
}
}
// MakeCreateBookingEndpoint returns an endpoint wrapping the given server.
func MakeCreateBookingEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
b, ok := request.(Booking)
if !ok {
return nil, errors.New("invalid request type, likely bad wiring")
}
u := contextToUser(ctx)
return s.CreateBooking(ctx, u, b)
}
}
// MakeGetBookingByIDEndpoint returns an endpoint wrapping the given server.
func MakeGetBookingByIDEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
id, ok := request.(string)
if !ok {
return nil, errors.New("invalid request type, likely bad wiring")
}
u := contextToUser(ctx)
return s.GetBookingByID(ctx, u, id)
}
}