@thzero/library_server 0.14.4 → 0.14.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/boot/index.js +4 -2
- package/package.json +11 -10
- package/utility/internalIp/index.js +49 -0
- package/boot/index.js.bak +0 -362
package/boot/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import internalIp from 'internal-ip';
|
|
1
|
+
// import internalIp from 'internal-ip';
|
|
2
|
+
// Until author fixes the issue with version 7+
|
|
3
|
+
import {internalIpV6, internalIpV4} from '@thzero/library_server/utility/internalIp';
|
|
2
4
|
|
|
3
5
|
import { createTerminus } from '@godaddy/terminus';
|
|
4
6
|
|
|
@@ -105,7 +107,7 @@ class BootMain {
|
|
|
105
107
|
await listen(this.port);
|
|
106
108
|
this.address = serverHttp.address() ? serverHttp.address().address : null;
|
|
107
109
|
if (this.address === '::')
|
|
108
|
-
this.address = await
|
|
110
|
+
this.address = await internalIpV4();
|
|
109
111
|
|
|
110
112
|
await this._initServer(serverHttp);
|
|
111
113
|
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thzero/library_server",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.8",
|
|
4
4
|
"version_major": 0,
|
|
5
5
|
"version_minor": 14,
|
|
6
|
-
"version_patch":
|
|
7
|
-
"version_date": "10/
|
|
6
|
+
"version_patch": 8,
|
|
7
|
+
"version_date": "10/20/2021",
|
|
8
8
|
"description": "An opinionated library of common functionality to bootstrap a Koa based API application using MongoDb and Firebase.",
|
|
9
9
|
"author": "thZero",
|
|
10
10
|
"license": "MIT",
|
|
@@ -32,16 +32,17 @@
|
|
|
32
32
|
"config": "^3.3.6",
|
|
33
33
|
"dayjs": "^1.10.7",
|
|
34
34
|
"dayjs-plugin-utc": "^0.1.2",
|
|
35
|
+
"default-gateway": "^6.0.3",
|
|
35
36
|
"easy-rbac": "^3.1.1",
|
|
36
37
|
"esm": "^3.2.25",
|
|
37
|
-
"
|
|
38
|
-
"koa": "^2.13.
|
|
38
|
+
"ipaddr.js": "^2.0.1",
|
|
39
|
+
"koa": "^2.13.4",
|
|
39
40
|
"koa-body": "^4.2.0",
|
|
40
41
|
"koa-helmet": "^6.1.0",
|
|
41
42
|
"koa-static": "^5.0.0"
|
|
42
43
|
},
|
|
43
44
|
"devDependencies": {
|
|
44
|
-
"@thzero/library_cli": "^0.13.
|
|
45
|
+
"@thzero/library_cli": "^0.13.21"
|
|
45
46
|
},
|
|
46
47
|
"peerDependencies": {
|
|
47
48
|
"@thzero/library_common": "^0.14",
|
|
@@ -50,10 +51,10 @@
|
|
|
50
51
|
"@babel/plugin-proposal-class-properties": "^7.14.5",
|
|
51
52
|
"@babel/preset-env": "^7.15.8",
|
|
52
53
|
"babel-loader": "^8.2.2",
|
|
53
|
-
"eslint": "^8.0.
|
|
54
|
+
"eslint": "^8.0.1",
|
|
54
55
|
"eslint-plugin-node": "^11.1.0",
|
|
55
|
-
"pino-pretty": "^7.0
|
|
56
|
-
"webpack": "^5.
|
|
57
|
-
"webpack-cli": "^4.9.
|
|
56
|
+
"pino-pretty": "^7.1.0",
|
|
57
|
+
"webpack": "^5.59.0",
|
|
58
|
+
"webpack-cli": "^4.9.1"
|
|
58
59
|
}
|
|
59
60
|
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {networkInterfaces} from 'os';
|
|
2
|
+
import defaultGateway from 'default-gateway';
|
|
3
|
+
import ip from 'ipaddr.js';
|
|
4
|
+
|
|
5
|
+
function findIp(gateway) {
|
|
6
|
+
const gatewayIp = ip.parse(gateway);
|
|
7
|
+
|
|
8
|
+
// Look for the matching interface in all local interfaces.
|
|
9
|
+
for (const addresses of Object.values(networkInterfaces())) {
|
|
10
|
+
for (const {cidr} of addresses) {
|
|
11
|
+
const net = ip.parseCIDR(cidr);
|
|
12
|
+
|
|
13
|
+
// eslint-disable-next-line unicorn/prefer-regexp-test
|
|
14
|
+
if (net[0] && net[0].kind() === gatewayIp.kind() && gatewayIp.match(net)) {
|
|
15
|
+
return net[0].toString();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function async(family) {
|
|
22
|
+
try {
|
|
23
|
+
const {gateway} = await defaultGateway[family]();
|
|
24
|
+
return findIp(gateway);
|
|
25
|
+
} catch {}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function sync(family) {
|
|
29
|
+
try {
|
|
30
|
+
const {gateway} = defaultGateway[family].sync();
|
|
31
|
+
return findIp(gateway);
|
|
32
|
+
} catch {}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function internalIpV6() {
|
|
36
|
+
return async('v6');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function internalIpV4() {
|
|
40
|
+
return async('v4');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function internalIpV6Sync() {
|
|
44
|
+
return sync('v6');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function internalIpV4Sync() {
|
|
48
|
+
return sync('v4');
|
|
49
|
+
}
|
package/boot/index.js.bak
DELETED
|
@@ -1,362 +0,0 @@
|
|
|
1
|
-
import internalIp from 'internal-ip';
|
|
2
|
-
|
|
3
|
-
import { createTerminus } from '@godaddy/terminus';
|
|
4
|
-
|
|
5
|
-
import config from 'config';
|
|
6
|
-
|
|
7
|
-
import LibraryConstants from '../constants';
|
|
8
|
-
import LibraryCommonServiceConstants from '@thzero/library_common_service/constants';
|
|
9
|
-
|
|
10
|
-
import Utility from '@thzero/library_common/utility';
|
|
11
|
-
|
|
12
|
-
import NotImplementedError from '@thzero/library_common/errors/notImplemented';
|
|
13
|
-
|
|
14
|
-
require('@thzero/library_common/utility/string');
|
|
15
|
-
import injector from '@thzero/library_common/utility/injector';
|
|
16
|
-
|
|
17
|
-
import usageMetricsRepository from '../repository/usageMetrics/devnull';
|
|
18
|
-
|
|
19
|
-
import configService from '../service/config';
|
|
20
|
-
import loggerService from '@thzero/library_common_service/service/logger';
|
|
21
|
-
import usageMetricsService from '../service/usageMetrics';
|
|
22
|
-
|
|
23
|
-
class BootMain {
|
|
24
|
-
async start(...args) {
|
|
25
|
-
process.on('uncaughtException', function(err) {
|
|
26
|
-
console.log('Caught exception', err);
|
|
27
|
-
return process.exit(99);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
this._injector = injector;
|
|
31
|
-
|
|
32
|
-
// https://github.com/lorenwest/node-config/wiki
|
|
33
|
-
this._appConfig = new configService(config.get('app'));
|
|
34
|
-
|
|
35
|
-
this._servicesPost = new Map();
|
|
36
|
-
|
|
37
|
-
const plugins = this._determinePlugins(args);
|
|
38
|
-
await await this._initPlugins(plugins);
|
|
39
|
-
|
|
40
|
-
const app = await this._initApp(args, plugins);
|
|
41
|
-
|
|
42
|
-
this.port = this._appConfig.get('port');
|
|
43
|
-
this.loggerServiceI.info2(`config.port: ${this.port}`);
|
|
44
|
-
this.loggerServiceI.info2(`process.env.PORT: ${process.env.PORT}`);
|
|
45
|
-
this.port = process.env.PORT || this.port;
|
|
46
|
-
this.loggerServiceI.info2(`selected.port: ${this.port}`);
|
|
47
|
-
const serverHttp = require('http').createServer(app.callback());
|
|
48
|
-
|
|
49
|
-
function onSignal() {
|
|
50
|
-
this.loggerServiceI.info2(`server is starting cleanup`);
|
|
51
|
-
const cleanupFuncs = [];
|
|
52
|
-
this._initCleanup(cleanupFuncs);
|
|
53
|
-
this._initCleanupDiscovery(cleanupFuncs);
|
|
54
|
-
return Promise.all(cleanupFuncs);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function onShutdown() {
|
|
58
|
-
this._initShutdown();
|
|
59
|
-
this.loggerServiceI.info2(`cleanup finished, server is shutting down`);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function healthCheck() {
|
|
63
|
-
return Promise.resolve(
|
|
64
|
-
// optionally include a resolve value to be included as
|
|
65
|
-
// info in the health check response
|
|
66
|
-
)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const healthcheckPath = this._appConfig.get('healthcheck.path', LibraryConstants.HealthCheck.DefaultPath);
|
|
70
|
-
if (!healthcheckPath.startsWith('/'))
|
|
71
|
-
healthcheckPath = '/' + healthcheckPath;
|
|
72
|
-
|
|
73
|
-
const healthCheckOptions = {
|
|
74
|
-
verbatim: true // [optional = false] use object returned from /healthcheck verbatim in response
|
|
75
|
-
};
|
|
76
|
-
if (healthCheck)
|
|
77
|
-
healthCheckOptions[healthcheckPath] = healthCheck;
|
|
78
|
-
|
|
79
|
-
const terminusOptions = {
|
|
80
|
-
// health check options
|
|
81
|
-
// healthChecks: {
|
|
82
|
-
// healthcheckPath: healthCheck, // a function returning a promise indicating service health,
|
|
83
|
-
// verbatim: true // [optional = false] use object returned from /healthcheck verbatim in response
|
|
84
|
-
// },
|
|
85
|
-
healthChecks: healthCheckOptions,
|
|
86
|
-
|
|
87
|
-
// cleanup options
|
|
88
|
-
signals: [ 'SIGINT', 'SIGTERM' ],
|
|
89
|
-
onSignal: onSignal.bind(this), // [optional] cleanup function, returning a promise (used to be onSigterm)
|
|
90
|
-
onShutdown: onShutdown.bind(this) // [optional] called right before exiting
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
createTerminus(serverHttp, terminusOptions);
|
|
94
|
-
|
|
95
|
-
const listen = async (port) => new Promise((resolve, reject) => {
|
|
96
|
-
serverHttp.listen(port, (err) => {
|
|
97
|
-
if (err) {
|
|
98
|
-
reject(err);
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
resolve();
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
await listen(this.port);
|
|
106
|
-
this.address = serverHttp.address() ? serverHttp.address().address : null;
|
|
107
|
-
if (this.address === '::')
|
|
108
|
-
this.address = await internalIp.v4();
|
|
109
|
-
|
|
110
|
-
await this._initServer(serverHttp);
|
|
111
|
-
|
|
112
|
-
for (const [key, value] of this._servicesPost) {
|
|
113
|
-
console.log(`services.init.post - ${key}`);
|
|
114
|
-
if (value.initPost)
|
|
115
|
-
await value.initPost();
|
|
116
|
-
}
|
|
117
|
-
this._initAppPost(app, args);
|
|
118
|
-
|
|
119
|
-
await this._initServerDiscovery(serverHttp);
|
|
120
|
-
|
|
121
|
-
this.loggerServiceI.info2(`Starting HTTP on: `, this.address);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
async _initApp(args, plugins) {
|
|
125
|
-
throw new NotImplementedError();
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
async _initAppPost(app, args) {
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
_determinePlugins(args) {
|
|
132
|
-
let obj;
|
|
133
|
-
const results = [];
|
|
134
|
-
for (const plugin of args) {
|
|
135
|
-
obj = new plugin();
|
|
136
|
-
obj.init(this._appConfig, injector);
|
|
137
|
-
results.push(obj);
|
|
138
|
-
}
|
|
139
|
-
return results;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async _initPlugins(plugins) {
|
|
143
|
-
try {
|
|
144
|
-
injector.addSingleton(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG, this._appConfig);
|
|
145
|
-
|
|
146
|
-
this._repositories = new Map();
|
|
147
|
-
|
|
148
|
-
for (const pluginRepository of plugins)
|
|
149
|
-
await pluginRepository.initRepositories(this._repositories);
|
|
150
|
-
|
|
151
|
-
await this._initRepositories();
|
|
152
|
-
this._injectRepository(LibraryConstants.InjectorKeys.REPOSITORY_USAGE_METRIC, this._initRepositoriesUsageMetrics());
|
|
153
|
-
|
|
154
|
-
this._services = new Map();
|
|
155
|
-
|
|
156
|
-
this.loggerServiceI = this._initServicesLogger();
|
|
157
|
-
this._initServicesLoggers();
|
|
158
|
-
this._injectService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER, this.loggerServiceI);
|
|
159
|
-
|
|
160
|
-
const monitoringService = this._initServicesMonitoring();
|
|
161
|
-
if (monitoringService)
|
|
162
|
-
this._injectService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_MONITORING, monitoringService);
|
|
163
|
-
|
|
164
|
-
this.usageMetricsServiceI = this._initServicesUsageMetrics();
|
|
165
|
-
this._injectService(LibraryConstants.InjectorKeys.SERVICE_USAGE_METRIC, this.usageMetricsServiceI);
|
|
166
|
-
|
|
167
|
-
this.resourceDiscoveryServiceI = this._initServicesDiscoveryResources();
|
|
168
|
-
if (this.resourceDiscoveryServiceI)
|
|
169
|
-
this._injectService(LibraryConstants.InjectorKeys.SERVICE_DISCOVERY_RESOURCES, this.resourceDiscoveryServiceI);
|
|
170
|
-
|
|
171
|
-
this.mdnsDiscoveryServiceI = this._initServicesDiscoveryMdns();
|
|
172
|
-
if (this.mdnsDiscoveryServiceI)
|
|
173
|
-
this._injectService(LibraryConstants.InjectorKeys.SERVICE_DISCOVERY_MDNS, this.mdnsDiscoveryServiceI);
|
|
174
|
-
|
|
175
|
-
for (const pluginService of plugins)
|
|
176
|
-
await pluginService.initServices(this._services);
|
|
177
|
-
|
|
178
|
-
await this._initServices();
|
|
179
|
-
|
|
180
|
-
for (const [key, value] of this._repositories) {
|
|
181
|
-
console.log(`repositories.init - ${key}`);
|
|
182
|
-
await value.init(injector);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
for (const [key, value] of this._services) {
|
|
186
|
-
console.log(`services.init - ${key}`);
|
|
187
|
-
await value.init(injector);
|
|
188
|
-
|
|
189
|
-
this._servicesPost.set(key, value);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
this._services = new Map();
|
|
193
|
-
|
|
194
|
-
await this._initServicesSecondary();
|
|
195
|
-
|
|
196
|
-
for (const pluginService of plugins)
|
|
197
|
-
await pluginService.initServicesSecondary(this._services);
|
|
198
|
-
|
|
199
|
-
for (const [key, value] of this._services) {
|
|
200
|
-
if (value.initialized)
|
|
201
|
-
continue;
|
|
202
|
-
|
|
203
|
-
console.log(`services.init.secondary - ${key}`);
|
|
204
|
-
await value.init(injector);
|
|
205
|
-
|
|
206
|
-
this._servicesPost.set(key, value);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
Utility.initDateTime();
|
|
210
|
-
}
|
|
211
|
-
finally {
|
|
212
|
-
this._repositories = null;
|
|
213
|
-
this._services = null;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
_initCleanup(cleanupFuncs) {
|
|
218
|
-
// your clean logic, like closing database connections
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
_initCleanupDiscovery(cleanupFuncs) {
|
|
222
|
-
if (this.resourceDiscoveryServiceI)
|
|
223
|
-
cleanupFuncs.push(this.resourceDiscoveryServiceI.cleanup());
|
|
224
|
-
if (this.mdnsDiscoveryServiceI)
|
|
225
|
-
cleanupFuncs.push(this.mdnsDiscoveryServiceI.cleanup());
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
_initPostAuth(app) {
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
_initPreAuth(app) {
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
_initPostRoutes(app) {
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
_initPreRoutes(app) {
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
async _initRepositories() {
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
_initRepositoriesUsageMetrics() {
|
|
244
|
-
return new usageMetricsRepository();
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
_initRoute(route) {
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
async _initRoutes() {
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
async _initServices() {
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
async _initServicesSecondary() {
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
_initServicesDiscoveryResources() {
|
|
260
|
-
return null;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
_initServicesDiscoveryMdns() {
|
|
264
|
-
return null;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
_initServicesLogger() {
|
|
268
|
-
return new loggerService();
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
_initServicesLoggers() {
|
|
272
|
-
throw new NotImplementedError();
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
_initServicesMonitoring() {
|
|
276
|
-
return null;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
_initServicesUsageMetrics() {
|
|
280
|
-
return new usageMetricsService();
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
async _initServer(serverHttp) {
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
async _initServerDiscovery(serverHttp) {
|
|
287
|
-
if (!this.resourceDiscoveryServiceI && !this.mdnsDiscoveryServiceI)
|
|
288
|
-
return;
|
|
289
|
-
|
|
290
|
-
const opts = await this._initServerDiscoveryOpts();
|
|
291
|
-
|
|
292
|
-
await this._initServerDiscoveryMdns(Utility.cloneDeep(opts));
|
|
293
|
-
await this._initServerDiscoveryResources(Utility.cloneDeep(opts));
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
async _initServerDiscoveryMdns(opts) {
|
|
297
|
-
if (!this.mdnsDiscoveryServiceI)
|
|
298
|
-
return;
|
|
299
|
-
|
|
300
|
-
await this.mdnsDiscoveryServiceI.initialize(Utility.generateId(), await this._initServerDiscoveryOptsMdns(opts));
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
async _initServerDiscoveryOpts() {
|
|
304
|
-
const dns = this._appConfig.get('dns', null);
|
|
305
|
-
const grpc = this._appConfig.get('grpc', null);
|
|
306
|
-
const secure = this._appConfig.get('secure', false);
|
|
307
|
-
|
|
308
|
-
const opts = {
|
|
309
|
-
address: this.address,
|
|
310
|
-
port: this.port,
|
|
311
|
-
healthCheck: 'healthcheck',
|
|
312
|
-
secure: secure ? secure : false,
|
|
313
|
-
dns: dns
|
|
314
|
-
};
|
|
315
|
-
|
|
316
|
-
if (grpc) {
|
|
317
|
-
opts.grpc = {
|
|
318
|
-
port: grpc ? grpc.port : null,
|
|
319
|
-
secure: grpc ? (grpc.secure ? grpc.secure : false) : false
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
return opts;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
async _initServerDiscoveryOptsMdns(opts) {
|
|
327
|
-
return opts;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
async _initServerDiscoveryOptsResources(opts) {
|
|
331
|
-
return opts;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
async _initServerDiscoveryResources(opts) {
|
|
335
|
-
if (!this.resourceDiscoveryServiceI)
|
|
336
|
-
return;
|
|
337
|
-
|
|
338
|
-
await this.resourceDiscoveryServiceI.initialize(Utility.generateId(), await this._initServerDiscoveryOptsResources(opts));
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
_injectRepository(key, repository) {
|
|
342
|
-
console.log(`repositories.inject - ${key}`);
|
|
343
|
-
this._repositories.set(key, repository);
|
|
344
|
-
injector.addSingleton(key, repository);
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
_injectService(key, service) {
|
|
348
|
-
console.log(`services.inject - ${key}`);
|
|
349
|
-
this._services.set(key, service);
|
|
350
|
-
injector.addSingleton(key, service);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
_initShutdown() {
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
_registerServicesLogger(key, service) {
|
|
357
|
-
this._injectService(key, service);
|
|
358
|
-
this.loggerServiceI.register(key);
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
export default BootMain;
|