-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.js
55 lines (45 loc) · 1.95 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
const http = require('http');
const path = require('path');
const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
const logger = require('../../lib/logger');
const app = require('./app');
const WEB_PORT = process.env.WEB_PORT !== undefined ? process.env.WEB_PORT : null;
const GIFSVC_HOST = process.env.GIFSVC_HOST !== undefined ? process.env.GIFSVC_HOST : null;
const VOTINGSVC_HOST = process.env.VOTINGSVC_HOST !== undefined ? process.env.VOTINGSVC_HOST : null;
const INDEX_BUNDLE = process.env.INDEX_BUNDLE !== undefined ? process.env.INDEX_BUNDLE : null;
const WEBPACK_DEV_SERVER = process.env.WEBPACK_DEV_SERVER !== undefined ? process.env.WEBPACK_DEV_SERVER : null;
if (WEB_PORT && GIFSVC_HOST && VOTINGSVC_HOST) {
Promise.resolve().then(async() => {
let gifClient = createGrpcClient('../../proto/Gif.proto', 'GifService', GIFSVC_HOST);
let votingClient = createGrpcClient('../../proto/Voting.proto', 'VotingService', VOTINGSVC_HOST);
let web = await app.create(WEB_PORT,
WEBPACK_DEV_SERVER,
INDEX_BUNDLE,
await gifClient,
await votingClient);
let server = http.createServer(web);
logger.info(`Starting web server on WEB_PORT=[${WEB_PORT}]`);
server.listen(WEB_PORT);
});
} else {
logger.error(`WEB_PORT (currently [${WEB_PORT}]) GIFSVC_HOST (currently [${GIFSVC_HOST}]) and VOTINGSVC_HOST (currently [${VOTINGSVC_HOST}]) INDEX_BUNDLE (currently [${INDEX_BUNDLE}]) environment variables must me set.`);
process.exit(1);
}
async function createGrpcClient(proto, svc, host) {
const PROTO_PATH = path.join(__dirname, proto);
let descriptor = await protoLoader.load(
PROTO_PATH,
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
let pkg = grpc.loadPackageDefinition(descriptor);
let Service = pkg.nodevoto.v1[svc];
let client = new Service(host, grpc.credentials.createInsecure());
return client;
}