This repository was archived by the owner on Mar 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver.js
More file actions
137 lines (121 loc) · 3.88 KB
/
server.js
File metadata and controls
137 lines (121 loc) · 3.88 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const grpc = require('grpc');
const fs = require('fs');
const Promise = require('bluebird');
const _ = require('lodash');
const chalk = require('chalk');
const Builder = require('./builder');
const Proxy = require('./proxy');
module.exports = class {
constructor(builder, options) {
const defaultOptions = {
'listen': '0.0.0.0:50051',
'checkClientCert': false,
};
this._validateBuilder(builder);
this._creds = grpc.ServerCredentials.createInsecure();
if (this._hasSslOptions(options)) {
this._validateSslOptions(options);
this._creds = this._getOptionsWithSslCredentials(options);
}
this._isStarted = false;
this._options = defaultOptions;
_.assign(this._options, options);
this._middleware = builder.getMiddleware();
this._errorHandlers = builder.getErrorHandlers();
this._grpcServer = new grpc.Server();
builder.getServices().forEach((serviceDefinition) => {
this._addService(serviceDefinition);
});
}
_addService(serviceDefinition) {
let fileToLoad = serviceDefinition.protoFileFullPath;
if (serviceDefinition.rootProtoPath) {
fileToLoad = {
'root': serviceDefinition.rootProtoPath,
'file': serviceDefinition.protoFilePath,
};
}
const grpcObject = grpc.load(fileToLoad);
const proxy = new Proxy(serviceDefinition, this._middleware, this._errorHandlers);
const service = this._getServiceFromName(grpcObject, serviceDefinition.serviceFullName);
this._grpcServer.addService(service, proxy);
}
_getServiceFromName(grpcObject, serviceFullName) {
let service = grpcObject;
const serviceNameComponents = serviceFullName.split('.');
serviceNameComponents.forEach((component) => {
service = service[component];
});
return service.service;
}
getOptions() {
return this._options;
}
start() {
this._grpcServer.bind(this._options.listen, this._creds);
this._grpcServer.start();
this._isStarted = this._grpcServer.started;
console.log(chalk.green(`Condor GRPC Server is listening at ${this._options.listen}`)); // eslint-disable-line
return this;
}
stop() {
this._validateServerIsRunning();
return new Promise((resolve, reject) => {
this._grpcServer.tryShutdown((error) => {
if (error) {
return reject(error);
}
this._isStarted = false;
resolve();
});
});
}
forceStop() {
this._validateServerIsRunning();
this._grpcServer.forceShutdown();
this._isStarted = false;
}
hasStarted() {
return this._isStarted;
}
_validateBuilder(builder) {
if (!(builder instanceof Builder)) {
throw new Error('Cannot perform operation: Server should be constructed with a builder');
}
if (builder.getServices().length === 0) {
throw new Error('Cannot perform operation: No services have been defined');
}
}
_validateServerIsRunning() {
if (!this.hasStarted()) {
throw new Error('Cannot perform operation: Server is not running');
}
}
_hasSslOptions(options) {
return options && (options.rootCert || options.certChain || options.privateKey);
}
_validateSslOptions(options) {
if (!(options.certChain && options.privateKey)) {
throw new Error('Cannot perform operation: privateKey and certChain' +
' are required when using ssl');
}
}
_getOptionsWithSslCredentials(options) {
let rootCert;
if (options.rootCert) {
rootCert = this._getFileBuffer(options.rootCert);
}
return grpc.ServerCredentials.createSsl(rootCert, [
{
'cert_chain': this._getFileBuffer(options.certChain),
'private_key': this._getFileBuffer(options.privateKey),
},
], options.checkClientCert);
}
_getFileBuffer(path) {
if (!fs.existsSync(path)) {
throw new Error(`Cannot perform operation: File not found: ${path}`);
}
return fs.readFileSync(path);
}
};