-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_server.js
41 lines (35 loc) · 1.21 KB
/
grpc_server.js
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
var PROTO_PATH = __dirname + '/view/grpc.proto';
var grpc = require('@grpc/grpc-js');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
PROTO_PATH,
{keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
var grpc_proto = grpc.loadPackageDefinition(packageDefinition).grpc;
var userMessageMap = new Map();
function insert(call, callback) {
userMessageMap.set(call.request.key, {value: call.request.value});
callback(null, {key: 'Key: ' + call.request.key, value: 'Value: ' + call.request.value});
}
function get(call, callback) {
var requestedValue = userMessageMap.get(call.request.key);
callback(null, { key: 'Key: ' + call.request.key, value: 'Value: ' + requestedValue.value})
}
function getAll(call, callback){
callback(null, {userMap: JSON.stringify([...userMessageMap])});
}
/**
* Starts an RPC server that receives requests for the CrService
*/
function main() {
var server = new grpc.Server();
server.addService(grpc_proto.CrService.service, {insert: insert, get: get, getAll: getAll});
server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => {
server.start();
});
}
main();