-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
grpcApi.go
207 lines (184 loc) · 7.43 KB
/
grpcApi.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
package service
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/plgd-dev/hub/v2/grpc-gateway/pb"
clientIS "github.com/plgd-dev/hub/v2/identity-store/client"
pbIS "github.com/plgd-dev/hub/v2/identity-store/pb"
"github.com/plgd-dev/hub/v2/pkg/config/database"
"github.com/plgd-dev/hub/v2/pkg/fn"
"github.com/plgd-dev/hub/v2/pkg/fsnotify"
"github.com/plgd-dev/hub/v2/pkg/log"
"github.com/plgd-dev/hub/v2/pkg/net/grpc/client"
"github.com/plgd-dev/hub/v2/pkg/net/grpc/server"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
naClient "github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventbus/nats/client"
"github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventbus/nats/subscriber"
"github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventstore"
eventstoreConfig "github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventstore/config"
"github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventstore/cqldb"
mongodb "github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventstore/mongodb"
"github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/utils"
pbRD "github.com/plgd-dev/hub/v2/resource-directory/pb"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
)
// RequestHandler handles incoming requests.
type RequestHandler struct {
pb.UnimplementedGrpcGatewayServer
pbRD.UnimplementedResourceDirectoryServer
resourceProjection *Projection
eventStore eventstore.EventStore
publicConfiguration PublicConfiguration
ownerCache *clientIS.OwnerCache
closeFunc fn.FuncList
hubID string
}
func (r *RequestHandler) Close() {
r.closeFunc.Execute()
}
func AddHandler(ctx context.Context, svr *server.Server, config Config, publicConfiguration PublicConfiguration, fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider, goroutinePoolGo func(func()) error) error {
handler, err := newRequestHandlerFromConfig(ctx, config, publicConfiguration, fileWatcher, logger, tracerProvider, goroutinePoolGo)
if err != nil {
return err
}
svr.AddCloseFunc(handler.Close)
pb.RegisterGrpcGatewayServer(svr.Server, handler)
pbRD.RegisterResourceDirectoryServer(svr.Server, handler)
return nil
}
// Register registers the handler instance with a gRPC server.
func Register(server *grpc.Server, handler *RequestHandler) {
pb.RegisterGrpcGatewayServer(server, handler)
}
func newIdentityStoreClient(ctx context.Context, config IdentityStoreConfig, fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider) (pbIS.IdentityStoreClient, func(), error) {
var closeIsClient fn.FuncList
isConn, err := client.New(ctx, config.Connection, fileWatcher, logger, tracerProvider)
if err != nil {
return nil, nil, fmt.Errorf("cannot connect to identity-store: %w", err)
}
closeIsClient.AddFunc(func() {
if err := isConn.Close(); err != nil {
logger.Errorf("error occurs during close connection to identity-store: %w", err)
}
})
isClient := pbIS.NewIdentityStoreClient(isConn.GRPC())
return isClient, closeIsClient.ToFunction(), nil
}
func createEventStore(ctx context.Context, config eventstoreConfig.Config, fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider) (eventstore.EventStore, error) {
switch config.Use {
case database.MongoDB:
s, err := mongodb.New(ctx, config.MongoDB, fileWatcher, logger, tracerProvider, mongodb.WithUnmarshaler(utils.Unmarshal), mongodb.WithMarshaler(utils.Marshal))
if err != nil {
return nil, fmt.Errorf("mongodb: %w", err)
}
return s, nil
case database.CqlDB:
s, err := cqldb.New(ctx, config.CqlDB, fileWatcher, logger, tracerProvider, cqldb.WithUnmarshaler(utils.Unmarshal), cqldb.WithMarshaler(utils.Marshal))
if err != nil {
return nil, fmt.Errorf("cqldb: %w", err)
}
return s, nil
}
return nil, fmt.Errorf("invalid eventstore use('%v')", config.Use)
}
func newRequestHandlerFromConfig(ctx context.Context, config Config, publicConfiguration PublicConfiguration, fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider, goroutinePoolGo func(func()) error) (*RequestHandler, error) {
var closeFunc fn.FuncList
if publicConfiguration.CAPool != "" {
content, err := publicConfiguration.CAPool.Read()
if err != nil {
return nil, fmt.Errorf("cannot read file %v: %w", publicConfiguration.CAPool, err)
}
publicConfiguration.cloudCertificateAuthorities = string(content)
}
isClient, closeIsClient, err := newIdentityStoreClient(ctx, config.Clients.IdentityStore, fileWatcher, logger, tracerProvider)
if err != nil {
return nil, fmt.Errorf("cannot create identity-store client: %w", err)
}
closeFunc.AddFunc(closeIsClient)
eventstore, err := createEventStore(ctx, config.Clients.Eventstore.Connection, fileWatcher, logger, tracerProvider)
if err != nil {
closeFunc.Execute()
return nil, fmt.Errorf("cannot create resource eventstore %w", err)
}
closeFunc.AddFunc(func() {
if errC := eventstore.Close(ctx); errC != nil {
logger.Errorf("error occurs during close connection to eventstore: %w", errC)
}
})
natsClient, err := naClient.New(config.Clients.Eventbus.NATS, fileWatcher, logger)
if err != nil {
closeFunc.Execute()
return nil, fmt.Errorf("cannot create nats client: %w", err)
}
closeFunc.AddFunc(natsClient.Close)
resourceSubscriber, err := subscriber.New(natsClient.GetConn(),
config.Clients.Eventbus.NATS.PendingLimits,
logger,
subscriber.WithGoPool(goroutinePoolGo),
subscriber.WithUnmarshaler(utils.Unmarshal),
)
if err != nil {
closeFunc.Execute()
return nil, fmt.Errorf("cannot create eventbus subscriber: %w", err)
}
closeFunc.AddFunc(resourceSubscriber.Close)
mf := NewEventStoreModelFactory()
projUUID, err := uuid.NewRandom()
if err != nil {
closeFunc.Execute()
return nil, fmt.Errorf("cannot create uuid for projection %w", err)
}
resourceProjection, err := NewProjection(ctx, projUUID.String(), eventstore, resourceSubscriber, mf, config.Clients.Eventstore.ProjectionCacheExpiration)
if err != nil {
closeFunc.Execute()
return nil, fmt.Errorf("cannot create projection over resource aggregate events: %w", err)
}
ownerCache := clientIS.NewOwnerCache(config.APIs.GRPC.Authorization.OwnerClaim,
config.APIs.GRPC.OwnerCacheExpiration,
natsClient.GetConn(),
isClient, func(err error) {
log.Errorf("ownerCache error: %w", err)
},
)
closeFunc.AddFunc(ownerCache.Close)
h := NewRequestHandler(
config.HubID,
resourceProjection,
eventstore,
publicConfiguration,
ownerCache,
closeFunc,
)
return h, nil
}
// NewRequestHandler factory for new RequestHandler.
func NewRequestHandler(
hubID string,
resourceProjection *Projection,
eventstore eventstore.EventStore,
publicConfiguration PublicConfiguration,
ownerCache *clientIS.OwnerCache,
closeFunc fn.FuncList,
) *RequestHandler {
return &RequestHandler{
hubID: hubID,
resourceProjection: resourceProjection,
eventStore: eventstore,
publicConfiguration: publicConfiguration,
ownerCache: ownerCache,
closeFunc: closeFunc,
}
}
func NewEventStoreModelFactory() func(context.Context, string, string) (eventstore.Model, error) {
return func(_ context.Context, deviceID, resourceID string) (eventstore.Model, error) {
switch resourceID {
case commands.MakeLinksResourceUUID(deviceID).String():
return NewResourceLinksProjection(deviceID), nil
case commands.MakeStatusResourceUUID(deviceID).String():
return NewDeviceMetadataProjection(deviceID), nil
}
return NewResourceProjection(), nil
}
}