-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapi.js
56 lines (43 loc) · 1.18 KB
/
api.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
const path = require('path');
const protoLoader = require('@grpc/proto-loader');
class GifServiceServer {
constructor(gif) {
this.gif = gif;
}
getByShortcode(shortcode) {
return this.gif.getByShortcode(shortcode);
}
getList() {
return this.gif.getList();
}
mapRPC() {
let implementations = {};
implementations.ListAll = (call, callback) => {
let results = { list: this.getList() };
callback(null, results);
};
implementations.FindByShortcode = (call, callback) => {
let gif = { Gif: this.getByShortcode(call.request.Shortcode) };
callback(null, gif);
};
return implementations;
}
}
module.exports.newGrpcServer = async (grpcServer, gif) => {
const PROTO_PATH = path.join(__dirname + '../../../proto/Gif.proto');
let descriptor = await protoLoader.load(
PROTO_PATH,
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
let gifSvc = descriptor['nodevoto.v1.GifService'];
let gifSrv = new GifServiceServer(gif);
let implementations = gifSrv.mapRPC();
grpcServer.addService(gifSvc, implementations);
return implementations;
};