/
grpc.go
172 lines (150 loc) · 5.11 KB
/
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
package grpc
import (
"context"
grpctransport "github.com/go-kit/kit/transport/grpc"
pb "github.com/karta0898098/iam/pb/identity"
"github.com/karta0898098/iam/pkg/app/identity/endpoints"
"github.com/karta0898098/iam/pkg/app/identity/entity"
)
type grpcServer struct {
signin grpctransport.Handler
signup grpctransport.Handler
}
func (g *grpcServer) Signin(ctx context.Context, req *pb.SigninReq) (*pb.SigninResp, error) {
_, rp, err := g.signin.ServeGRPC(ctx, req)
if err != nil {
return nil, err
}
reply := (rp).(*pb.SigninResp)
return reply, nil
}
func (g *grpcServer) Signup(ctx context.Context, req *pb.SignupReq) (*pb.SignupResp, error) {
_, rp, err := g.signup.ServeGRPC(ctx, req)
if err != nil {
return nil, err
}
reply := (rp).(*pb.SignupResp)
return reply, nil
}
func MakeGRPCServer(endpoints endpoints.Endpoints) (req pb.IdentityServiceServer) {
options := []grpctransport.ServerOption{}
return &grpcServer{
signin: grpctransport.NewServer(
endpoints.SigninEndpoint,
decodeGRPCSigninRequest,
encodeGRPCSigninResponse,
options...,
),
signup: grpctransport.NewServer(
endpoints.SignupEndpoint,
decodeGRPCSignupRequest,
encodeGRPCSignupResponse,
options...,
),
}
}
// decodeGRPCSigninRequest is a transport/grpc.DecodeRequestFunc that converts a
// gRPC request to a user-domain request. Primarily useful in a server.
func decodeGRPCSigninRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(*pb.SigninReq)
return &endpoints.SigninRequest{
Username: req.Username,
Password: req.Password,
IPAddress: req.IPAddress,
Device: entity.Device{
Model: req.Device.Model,
Name: req.Device.Name,
OSVersion: req.Device.OSVersion,
},
}, nil
}
// encodeGRPCSigninResponse is a transport/grpc.EncodeResponseFunc that converts a
// user-domain response to a gRPC reply. Primarily useful in a server.
func encodeGRPCSigninResponse(_ context.Context, grpcReply interface{}) (res interface{}, err error) {
reply := grpcReply.(*endpoints.SigninResponse)
return &pb.SigninResp{
AccessToken: reply.AccessToken,
RefreshToken: reply.RefreshToken,
IDToken: reply.IDToken,
}, nil
}
// encodeGRPCSigninRequest is a transport/grpc.EncodeRequestFunc that converts a
// user-domain Signin request to a gRPC Signin request. Primarily useful in a client.
func encodeGRPCSigninRequest(_ context.Context, request interface{}) (interface{}, error) {
req := request.(*endpoints.SigninRequest)
return &pb.SigninReq{
Username: req.Username,
Password: req.Password,
IPAddress: req.IPAddress,
Device: &pb.Device{
Model: req.Device.Model,
Name: req.Device.Name,
OSVersion: req.Device.OSVersion,
},
IdpProvider: req.IdpProvider,
}, nil
}
func decodeGRPCSigninResponse(ctx context.Context, grpcReply interface{}) (response interface{}, err error) {
reply := grpcReply.(*pb.SigninResp)
return &endpoints.SigninResponse{
IDToken: reply.IDToken,
AccessToken: reply.AccessToken,
RefreshToken: reply.RefreshToken,
}, nil
}
// decodeGRPCSignupRequest is a transport/grpc.DecodeRequestFunc that converts a
// gRPC request to a user-domain request. Primarily useful in a server.
func decodeGRPCSignupRequest(ctx context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(*pb.SignupReq)
return &endpoints.SignupRequest{
Username: req.Username,
Password: req.Password,
Nickname: req.Nickname,
FirstName: req.FirstName,
LastName: req.LastName,
Email: req.Email,
Platform: req.Platform,
IPAddress: req.IPAddress,
Device: entity.Device{
Model: req.Device.Model,
Name: req.Device.Name,
OSVersion: req.Device.OSVersion,
},
}, nil
}
// encodeGRPCSignupResponse is a transport/grpc.EncodeResponseFunc that converts a
// user-domain response to a gRPC reply. Primarily useful in a server.
func encodeGRPCSignupResponse(_ context.Context, grpcReply interface{}) (res interface{}, err error) {
reply := grpcReply.(*endpoints.SignupResponse)
return &pb.SignupResp{
AccessToken: reply.AccessToken,
RefreshToken: reply.RefreshToken,
IDToken: reply.IDToken,
}, nil
}
// encodeGRPCSignupRequest is a transport/grpc.EncodeRequestFunc that converts a
// user-domain Signup request to a gRPC Signup request. Primarily useful in a client.
func encodeGRPCSignupRequest(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(*endpoints.SigninRequest)
return &pb.SigninReq{
Username: req.Username,
Password: req.Password,
IPAddress: req.IPAddress,
Device: &pb.Device{
Model: req.Device.Model,
Name: req.Device.Name,
OSVersion: req.Device.OSVersion,
},
IdpProvider: req.IdpProvider,
}, nil
}
// decodeGRPCSignupResponse is a transport/grpc.DecodeResponseFunc that converts a
// gRPC Signup reply to a user-domain Sum response. Primarily useful in a client.
func decodeGRPCSignupResponse(ctx context.Context, grpcReply interface{}) (response interface{}, err error) {
reply := grpcReply.(*pb.SignupResp)
return &endpoints.SignupResponse{
IDToken: reply.IDToken,
AccessToken: reply.AccessToken,
RefreshToken: reply.RefreshToken,
}, nil
}