-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
42 lines (38 loc) · 1.12 KB
/
server.ts
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
import { loadSync } from "@grpc/proto-loader";
import {
loadPackageDefinition,
Server,
ServerCredentials,
} from "@grpc/grpc-js";
import { ProtoGrpcType } from "./proto/hello_world";
import { GreeterServiceHandlers } from "./proto/GreeterService";
const PROTO_PATH = "./hello_world.proto";
const packageDefinition = loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
});
const hello_proto = loadPackageDefinition(
packageDefinition
) as unknown as ProtoGrpcType;
const sayHello: GreeterServiceHandlers["SayHello"] = (call, callback) => {
const { name } = call.request;
callback(null, { message: `Bonjour ${name}` });
};
function startServer() {
const server = new Server();
server.addService(hello_proto.GreeterService.service, { sayHello });
const PORT = 3000;
const host = `localhost:${PORT}`;
server.bindAsync(host, ServerCredentials.createInsecure(), (error) => {
if (error) {
console.log("An error has occurred in bindAsync", error);
return;
}
server.start();
console.log(`Server listening on: ${host}`);
});
}
startServer();