/
transport_grpc.go
255 lines (233 loc) · 8.13 KB
/
transport_grpc.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package sessionmanagement
import (
"time"
"github.com/go-kit/kit/log"
client "github.com/solher/styx/sessionmanagement/client/grpc"
grpctransport "github.com/go-kit/kit/transport/grpc"
"github.com/golang/protobuf/ptypes"
stdopentracing "github.com/opentracing/opentracing-go"
"github.com/solher/kitty"
"github.com/solher/styx/pb"
"github.com/solher/styx/sessions"
"golang.org/x/net/context"
)
// MakeGRPCServer makes a set of endpoints available as a gRPC server.
func MakeGRPCServer(ctx context.Context, endpoints Endpoints, tracer stdopentracing.Tracer, logger log.Logger) pb.SessionManagementServer {
opts := []grpctransport.ServerOption{
grpctransport.ServerErrorLogger(logger),
}
return &grpcServer{
createSession: grpctransport.NewServer(
ctx,
endpoints.CreateSessionEndpoint,
DecodeGRPCCreateSessionRequest,
EncodeGRPCCreateSessionResponse,
append(
opts,
grpctransport.ServerBefore(kitty.FromGRPCRequest(tracer, "Create session", logger)),
grpctransport.ServerAfter(kitty.GRPCFinish()),
)...,
),
findSessionByToken: grpctransport.NewServer(
ctx,
endpoints.FindSessionByTokenEndpoint,
DecodeGRPCFindSessionByTokenRequest,
EncodeGRPCFindSessionByTokenResponse,
append(
opts,
grpctransport.ServerBefore(kitty.FromGRPCRequest(tracer, "Find session by token", logger)),
grpctransport.ServerAfter(kitty.GRPCFinish()),
)...,
),
deleteSessionByToken: grpctransport.NewServer(
ctx,
endpoints.DeleteSessionByTokenEndpoint,
DecodeGRPCDeleteSessionByTokenRequest,
EncodeGRPCDeleteSessionByTokenResponse,
append(
opts,
grpctransport.ServerBefore(kitty.FromGRPCRequest(tracer, "Delete session by token", logger)),
grpctransport.ServerAfter(kitty.GRPCFinish()),
)...,
),
deleteSessionsByOwnerToken: grpctransport.NewServer(
ctx,
endpoints.DeleteSessionsByOwnerTokenEndpoint,
DecodeGRPCDeleteSessionsByOwnerTokenRequest,
EncodeGRPCDeleteSessionsByOwnerTokenResponse,
append(
opts,
grpctransport.ServerBefore(kitty.FromGRPCRequest(tracer, "Delete sessions by owner token", logger)),
grpctransport.ServerAfter(kitty.GRPCFinish()),
)...,
),
}
}
type grpcServer struct {
createSession grpctransport.Handler
findSessionByToken grpctransport.Handler
deleteSessionByToken grpctransport.Handler
deleteSessionsByOwnerToken grpctransport.Handler
}
func (s *grpcServer) CreateSession(ctx context.Context, req *pb.CreateSessionRequest) (*pb.CreateSessionReply, error) {
_, rep, err := s.createSession.ServeGRPC(ctx, req)
if err != nil {
return nil, err
}
return rep.(*pb.CreateSessionReply), nil
}
func (s *grpcServer) FindSessionByToken(ctx context.Context, req *pb.FindSessionByTokenRequest) (*pb.FindSessionByTokenReply, error) {
_, rep, err := s.findSessionByToken.ServeGRPC(ctx, req)
if err != nil {
return nil, err
}
return rep.(*pb.FindSessionByTokenReply), nil
}
func (s *grpcServer) DeleteSessionByToken(ctx context.Context, req *pb.DeleteSessionByTokenRequest) (*pb.DeleteSessionByTokenReply, error) {
_, rep, err := s.deleteSessionByToken.ServeGRPC(ctx, req)
if err != nil {
return nil, err
}
return rep.(*pb.DeleteSessionByTokenReply), nil
}
func (s *grpcServer) DeleteSessionsByOwnerToken(ctx context.Context, req *pb.DeleteSessionsByOwnerTokenRequest) (*pb.DeleteSessionsByOwnerTokenReply, error) {
_, rep, err := s.deleteSessionsByOwnerToken.ServeGRPC(ctx, req)
if err != nil {
return nil, err
}
return rep.(*pb.DeleteSessionsByOwnerTokenReply), nil
}
// DecodeGRPCCreateSessionRequest is a transport/grpc.DecodeRequestFunc that converts a
// gRPC request to a user-domain request.
func DecodeGRPCCreateSessionRequest(_ context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.CreateSessionRequest)
return createSessionRequest{
Session: toSession(req.Session),
}, nil
}
// EncodeGRPCCreateSessionResponse is a transport/grpc.EncodeResponseFunc that converts a
// user-domain response to a gRPC reply.
func EncodeGRPCCreateSessionResponse(_ context.Context, response interface{}) (interface{}, error) {
res := response.(createSessionResponse)
return &pb.CreateSessionReply{
Session: toPBSession(res.Session),
Err: toPBError(res.Err),
}, nil
}
// DecodeGRPCFindSessionByTokenRequest is a transport/grpc.DecodeRequestFunc that converts a
// gRPC request to a user-domain request.
func DecodeGRPCFindSessionByTokenRequest(_ context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.FindSessionByTokenRequest)
return findSessionByTokenRequest{
Token: req.Token,
}, nil
}
// EncodeGRPCFindSessionByTokenResponse is a transport/grpc.EncodeResponseFunc that converts a
// user-domain response to a gRPC reply.
func EncodeGRPCFindSessionByTokenResponse(_ context.Context, response interface{}) (interface{}, error) {
res := response.(findSessionByTokenResponse)
return &pb.FindSessionByTokenReply{
Session: toPBSession(res.Session),
Err: toPBError(res.Err),
}, nil
}
// DecodeGRPCDeleteSessionByTokenRequest is a transport/grpc.DecodeRequestFunc that converts a
// gRPC request to a user-domain request.
func DecodeGRPCDeleteSessionByTokenRequest(_ context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.DeleteSessionByTokenRequest)
return deleteSessionByTokenRequest{
Token: req.Token,
}, nil
}
// EncodeGRPCDeleteSessionByTokenResponse is a transport/grpc.EncodeResponseFunc that converts a
// user-domain response to a gRPC reply.
func EncodeGRPCDeleteSessionByTokenResponse(_ context.Context, response interface{}) (interface{}, error) {
res := response.(deleteSessionByTokenResponse)
return &pb.DeleteSessionByTokenReply{
Session: toPBSession(res.Session),
Err: toPBError(res.Err),
}, nil
}
// DecodeGRPCDeleteSessionsByOwnerTokenRequest is a transport/grpc.DecodeRequestFunc that converts a
// gRPC request to a user-domain request.
func DecodeGRPCDeleteSessionsByOwnerTokenRequest(_ context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.DeleteSessionsByOwnerTokenRequest)
return deleteSessionsByOwnerTokenRequest{
OwnerToken: req.OwnerToken,
}, nil
}
// EncodeGRPCDeleteSessionsByOwnerTokenResponse is a transport/grpc.EncodeResponseFunc that converts a
// user-domain response to a gRPC reply.
func EncodeGRPCDeleteSessionsByOwnerTokenResponse(_ context.Context, response interface{}) (interface{}, error) {
res := response.(deleteSessionsByOwnerTokenResponse)
return &pb.DeleteSessionsByOwnerTokenReply{
Sessions: toPBSessions(res.Sessions),
Err: toPBError(res.Err),
}, nil
}
func toPBError(err error) string {
if err == nil {
return ""
}
if _, _, ok := isErrValidation(err); ok {
return client.ErrValidation.Error()
} else if isErrNotFound(err) {
return client.ErrNotFound.Error()
}
return err.Error()
}
func toPBSession(session *sessions.Session) *pb.Session {
if session == nil {
return nil
}
return toPBSessions([]sessions.Session{*session})[0]
}
func toPBSessions(sessions []sessions.Session) []*pb.Session {
pbSessionSl := make([]*pb.Session, len(sessions))
for i, n := range sessions {
created, _ := ptypes.TimestampProto(*n.Created)
validTo, _ := ptypes.TimestampProto(*n.ValidTo)
pbSessionSl[i] = &pb.Session{
Created: created,
ValidTo: validTo,
Token: n.Token,
OwnerToken: n.OwnerToken,
Agent: n.Agent,
Ip: n.IP,
Policies: n.Policies,
Payload: n.Payload,
}
}
return pbSessionSl
}
func toSession(pbSession *pb.Session) *sessions.Session {
if pbSession == nil {
return nil
}
return &toSessions([]*pb.Session{pbSession})[0]
}
func toSessions(pbSessions []*pb.Session) []sessions.Session {
sessionSl := make([]sessions.Session, len(pbSessions))
for i, n := range pbSessions {
var created, validTo *time.Time
if n.Created != nil {
c, _ := ptypes.Timestamp(n.Created)
created = &c
}
if n.ValidTo != nil {
v, _ := ptypes.Timestamp(n.ValidTo)
validTo = &v
}
sessionSl[i] = sessions.Session{
Created: created,
ValidTo: validTo,
Token: n.Token,
OwnerToken: n.OwnerToken,
Agent: n.Agent,
IP: n.Ip,
Policies: n.Policies,
Payload: n.Payload,
}
}
return sessionSl
}