This repository was archived by the owner on May 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (54 loc) · 1.59 KB
/
Copy pathserver.js
File metadata and controls
62 lines (54 loc) · 1.59 KB
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
const fs = require("fs");
const path = require("path");
const protoLoader = require("@grpc/proto-loader");
const grpc = require("grpc");
const packageDefinition = protoLoader.loadSync("helloworld.proto");
const hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;
function sayHello(call, callback) {
callback(null, { message: "Hello " + call.request.name });
}
function sayHelloAgain(call, callback) {
callback(null, { message: "Hello again, " + call.request.name });
}
function main() {
var server = new grpc.Server();
server.addService(hello_proto.Greeter.service, {
sayHello: sayHello,
sayHelloAgain: sayHelloAgain
});
const certsDir = path.join(process.cwd(), "server-certs");
if (process.env.NODE_ENV === "production") {
server.bind(
"0.0.0.0:50051",
grpc.ServerCredentials.createSsl({
rootCerts: fs.readFileSync(
path.join(certsDir, "Snazzy_Microservices.crt")
),
keyCertPairs: [
{
privateKey: fs.readFileSync(
path.join(
certsDir,
"server-certs",
"login.services.widgets.inc.key"
)
),
certChain: fs.readFileSync(
path.join(
certsDir,
"server-certs",
"login.services.widgets.inc.crt"
)
)
}
],
checkClientCertificate: true
})
);
} else {
server.bind("0.0.0.0:50051", grpc.ServerCredentials.createInsecure());
}
server.start();
console.log("Server started");
}
main();