-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
41 lines (31 loc) · 1.04 KB
/
server.ts
File metadata and controls
41 lines (31 loc) · 1.04 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
import path from 'path';
import * as grpc from '@grpc/grpc-js';
import * as protoloader from '@grpc/proto-loader';
import { ProtoGrpcType } from './proto/random';
import { RandomHandlers } from './proto/randomPackage/Random';
const Port = 8082;
const PROTO_FILE = './proto/random.proto';
const packageDef = protoloader.loadSync(path.resolve(__dirname, PROTO_FILE))
const grpcObj = (grpc.loadPackageDefinition(packageDef) as unknown) as ProtoGrpcType
const randomPackage = grpcObj.randomPackage
function main() {
const server = getServer();
server.bindAsync(`0.0.0.0:${Port}`, grpc.ServerCredentials.createInsecure(), (err, port) => {
if(err) {
console.error(err);
return
}
console.log(`Server has started on port ${port}`);
server.start()
})
}
function getServer() {
const server = new grpc.Server();
server.addService(randomPackage.Random.service, {
pingPong: (req, res) => {
console.log(req, res)
}
} as RandomHandlers)
return server;
}
main()