@thzero/library_server 0.18.22 → 0.18.24

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.
Files changed (53) hide show
  1. package/README.md +289 -112
  2. package/boot/index.js +529 -529
  3. package/boot/plugins/admin/index.js +6 -6
  4. package/boot/plugins/admin/news.js +33 -33
  5. package/boot/plugins/admin/users.js +33 -33
  6. package/boot/plugins/api.js +64 -64
  7. package/boot/plugins/apiFront.js +31 -31
  8. package/boot/plugins/index.js +70 -70
  9. package/boot/plugins/news.js +44 -44
  10. package/boot/plugins/users.js +46 -46
  11. package/boot/plugins/usersExtended.js +32 -32
  12. package/constants.js +45 -45
  13. package/data/baseNews.js +42 -42
  14. package/data/baseSettingsUser.js +9 -9
  15. package/data/baseUser.js +28 -28
  16. package/data/index.js +24 -24
  17. package/data/named.js +20 -20
  18. package/errors/tokenExpired.js +7 -7
  19. package/license.md +8 -8
  20. package/openSource.js +66 -66
  21. package/package.json +38 -38
  22. package/repository/index.js +182 -182
  23. package/repository/usageMetrics/devnull.js +16 -16
  24. package/routes/index.js +76 -76
  25. package/service/admin/baseNews.js +45 -45
  26. package/service/admin/index.js +130 -130
  27. package/service/admin/news.js +6 -6
  28. package/service/admin/users.js +107 -107
  29. package/service/baseSecurity.js +183 -183
  30. package/service/baseUser.js +122 -122
  31. package/service/communication.js +6 -6
  32. package/service/config.js +37 -37
  33. package/service/crypto.js +16 -16
  34. package/service/discovery/index.js +6 -6
  35. package/service/discovery/resources/index.js +101 -101
  36. package/service/external.js +19 -19
  37. package/service/externalRest.js +19 -19
  38. package/service/index.js +20 -20
  39. package/service/monitoring.js +12 -12
  40. package/service/news/base.js +59 -59
  41. package/service/news/index.js +6 -6
  42. package/service/news/validation/index.js +6 -6
  43. package/service/plans.js +31 -31
  44. package/service/restCommunication.js +21 -21
  45. package/service/usageMetrics.js +84 -84
  46. package/service/utility.js +190 -190
  47. package/service/version.js +37 -37
  48. package/utility/injector.js +59 -59
  49. package/utility/internalIp/index.js +48 -48
  50. package/utility/list/doubleLinked.js +88 -88
  51. package/utility/list/priorityQueue.js +109 -109
  52. package/utility/list/queue.js +72 -72
  53. package/utility/os.js +20 -20
package/boot/index.js CHANGED
@@ -1,529 +1,529 @@
1
- import { createTerminus } from '@godaddy/terminus';
2
-
3
- import config from 'config';
4
-
5
- import { internalIpV4 } from '@thzero/library_server/utility/internalIp/index.js';
6
-
7
- import LibraryServerConstants from '../constants.js';
8
- import LibraryCommonServiceConstants from '@thzero/library_common_service/constants.js';
9
-
10
- import LibraryCommonUtility from '@thzero/library_common/utility/index.js';
11
- import LibraryMomentUtility from '@thzero/library_common/utility/moment.js';
12
-
13
- import NotImplementedError from '@thzero/library_common/errors/notImplemented.js';
14
-
15
- import nullMonitoringService from '../service/monitoring.js';
16
-
17
- // require('@thzero/library_server/utility/string.cjs');
18
- import String2 from '@thzero/library_common/utility/string.js';
19
-
20
- // String.capitalize = function(word) {
21
- // if (String.isNullOrEmpty(word))
22
- // return word;
23
- // return word[0].toUpperCase() + word.slice(1).toLowerCase();
24
- // };
25
-
26
- // String.isNullOrEmpty = function(value) {
27
- // //return !(typeof value === 'string' && value.length > 0)
28
- // return !value;
29
- // }
30
-
31
- // String.isString = function(value) {
32
- // return (typeof value === "string" || value instanceof String);
33
- // }
34
-
35
- // String.trim = function(value) {
36
- // if (!value || !String.isString(value))
37
- // return value;
38
- // return value.trim();
39
- // }
40
-
41
- for (const funcName in String2) {
42
- String[funcName] = String2[funcName];
43
- }
44
-
45
- import injector from '@thzero/library_common/utility/injector.js';
46
-
47
- import usageMetricsRepository from '../repository/usageMetrics/devnull.js';
48
-
49
- import configService from '../service/config.js';
50
- import loggerService from '@thzero/library_common_service/service/logger.js';
51
- import usageMetricsService from '../service/usageMetrics.js';
52
-
53
- class BootMain {
54
- async start(...args) {
55
- process.on('uncaughtException', function(err) {
56
- console.log('Caught exception', err);
57
- return process.exit(99);
58
- });
59
-
60
- const idGeneratorOverride = this._initIdGenerator();
61
- if (idGeneratorOverride)
62
- LibraryCommonUtility.setIdGenerator(idGeneratorOverride);
63
- const idGeneratorAlphabet = this._initIdGeneratorAlphabet();
64
- if (idGeneratorAlphabet)
65
- LibraryCommonUtility.setIdGeneratorAlphabet(idGeneratorAlphabet);
66
- const idGeneratorLengthLong = this._initIdGeneratorLengthLong();
67
- if (idGeneratorLengthLong)
68
- LibraryCommonUtility.setIdGeneratorLengthLong(idGeneratorLengthLong);
69
- const idGeneratorLengthShort = this._initIdGeneratorLengthShort();
70
- if (idGeneratorLengthShort)
71
- LibraryCommonUtility.setIdGeneratorLengthShort(idGeneratorLengthShort);
72
-
73
- this._injector = injector;
74
-
75
- // https://github.com/lorenwest/node-config/wiki
76
- this._appConfig = new configService(config.get('app'));
77
-
78
- this._repositoriesPost = new Map();
79
- this._servicesPost = new Map();
80
-
81
- const plugins = this._determinePlugins(args);
82
- await await this._initPlugins(plugins);
83
-
84
- // this.ip = this._appConfig.get('ip', null);
85
- // this.loggerServiceI.info2(`config.ip.override: ${this.ip}`);
86
- // this.port = this._appConfig.get('port');
87
- // this.loggerServiceI.info2(`config.port.override: ${this.port}`);
88
- // this.loggerServiceI.info2(`process.env.PORT: ${process.env.PORT}`);
89
- // this.port = process.env.PORT || this.port;
90
- // this.loggerServiceI.info2(`selected.port: ${this.port}`);
91
-
92
- const results = await this._initApp(args, plugins);
93
-
94
- async function onSignal() {
95
- console.log('server is starting cleanup');
96
- this.loggerServiceI.info2('server is starting cleanup');
97
- const cleanupFuncs = [];
98
- this._initCleanup(cleanupFuncs);
99
- this._initCleanupDiscovery(cleanupFuncs);
100
- await Promise.all(cleanupFuncs);
101
- console.log('server is starting cleanup completed');
102
- this.loggerServiceI.info2('server is starting cleanup completed');
103
- }
104
-
105
- async function onShutdown() {
106
- console.log('server is shutting down');
107
- this.loggerServiceI.info2('server is shutting down');
108
- this._initShutdown();
109
- console.log('server is shutting down completed');
110
- this.loggerServiceI.info2('server is shutting down completed');
111
- }
112
-
113
- function healthCheck() {
114
- return Promise.resolve(
115
- // optionally include a resolve value to be included as
116
- // info in the health check response
117
- )
118
- }
119
-
120
- const healthcheckPath = this._appConfig.get('healthcheck.path', LibraryServerConstants.HealthCheck.DefaultPath);
121
- if (!healthcheckPath.startsWith('/'))
122
- healthcheckPath = '/' + healthcheckPath;
123
-
124
- const healthCheckOptions = {
125
- verbatim: true // [optional = false] use object returned from /healthcheck verbatim in response
126
- };
127
- if (healthCheck)
128
- healthCheckOptions[healthcheckPath] = healthCheck;
129
-
130
- const terminusOptions = {
131
- // health check options
132
- // healthChecks: {
133
- // healthcheckPath: healthCheck, // a function returning a promise indicating service health,
134
- // verbatim: true // [optional = false] use object returned from /healthcheck verbatim in response
135
- // },
136
- healthChecks: healthCheckOptions,
137
-
138
- // cleanup options
139
- signals: [ 'SIGINT', 'SIGTERM', 'SIGQUIT' ], // SIGKILL does not work with Linux
140
- onSignal: onSignal.bind(this), // [optional] cleanup function, returning a promise (used to be onSigterm)
141
- onShutdown: onShutdown.bind(this) // [optional] called right before exiting
142
- };
143
-
144
- createTerminus(results.server, terminusOptions);
145
-
146
- // const self = this;
147
- // const listen = async (port, address) => new Promise((resolve, reject) => {
148
- // self._initAppListen(results.app, results.server, address, port, (err) => {
149
- // if (err) {
150
- // reject(err);
151
- // return;
152
- // }
153
-
154
- // resolve();
155
- // });
156
- // });
157
- // await listen(this.port, this.ip);
158
- // this.address = results.server.address() ? results.server.address().address : null;
159
- // if (this.address === '::')
160
- // this.address = await internalIpV4();
161
-
162
- await this._initServer(results.server);
163
-
164
- console.log('----repositories.init.post-------------');
165
-
166
- for (const [key, value] of this._repositoriesPost) {
167
- if (value.initPost) {
168
- console.log(`repositories.init.post - ${key}`);
169
- await value.initPost();
170
- }
171
- }
172
-
173
- console.log('----repositories.init.post.complete----');
174
- console.log();
175
-
176
- console.log('----services.init.post-----------------');
177
-
178
- for (const [key, value] of this._servicesPost) {
179
- if (value.initPost) {
180
- console.log(`services.init.post - ${key}`);
181
- await value.initPost();
182
- }
183
- }
184
-
185
- console.log('----services.init.post.complete--------');
186
- console.log();
187
-
188
- console.log('----services.init.app.post-------------');
189
-
190
- this._initAppPost(results.app, args);
191
-
192
- console.log('----services.init.app.post.complete----');
193
- console.log();
194
-
195
- console.log('----server.startup.config--------------');
196
- this.ip = this._appConfig.get('ip', null);
197
- console.log(`config.ip.override: ${this.ip}`);
198
- this.port = this._appConfig.get('port');
199
- console.log(`config.port.override: ${this.port}`);
200
- console.log(`process.env.PORT: ${process.env.PORT}`);
201
- this.port = process.env.PORT || this.port;
202
- console.log(`selected.port: ${this.port}`);
203
-
204
- const self = this;
205
- const listen = async (port, address) => new Promise((resolve, reject) => {
206
- self._initAppListen(results.app, results.server, address, port, (err) => {
207
- if (err) {
208
- console.log(err);
209
- reject(err);
210
- process.exit(1);
211
- }
212
-
213
- resolve();
214
- });
215
- });
216
- await listen(this.port, this.ip);
217
- this.address = results.server.address() ? results.server.address().address : null;
218
- if (this.address === '::')
219
- this.address = await internalIpV4();
220
-
221
- console.log();
222
- console.log(`Starting HTTP on: ${this.address}:${this.port}`);
223
-
224
- console.log('----server.startup.config.complete-----');
225
- console.log();
226
-
227
- await this._initServerDiscovery();
228
-
229
- await this._initServerStart(injector);
230
- }
231
-
232
- _determinePlugins(args) {
233
- let obj;
234
- const results = [];
235
- for (const plugin of args) {
236
- obj = plugin;
237
- if (!this._isObject(obj))
238
- obj = new plugin();
239
- obj.init(this._appConfig, injector);
240
- results.push(obj);
241
- }
242
- return results;
243
- }
244
-
245
- async _initApp(args, plugins) {
246
- throw new NotImplementedError();
247
- }
248
-
249
- _initAppListen(app, server, address, port, err) {
250
- throw new NotImplementedError();
251
- }
252
-
253
- async _initAppPost(app, args) {
254
- }
255
-
256
- _initIdGenerator() {
257
- return null;
258
- }
259
-
260
- _initIdGeneratorAlphabet() {
261
- return null;
262
- }
263
-
264
- _initIdGeneratorLengthLong() {
265
- return null;
266
- }
267
-
268
- _initIdGeneratorLengthShort() {
269
- return null;
270
- }
271
-
272
- async _initPlugins(plugins) {
273
- try {
274
- injector.addSingleton(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG, this._appConfig);
275
-
276
- this._repositories = new Map();
277
-
278
- console.log();
279
- console.log('----repository.injection-------------------------');
280
- for (const pluginRepository of plugins)
281
- await pluginRepository.initRepositories(this._repositories);
282
-
283
- await this._initRepositories();
284
-
285
- this._injectRepository(LibraryServerConstants.InjectorKeys.REPOSITORY_USAGE_METRIC, this._initRepositoriesUsageMetrics());
286
-
287
- console.log('----repository.injection.complete----------------');
288
- console.log();
289
-
290
- this._services = new Map();
291
-
292
- console.log('----services.injection---------------------------');
293
- this.loggerServiceI = this._initServicesLogger();
294
- this._initServicesLoggers();
295
- this._injectService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER, this.loggerServiceI);
296
-
297
- let monitoringService = this._initServicesMonitoring();
298
- if (!monitoringService)
299
- monitoringService = new nullMonitoringService();
300
- this._injectService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_MONITORING, monitoringService);
301
-
302
- this.usageMetricsServiceI = this._initServicesUsageMetrics();
303
- this._injectService(LibraryServerConstants.InjectorKeys.SERVICE_USAGE_METRIC, this.usageMetricsServiceI);
304
-
305
- this.resourceDiscoveryServiceI = this._initServicesDiscoveryResources();
306
- if (this.resourceDiscoveryServiceI)
307
- this._injectService(LibraryServerConstants.InjectorKeys.SERVICE_DISCOVERY_RESOURCES, this.resourceDiscoveryServiceI);
308
-
309
- this.mdnsDiscoveryServiceI = this._initServicesDiscoveryMdns();
310
- if (this.mdnsDiscoveryServiceI)
311
- this._injectService(LibraryServerConstants.InjectorKeys.SERVICE_DISCOVERY_MDNS, this.mdnsDiscoveryServiceI);
312
-
313
- for (const pluginService of plugins)
314
- await pluginService.initServices(this._services);
315
-
316
- await this._initServices();
317
-
318
- console.log('----services.injection.complete------------------');
319
- console.log();
320
-
321
- console.log('----repositories.injection.init------------------');
322
- for (const [key, value] of this._repositories) {
323
- console.log(`repositories.init - ${key}`);
324
- await value.init(injector);
325
-
326
- this._repositoriesPost.set(key, value);
327
- }
328
-
329
- console.log('----repositories.injection.init.complete---------');
330
- console.log();
331
-
332
- console.log('----services.injection.init----------------------');
333
- for (const [key, value] of this._services) {
334
- console.log(`services.init - ${key}`);
335
- await value.init(injector);
336
-
337
- this._servicesPost.set(key, value);
338
- }
339
-
340
- console.log('----services.injection.init.complete-------------');
341
- console.log();
342
-
343
- this._services = new Map();
344
-
345
- console.log('----services.injection.secondary-----------------');
346
- await this._initServicesSecondary();
347
-
348
- for (const pluginService of plugins)
349
- await pluginService.initServicesSecondary(this._services);
350
-
351
- console.log('----services.injection.secondary.complete--------');
352
- console.log();
353
-
354
- console.log('----services.injection.initsecondary-------------');
355
-
356
- for (const [key, value] of this._services) {
357
- if (value.initialized)
358
- continue;
359
-
360
- console.log(`services.init.secondary - ${key}`);
361
- await value.init(injector);
362
-
363
- this._servicesPost.set(key, value);
364
- }
365
-
366
- console.log('----services.injection.initsecondary.complete----');
367
- console.log();
368
-
369
- LibraryMomentUtility.initDateTime();
370
- }
371
- finally {
372
- this._repositories = null;
373
- this._services = null;
374
- }
375
- }
376
-
377
- _initCleanup(cleanupFuncs) {
378
- // your clean logic, like closing database connections
379
- }
380
-
381
- _initCleanupDiscovery(cleanupFuncs) {
382
- if (this.resourceDiscoveryServiceI)
383
- cleanupFuncs.push(this.resourceDiscoveryServiceI.cleanup());
384
- if (this.mdnsDiscoveryServiceI)
385
- cleanupFuncs.push(this.mdnsDiscoveryServiceI.cleanup());
386
- }
387
-
388
- _initPostAuth(app) {
389
- }
390
-
391
- _initPreAuth(app) {
392
- }
393
-
394
- _initPostRoutes(app) {
395
- }
396
-
397
- _initPreRoutes(app) {
398
- }
399
-
400
- async _initRepositories() {
401
- }
402
-
403
- _initRepositoriesUsageMetrics() {
404
- return new usageMetricsRepository();
405
- }
406
-
407
- _initRoute(route) {
408
- }
409
-
410
- async _initRoutes() {
411
- }
412
-
413
- async _initServices() {
414
- }
415
-
416
- async _initServicesSecondary() {
417
- }
418
-
419
- _initServicesDiscoveryResources() {
420
- return null;
421
- }
422
-
423
- _initServicesDiscoveryMdns() {
424
- return null;
425
- }
426
-
427
- _initServicesLogger() {
428
- return new loggerService();
429
- }
430
-
431
- _initServicesLoggers() {
432
- throw new NotImplementedError();
433
- }
434
-
435
- _initServicesMonitoring() {
436
- return null;
437
- }
438
-
439
- _initServicesUsageMetrics() {
440
- return new usageMetricsService();
441
- }
442
-
443
- async _initServer(serverHttp) {
444
- }
445
-
446
- async _initServerDiscovery() {
447
- if (!this.resourceDiscoveryServiceI && !this.mdnsDiscoveryServiceI)
448
- return;
449
-
450
- const opts = await this._initServerDiscoveryOpts();
451
-
452
- await this._initServerDiscoveryMdns(LibraryCommonUtility.cloneDeep(opts));
453
- await this._initServerDiscoveryResources(LibraryCommonUtility.cloneDeep(opts));
454
- }
455
-
456
- async _initServerDiscoveryMdns(opts) {
457
- if (!this.mdnsDiscoveryServiceI)
458
- return;
459
-
460
- await this.mdnsDiscoveryServiceI.initializeDiscovery(await this._initServerDiscoveryOptsMdns(opts));
461
- }
462
-
463
- async _initServerDiscoveryOpts() {
464
- const dns = this._appConfig.get('dns', null);
465
- const grpc = this._appConfig.get('grpc', null);
466
- const secure = this._appConfig.get('secure', false);
467
-
468
- const opts = {
469
- address: this.address,
470
- port: this.port,
471
- healthCheck: 'healthcheck',
472
- secure: secure ? secure : false,
473
- dns: dns
474
- };
475
-
476
- if (grpc) {
477
- opts.grpc = {
478
- port: grpc ? grpc.port : null,
479
- secure: grpc ? (grpc.secure ? grpc.secure : false) : false
480
- };
481
- }
482
-
483
- return opts;
484
- }
485
-
486
- async _initServerDiscoveryOptsMdns(opts) {
487
- return opts;
488
- }
489
-
490
- async _initServerDiscoveryOptsResources(opts) {
491
- return opts;
492
- }
493
-
494
- async _initServerDiscoveryResources(opts) {
495
- if (!this.resourceDiscoveryServiceI)
496
- return;
497
-
498
- await this.resourceDiscoveryServiceI.initializeDiscovery(await this._initServerDiscoveryOptsResources(opts));
499
- }
500
-
501
- async _initServerStart(injector) {
502
- }
503
-
504
- _injectRepository(key, repository) {
505
- console.log(`repositories.inject - ${key}`);
506
- this._repositories.set(key, repository);
507
- injector.addSingleton(key, repository);
508
- }
509
-
510
- _injectService(key, service) {
511
- console.log(`services.inject - ${key}`);
512
- this._services.set(key, service);
513
- injector.addSingleton(key, service);
514
- }
515
-
516
- _initShutdown() {
517
- }
518
-
519
- _isObject(objValue) {
520
- return objValue && typeof objValue === 'object';
521
- }
522
-
523
- _registerServicesLogger(key, service) {
524
- this._injectService(key, service);
525
- this.loggerServiceI.register(key);
526
- }
527
- }
528
-
529
- export default BootMain;
1
+ import { createTerminus } from '@godaddy/terminus';
2
+
3
+ import config from 'config';
4
+
5
+ import { internalIpV4 } from '@thzero/library_server/utility/internalIp/index.js';
6
+
7
+ import LibraryServerConstants from '../constants.js';
8
+ import LibraryCommonServiceConstants from '@thzero/library_common_service/constants.js';
9
+
10
+ import LibraryCommonUtility from '@thzero/library_common/utility/index.js';
11
+ import LibraryMomentUtility from '@thzero/library_common/utility/moment.js';
12
+
13
+ import NotImplementedError from '@thzero/library_common/errors/notImplemented.js';
14
+
15
+ import nullMonitoringService from '../service/monitoring.js';
16
+
17
+ // require('@thzero/library_server/utility/string.cjs');
18
+ import String2 from '@thzero/library_common/utility/string.js';
19
+
20
+ // String.capitalize = function(word) {
21
+ // if (String.isNullOrEmpty(word))
22
+ // return word;
23
+ // return word[0].toUpperCase() + word.slice(1).toLowerCase();
24
+ // };
25
+
26
+ // String.isNullOrEmpty = function(value) {
27
+ // //return !(typeof value === 'string' && value.length > 0)
28
+ // return !value;
29
+ // }
30
+
31
+ // String.isString = function(value) {
32
+ // return (typeof value === "string" || value instanceof String);
33
+ // }
34
+
35
+ // String.trim = function(value) {
36
+ // if (!value || !String.isString(value))
37
+ // return value;
38
+ // return value.trim();
39
+ // }
40
+
41
+ for (const funcName in String2) {
42
+ String[funcName] = String2[funcName];
43
+ }
44
+
45
+ import injector from '@thzero/library_common/utility/injector.js';
46
+
47
+ import usageMetricsRepository from '../repository/usageMetrics/devnull.js';
48
+
49
+ import configService from '../service/config.js';
50
+ import loggerService from '@thzero/library_common_service/service/logger.js';
51
+ import usageMetricsService from '../service/usageMetrics.js';
52
+
53
+ class BootMain {
54
+ async start(...args) {
55
+ process.on('uncaughtException', function(err) {
56
+ console.log('Caught exception', err);
57
+ return process.exit(99);
58
+ });
59
+
60
+ const idGeneratorOverride = this._initIdGenerator();
61
+ if (idGeneratorOverride)
62
+ LibraryCommonUtility.setIdGenerator(idGeneratorOverride);
63
+ const idGeneratorAlphabet = this._initIdGeneratorAlphabet();
64
+ if (idGeneratorAlphabet)
65
+ LibraryCommonUtility.setIdGeneratorAlphabet(idGeneratorAlphabet);
66
+ const idGeneratorLengthLong = this._initIdGeneratorLengthLong();
67
+ if (idGeneratorLengthLong)
68
+ LibraryCommonUtility.setIdGeneratorLengthLong(idGeneratorLengthLong);
69
+ const idGeneratorLengthShort = this._initIdGeneratorLengthShort();
70
+ if (idGeneratorLengthShort)
71
+ LibraryCommonUtility.setIdGeneratorLengthShort(idGeneratorLengthShort);
72
+
73
+ this._injector = injector;
74
+
75
+ // https://github.com/lorenwest/node-config/wiki
76
+ this._appConfig = new configService(config.get('app'));
77
+
78
+ this._repositoriesPost = new Map();
79
+ this._servicesPost = new Map();
80
+
81
+ const plugins = this._determinePlugins(args);
82
+ await await this._initPlugins(plugins);
83
+
84
+ // this.ip = this._appConfig.get('ip', null);
85
+ // this.loggerServiceI.info2(`config.ip.override: ${this.ip}`);
86
+ // this.port = this._appConfig.get('port');
87
+ // this.loggerServiceI.info2(`config.port.override: ${this.port}`);
88
+ // this.loggerServiceI.info2(`process.env.PORT: ${process.env.PORT}`);
89
+ // this.port = process.env.PORT || this.port;
90
+ // this.loggerServiceI.info2(`selected.port: ${this.port}`);
91
+
92
+ const results = await this._initApp(args, plugins);
93
+
94
+ async function onSignal() {
95
+ console.log('server is starting cleanup');
96
+ this.loggerServiceI.info2('server is starting cleanup');
97
+ const cleanupFuncs = [];
98
+ this._initCleanup(cleanupFuncs);
99
+ this._initCleanupDiscovery(cleanupFuncs);
100
+ await Promise.all(cleanupFuncs);
101
+ console.log('server is starting cleanup completed');
102
+ this.loggerServiceI.info2('server is starting cleanup completed');
103
+ }
104
+
105
+ async function onShutdown() {
106
+ console.log('server is shutting down');
107
+ this.loggerServiceI.info2('server is shutting down');
108
+ this._initShutdown();
109
+ console.log('server is shutting down completed');
110
+ this.loggerServiceI.info2('server is shutting down completed');
111
+ }
112
+
113
+ function healthCheck() {
114
+ return Promise.resolve(
115
+ // optionally include a resolve value to be included as
116
+ // info in the health check response
117
+ )
118
+ }
119
+
120
+ const healthcheckPath = this._appConfig.get('healthcheck.path', LibraryServerConstants.HealthCheck.DefaultPath);
121
+ if (!healthcheckPath.startsWith('/'))
122
+ healthcheckPath = '/' + healthcheckPath;
123
+
124
+ const healthCheckOptions = {
125
+ verbatim: true // [optional = false] use object returned from /healthcheck verbatim in response
126
+ };
127
+ if (healthCheck)
128
+ healthCheckOptions[healthcheckPath] = healthCheck;
129
+
130
+ const terminusOptions = {
131
+ // health check options
132
+ // healthChecks: {
133
+ // healthcheckPath: healthCheck, // a function returning a promise indicating service health,
134
+ // verbatim: true // [optional = false] use object returned from /healthcheck verbatim in response
135
+ // },
136
+ healthChecks: healthCheckOptions,
137
+
138
+ // cleanup options
139
+ signals: [ 'SIGINT', 'SIGTERM', 'SIGQUIT' ], // SIGKILL does not work with Linux
140
+ onSignal: onSignal.bind(this), // [optional] cleanup function, returning a promise (used to be onSigterm)
141
+ onShutdown: onShutdown.bind(this) // [optional] called right before exiting
142
+ };
143
+
144
+ createTerminus(results.server, terminusOptions);
145
+
146
+ // const self = this;
147
+ // const listen = async (port, address) => new Promise((resolve, reject) => {
148
+ // self._initAppListen(results.app, results.server, address, port, (err) => {
149
+ // if (err) {
150
+ // reject(err);
151
+ // return;
152
+ // }
153
+
154
+ // resolve();
155
+ // });
156
+ // });
157
+ // await listen(this.port, this.ip);
158
+ // this.address = results.server.address() ? results.server.address().address : null;
159
+ // if (this.address === '::')
160
+ // this.address = await internalIpV4();
161
+
162
+ await this._initServer(results.server);
163
+
164
+ console.log('----repositories.init.post-------------');
165
+
166
+ for (const [key, value] of this._repositoriesPost) {
167
+ if (value.initPost) {
168
+ console.log(`repositories.init.post - ${key}`);
169
+ await value.initPost();
170
+ }
171
+ }
172
+
173
+ console.log('----repositories.init.post.complete----');
174
+ console.log();
175
+
176
+ console.log('----services.init.post-----------------');
177
+
178
+ for (const [key, value] of this._servicesPost) {
179
+ if (value.initPost) {
180
+ console.log(`services.init.post - ${key}`);
181
+ await value.initPost();
182
+ }
183
+ }
184
+
185
+ console.log('----services.init.post.complete--------');
186
+ console.log();
187
+
188
+ console.log('----services.init.app.post-------------');
189
+
190
+ this._initAppPost(results.app, args);
191
+
192
+ console.log('----services.init.app.post.complete----');
193
+ console.log();
194
+
195
+ console.log('----server.startup.config--------------');
196
+ this.ip = this._appConfig.get('ip', null);
197
+ console.log(`config.ip.override: ${this.ip}`);
198
+ this.port = this._appConfig.get('port');
199
+ console.log(`config.port.override: ${this.port}`);
200
+ console.log(`process.env.PORT: ${process.env.PORT}`);
201
+ this.port = process.env.PORT || this.port;
202
+ console.log(`selected.port: ${this.port}`);
203
+
204
+ const self = this;
205
+ const listen = async (port, address) => new Promise((resolve, reject) => {
206
+ self._initAppListen(results.app, results.server, address, port, (err) => {
207
+ if (err) {
208
+ console.log(err);
209
+ reject(err);
210
+ process.exit(1);
211
+ }
212
+
213
+ resolve();
214
+ });
215
+ });
216
+ await listen(this.port, this.ip);
217
+ this.address = results.server.address() ? results.server.address().address : null;
218
+ if (this.address === '::')
219
+ this.address = await internalIpV4();
220
+
221
+ console.log();
222
+ console.log(`Starting HTTP on: ${this.address}:${this.port}`);
223
+
224
+ console.log('----server.startup.config.complete-----');
225
+ console.log();
226
+
227
+ await this._initServerDiscovery();
228
+
229
+ await this._initServerStart(injector);
230
+ }
231
+
232
+ _determinePlugins(args) {
233
+ let obj;
234
+ const results = [];
235
+ for (const plugin of args) {
236
+ obj = plugin;
237
+ if (!this._isObject(obj))
238
+ obj = new plugin();
239
+ obj.init(this._appConfig, injector);
240
+ results.push(obj);
241
+ }
242
+ return results;
243
+ }
244
+
245
+ async _initApp(args, plugins) {
246
+ throw new NotImplementedError();
247
+ }
248
+
249
+ _initAppListen(app, server, address, port, err) {
250
+ throw new NotImplementedError();
251
+ }
252
+
253
+ async _initAppPost(app, args) {
254
+ }
255
+
256
+ _initIdGenerator() {
257
+ return null;
258
+ }
259
+
260
+ _initIdGeneratorAlphabet() {
261
+ return null;
262
+ }
263
+
264
+ _initIdGeneratorLengthLong() {
265
+ return null;
266
+ }
267
+
268
+ _initIdGeneratorLengthShort() {
269
+ return null;
270
+ }
271
+
272
+ async _initPlugins(plugins) {
273
+ try {
274
+ injector.addSingleton(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG, this._appConfig);
275
+
276
+ this._repositories = new Map();
277
+
278
+ console.log();
279
+ console.log('----repository.injection-------------------------');
280
+ for (const pluginRepository of plugins)
281
+ await pluginRepository.initRepositories(this._repositories);
282
+
283
+ await this._initRepositories();
284
+
285
+ this._injectRepository(LibraryServerConstants.InjectorKeys.REPOSITORY_USAGE_METRIC, this._initRepositoriesUsageMetrics());
286
+
287
+ console.log('----repository.injection.complete----------------');
288
+ console.log();
289
+
290
+ this._services = new Map();
291
+
292
+ console.log('----services.injection---------------------------');
293
+ this.loggerServiceI = this._initServicesLogger();
294
+ this._initServicesLoggers();
295
+ this._injectService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER, this.loggerServiceI);
296
+
297
+ let monitoringService = this._initServicesMonitoring();
298
+ if (!monitoringService)
299
+ monitoringService = new nullMonitoringService();
300
+ this._injectService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_MONITORING, monitoringService);
301
+
302
+ this.usageMetricsServiceI = this._initServicesUsageMetrics();
303
+ this._injectService(LibraryServerConstants.InjectorKeys.SERVICE_USAGE_METRIC, this.usageMetricsServiceI);
304
+
305
+ this.resourceDiscoveryServiceI = this._initServicesDiscoveryResources();
306
+ if (this.resourceDiscoveryServiceI)
307
+ this._injectService(LibraryServerConstants.InjectorKeys.SERVICE_DISCOVERY_RESOURCES, this.resourceDiscoveryServiceI);
308
+
309
+ this.mdnsDiscoveryServiceI = this._initServicesDiscoveryMdns();
310
+ if (this.mdnsDiscoveryServiceI)
311
+ this._injectService(LibraryServerConstants.InjectorKeys.SERVICE_DISCOVERY_MDNS, this.mdnsDiscoveryServiceI);
312
+
313
+ for (const pluginService of plugins)
314
+ await pluginService.initServices(this._services);
315
+
316
+ await this._initServices();
317
+
318
+ console.log('----services.injection.complete------------------');
319
+ console.log();
320
+
321
+ console.log('----repositories.injection.init------------------');
322
+ for (const [key, value] of this._repositories) {
323
+ console.log(`repositories.init - ${key}`);
324
+ await value.init(injector);
325
+
326
+ this._repositoriesPost.set(key, value);
327
+ }
328
+
329
+ console.log('----repositories.injection.init.complete---------');
330
+ console.log();
331
+
332
+ console.log('----services.injection.init----------------------');
333
+ for (const [key, value] of this._services) {
334
+ console.log(`services.init - ${key}`);
335
+ await value.init(injector);
336
+
337
+ this._servicesPost.set(key, value);
338
+ }
339
+
340
+ console.log('----services.injection.init.complete-------------');
341
+ console.log();
342
+
343
+ this._services = new Map();
344
+
345
+ console.log('----services.injection.secondary-----------------');
346
+ await this._initServicesSecondary();
347
+
348
+ for (const pluginService of plugins)
349
+ await pluginService.initServicesSecondary(this._services);
350
+
351
+ console.log('----services.injection.secondary.complete--------');
352
+ console.log();
353
+
354
+ console.log('----services.injection.initsecondary-------------');
355
+
356
+ for (const [key, value] of this._services) {
357
+ if (value.initialized)
358
+ continue;
359
+
360
+ console.log(`services.init.secondary - ${key}`);
361
+ await value.init(injector);
362
+
363
+ this._servicesPost.set(key, value);
364
+ }
365
+
366
+ console.log('----services.injection.initsecondary.complete----');
367
+ console.log();
368
+
369
+ LibraryMomentUtility.initDateTime();
370
+ }
371
+ finally {
372
+ this._repositories = null;
373
+ this._services = null;
374
+ }
375
+ }
376
+
377
+ _initCleanup(cleanupFuncs) {
378
+ // your clean logic, like closing database connections
379
+ }
380
+
381
+ _initCleanupDiscovery(cleanupFuncs) {
382
+ if (this.resourceDiscoveryServiceI)
383
+ cleanupFuncs.push(this.resourceDiscoveryServiceI.cleanup());
384
+ if (this.mdnsDiscoveryServiceI)
385
+ cleanupFuncs.push(this.mdnsDiscoveryServiceI.cleanup());
386
+ }
387
+
388
+ _initPostAuth(app) {
389
+ }
390
+
391
+ _initPreAuth(app) {
392
+ }
393
+
394
+ _initPostRoutes(app) {
395
+ }
396
+
397
+ _initPreRoutes(app) {
398
+ }
399
+
400
+ async _initRepositories() {
401
+ }
402
+
403
+ _initRepositoriesUsageMetrics() {
404
+ return new usageMetricsRepository();
405
+ }
406
+
407
+ _initRoute(route) {
408
+ }
409
+
410
+ async _initRoutes() {
411
+ }
412
+
413
+ async _initServices() {
414
+ }
415
+
416
+ async _initServicesSecondary() {
417
+ }
418
+
419
+ _initServicesDiscoveryResources() {
420
+ return null;
421
+ }
422
+
423
+ _initServicesDiscoveryMdns() {
424
+ return null;
425
+ }
426
+
427
+ _initServicesLogger() {
428
+ return new loggerService();
429
+ }
430
+
431
+ _initServicesLoggers() {
432
+ throw new NotImplementedError();
433
+ }
434
+
435
+ _initServicesMonitoring() {
436
+ return null;
437
+ }
438
+
439
+ _initServicesUsageMetrics() {
440
+ return new usageMetricsService();
441
+ }
442
+
443
+ async _initServer(serverHttp) {
444
+ }
445
+
446
+ async _initServerDiscovery() {
447
+ if (!this.resourceDiscoveryServiceI && !this.mdnsDiscoveryServiceI)
448
+ return;
449
+
450
+ const opts = await this._initServerDiscoveryOpts();
451
+
452
+ await this._initServerDiscoveryMdns(LibraryCommonUtility.cloneDeep(opts));
453
+ await this._initServerDiscoveryResources(LibraryCommonUtility.cloneDeep(opts));
454
+ }
455
+
456
+ async _initServerDiscoveryMdns(opts) {
457
+ if (!this.mdnsDiscoveryServiceI)
458
+ return;
459
+
460
+ await this.mdnsDiscoveryServiceI.initializeDiscovery(await this._initServerDiscoveryOptsMdns(opts));
461
+ }
462
+
463
+ async _initServerDiscoveryOpts() {
464
+ const dns = this._appConfig.get('dns', null);
465
+ const grpc = this._appConfig.get('grpc', null);
466
+ const secure = this._appConfig.get('secure', false);
467
+
468
+ const opts = {
469
+ address: this.address,
470
+ port: this.port,
471
+ healthCheck: 'healthcheck',
472
+ secure: secure ? secure : false,
473
+ dns: dns
474
+ };
475
+
476
+ if (grpc) {
477
+ opts.grpc = {
478
+ port: grpc ? grpc.port : null,
479
+ secure: grpc ? (grpc.secure ? grpc.secure : false) : false
480
+ };
481
+ }
482
+
483
+ return opts;
484
+ }
485
+
486
+ async _initServerDiscoveryOptsMdns(opts) {
487
+ return opts;
488
+ }
489
+
490
+ async _initServerDiscoveryOptsResources(opts) {
491
+ return opts;
492
+ }
493
+
494
+ async _initServerDiscoveryResources(opts) {
495
+ if (!this.resourceDiscoveryServiceI)
496
+ return;
497
+
498
+ await this.resourceDiscoveryServiceI.initializeDiscovery(await this._initServerDiscoveryOptsResources(opts));
499
+ }
500
+
501
+ async _initServerStart(injector) {
502
+ }
503
+
504
+ _injectRepository(key, repository) {
505
+ console.log(`repositories.inject - ${key}`);
506
+ this._repositories.set(key, repository);
507
+ injector.addSingleton(key, repository);
508
+ }
509
+
510
+ _injectService(key, service) {
511
+ console.log(`services.inject - ${key}`);
512
+ this._services.set(key, service);
513
+ injector.addSingleton(key, service);
514
+ }
515
+
516
+ _initShutdown() {
517
+ }
518
+
519
+ _isObject(objValue) {
520
+ return objValue && typeof objValue === 'object';
521
+ }
522
+
523
+ _registerServicesLogger(key, service) {
524
+ this._injectService(key, service);
525
+ this.loggerServiceI.register(key);
526
+ }
527
+ }
528
+
529
+ export default BootMain;