/
transport-service.go
222 lines (186 loc) · 7.15 KB
/
transport-service.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
// Package grpc implements functions to expose user service endpoint using GRPC protocol.
package grpc
import (
"context"
"fmt"
"net"
userGRPCContract "github.com/decentralized-cloud/user/contract/grpc/go"
"github.com/decentralized-cloud/user/services/configuration"
"github.com/decentralized-cloud/user/services/endpoint"
"github.com/decentralized-cloud/user/services/transport"
gokitgrpc "github.com/go-kit/kit/transport/grpc"
"github.com/micro-business/go-core/gokit/middleware"
commonErrors "github.com/micro-business/go-core/system/errors"
"go.uber.org/zap"
"google.golang.org/grpc"
)
type transportService struct {
logger *zap.Logger
configurationService configuration.ConfigurationContract
endpointCreatorService endpoint.EndpointCreatorContract
middlewareProviderService middleware.MiddlewareProviderContract
jwksURL string
createUserHandler gokitgrpc.Handler
readUserHandler gokitgrpc.Handler
updateUserHandler gokitgrpc.Handler
deleteUserHandler gokitgrpc.Handler
}
var Live bool
var Ready bool
func init() {
Live = false
Ready = false
}
// NewTransportService creates new instance of the transportService, setting up all dependencies and returns the instance
// logger: Mandatory. Reference to the logger service
// configurationService: Mandatory. Reference to the service that provides required configurations
// endpointCreatorService: Mandatory. Reference to the service that creates go-kit compatible endpoints
// middlewareProviderService: Mandatory. Reference to the service that provides different go-kit middlewares
// Returns the new service or error if something goes wrong
func NewTransportService(
logger *zap.Logger,
configurationService configuration.ConfigurationContract,
endpointCreatorService endpoint.EndpointCreatorContract,
middlewareProviderService middleware.MiddlewareProviderContract) (transport.TransportContract, error) {
if logger == nil {
return nil, commonErrors.NewArgumentNilError("logger", "logger is required")
}
if configurationService == nil {
return nil, commonErrors.NewArgumentNilError("configurationService", "configurationService is required")
}
if endpointCreatorService == nil {
return nil, commonErrors.NewArgumentNilError("endpointCreatorService", "endpointCreatorService is required")
}
if middlewareProviderService == nil {
return nil, commonErrors.NewArgumentNilError("middlewareProviderService", "middlewareProviderService is required")
}
jwksURL, err := configurationService.GetJwksURL()
if err != nil {
return nil, err
}
return &transportService{
logger: logger,
configurationService: configurationService,
endpointCreatorService: endpointCreatorService,
middlewareProviderService: middlewareProviderService,
jwksURL: jwksURL,
}, nil
}
// Start starts the GRPC transport service
// Returns error if something goes wrong
func (service *transportService) Start() error {
service.setupHandlers()
host, err := service.configurationService.GetGrpcHost()
if err != nil {
return err
}
port, err := service.configurationService.GetGrpcPort()
if err != nil {
return err
}
address := fmt.Sprintf("%s:%d", host, port)
listener, err := net.Listen("tcp", address)
if err != nil {
return err
}
gRPCServer := grpc.NewServer()
userGRPCContract.RegisterServiceServer(gRPCServer, service)
service.logger.Info("gRPC service started", zap.String("address", address))
Live = true
Ready = true
err = gRPCServer.Serve(listener)
Live = false
Ready = false
return err
}
// Stop stops the GRPC transport service
// Returns error if something goes wrong
func (service *transportService) Stop() error {
return nil
}
func (service *transportService) setupHandlers() {
endpoint := service.endpointCreatorService.CreateUserEndpoint()
endpoint = service.middlewareProviderService.CreateLoggingMiddleware("CreateUser")(endpoint)
endpoint = service.createAuthMiddleware("CreateUser")(endpoint)
service.createUserHandler = gokitgrpc.NewServer(
endpoint,
decodeCreateUserRequest,
encodeCreateUserResponse,
)
endpoint = service.endpointCreatorService.ReadUserEndpoint()
endpoint = service.middlewareProviderService.CreateLoggingMiddleware("ReadUser")(endpoint)
endpoint = service.createAuthMiddleware("ReadUser")(endpoint)
service.readUserHandler = gokitgrpc.NewServer(
endpoint,
decodeReadUserRequest,
encodeReadUserResponse,
)
endpoint = service.endpointCreatorService.UpdateUserEndpoint()
endpoint = service.middlewareProviderService.CreateLoggingMiddleware("UpdateUser")(endpoint)
endpoint = service.createAuthMiddleware("UpdateUser")(endpoint)
service.updateUserHandler = gokitgrpc.NewServer(
endpoint,
decodeUpdateUserRequest,
encodeUpdateUserResponse,
)
endpoint = service.endpointCreatorService.DeleteUserEndpoint()
endpoint = service.middlewareProviderService.CreateLoggingMiddleware("DeleteUser")(endpoint)
endpoint = service.createAuthMiddleware("DeleteUser")(endpoint)
service.deleteUserHandler = gokitgrpc.NewServer(
endpoint,
decodeDeleteUserRequest,
encodeDeleteUserResponse,
)
}
// CreateUser creates a new user
// context: Mandatory. The reference to the context
// request: mandatory. The request to create a new user
// Returns the result of creating new user
func (service *transportService) CreateUser(
ctx context.Context,
request *userGRPCContract.CreateUserRequest) (*userGRPCContract.CreateUserResponse, error) {
_, response, err := service.createUserHandler.ServeGRPC(ctx, request)
if err != nil {
return nil, err
}
return response.(*userGRPCContract.CreateUserResponse), nil
}
// ReadUser read an existing user
// context: Mandatory. The reference to the context
// request: Mandatory. The request to read an existing user
// Returns the result of reading an existing user
func (service *transportService) ReadUser(
ctx context.Context,
request *userGRPCContract.ReadUserRequest) (*userGRPCContract.ReadUserResponse, error) {
_, response, err := service.readUserHandler.ServeGRPC(ctx, request)
if err != nil {
return nil, err
}
return response.(*userGRPCContract.ReadUserResponse), nil
}
// UpdateUser update an existing user
// context: Mandatory. The reference to the context
// request: Mandatory. The request to update an existing user
// Returns the result of updateing an existing user
func (service *transportService) UpdateUser(
ctx context.Context,
request *userGRPCContract.UpdateUserRequest) (*userGRPCContract.UpdateUserResponse, error) {
_, response, err := service.updateUserHandler.ServeGRPC(ctx, request)
if err != nil {
return nil, err
}
return response.(*userGRPCContract.UpdateUserResponse), nil
}
// DeleteUser delete an existing user
// context: Mandatory. The reference to the context
// request: Mandatory. The request to delete an existing user
// Returns the result of deleting an existing user
func (service *transportService) DeleteUser(
ctx context.Context,
request *userGRPCContract.DeleteUserRequest) (*userGRPCContract.DeleteUserResponse, error) {
_, response, err := service.deleteUserHandler.ServeGRPC(ctx, request)
if err != nil {
return nil, err
}
return response.(*userGRPCContract.DeleteUserResponse), nil
}