@thzero/library_server 0.18.7 → 0.18.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.
Files changed (53) hide show
  1. package/README.md +93 -93
  2. package/boot/index.js +475 -466
  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 +188 -188
  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,466 +1,475 @@
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._servicesPost = new Map();
79
-
80
- const plugins = this._determinePlugins(args);
81
- await await this._initPlugins(plugins);
82
-
83
- // this.ip = this._appConfig.get('ip', null);
84
- // this.loggerServiceI.info2(`config.ip.override: ${this.ip}`);
85
- // this.port = this._appConfig.get('port');
86
- // this.loggerServiceI.info2(`config.port.override: ${this.port}`);
87
- // this.loggerServiceI.info2(`process.env.PORT: ${process.env.PORT}`);
88
- // this.port = process.env.PORT || this.port;
89
- // this.loggerServiceI.info2(`selected.port: ${this.port}`);
90
-
91
- const results = await this._initApp(args, plugins);
92
-
93
- function onSignal() {
94
- this.loggerServiceI.info2(`server is starting cleanup`);
95
- const cleanupFuncs = [];
96
- this._initCleanup(cleanupFuncs);
97
- this._initCleanupDiscovery(cleanupFuncs);
98
- return Promise.all(cleanupFuncs);
99
- }
100
-
101
- function onShutdown() {
102
- this._initShutdown();
103
- this.loggerServiceI.info2(`cleanup finished, server is shutting down`);
104
- }
105
-
106
- function healthCheck() {
107
- return Promise.resolve(
108
- // optionally include a resolve value to be included as
109
- // info in the health check response
110
- )
111
- }
112
-
113
- const healthcheckPath = this._appConfig.get('healthcheck.path', LibraryServerConstants.HealthCheck.DefaultPath);
114
- if (!healthcheckPath.startsWith('/'))
115
- healthcheckPath = '/' + healthcheckPath;
116
-
117
- const healthCheckOptions = {
118
- verbatim: true // [optional = false] use object returned from /healthcheck verbatim in response
119
- };
120
- if (healthCheck)
121
- healthCheckOptions[healthcheckPath] = healthCheck;
122
-
123
- const terminusOptions = {
124
- // health check options
125
- // healthChecks: {
126
- // healthcheckPath: healthCheck, // a function returning a promise indicating service health,
127
- // verbatim: true // [optional = false] use object returned from /healthcheck verbatim in response
128
- // },
129
- healthChecks: healthCheckOptions,
130
-
131
- // cleanup options
132
- signals: [ 'SIGINT', 'SIGTERM' ],
133
- onSignal: onSignal.bind(this), // [optional] cleanup function, returning a promise (used to be onSigterm)
134
- onShutdown: onShutdown.bind(this) // [optional] called right before exiting
135
- };
136
-
137
- createTerminus(results.server, terminusOptions);
138
-
139
- // const self = this;
140
- // const listen = async (port, address) => new Promise((resolve, reject) => {
141
- // self._initAppListen(results.app, results.server, address, port, (err) => {
142
- // if (err) {
143
- // reject(err);
144
- // return;
145
- // }
146
-
147
- // resolve();
148
- // });
149
- // });
150
- // await listen(this.port, this.ip);
151
- // this.address = results.server.address() ? results.server.address().address : null;
152
- // if (this.address === '::')
153
- // this.address = await internalIpV4();
154
-
155
- await this._initServer(results.server);
156
-
157
- for (const [key, value] of this._servicesPost) {
158
- console.log(`services.init.post - ${key}`);
159
- if (value.initPost)
160
- await value.initPost();
161
- }
162
- this._initAppPost(results.app, args);
163
-
164
- console.log();
165
- this.ip = this._appConfig.get('ip', null);
166
- console.log(`config.ip.override: ${this.ip}`);
167
- this.port = this._appConfig.get('port');
168
- console.log(`config.port.override: ${this.port}`);
169
- console.log(`process.env.PORT: ${process.env.PORT}`);
170
- this.port = process.env.PORT || this.port;
171
- console.log(`selected.port: ${this.port}`);
172
- console.log();
173
-
174
- const self = this;
175
- const listen = async (port, address) => new Promise((resolve, reject) => {
176
- self._initAppListen(results.app, results.server, address, port, (err) => {
177
- if (err) {
178
- reject(err);
179
- return;
180
- }
181
-
182
- resolve();
183
- });
184
- });
185
- await listen(this.port, this.ip);
186
- this.address = results.server.address() ? results.server.address().address : null;
187
- if (this.address === '::')
188
- this.address = await internalIpV4();
189
-
190
- console.log();
191
- console.log(`Starting HTTP on: ${this.address}:${this.port}`);
192
-
193
- await this._initServerDiscovery();
194
-
195
- await this._initServerStart(injector);
196
- }
197
-
198
- _determinePlugins(args) {
199
- let obj;
200
- const results = [];
201
- for (const plugin of args) {
202
- obj = plugin;
203
- if (!this._isObject(obj))
204
- obj = new plugin();
205
- obj.init(this._appConfig, injector);
206
- results.push(obj);
207
- }
208
- return results;
209
- }
210
-
211
- async _initApp(args, plugins) {
212
- throw new NotImplementedError();
213
- }
214
-
215
- _initAppListen(app, server, address, port, err) {
216
- throw new NotImplementedError();
217
- }
218
-
219
- async _initAppPost(app, args) {
220
- }
221
-
222
- _initIdGenerator() {
223
- return null;
224
- }
225
-
226
- _initIdGeneratorAlphabet() {
227
- return null;
228
- }
229
-
230
- _initIdGeneratorLengthLong() {
231
- return null;
232
- }
233
-
234
- _initIdGeneratorLengthShort() {
235
- return null;
236
- }
237
-
238
- async _initPlugins(plugins) {
239
- try {
240
- injector.addSingleton(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG, this._appConfig);
241
-
242
- this._repositories = new Map();
243
-
244
- for (const pluginRepository of plugins)
245
- await pluginRepository.initRepositories(this._repositories);
246
-
247
- await this._initRepositories();
248
- this._injectRepository(LibraryServerConstants.InjectorKeys.REPOSITORY_USAGE_METRIC, this._initRepositoriesUsageMetrics());
249
-
250
- this._services = new Map();
251
-
252
- this.loggerServiceI = this._initServicesLogger();
253
- this._initServicesLoggers();
254
- this._injectService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER, this.loggerServiceI);
255
-
256
- let monitoringService = this._initServicesMonitoring();
257
- if (!monitoringService)
258
- monitoringService = new nullMonitoringService();
259
- this._injectService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_MONITORING, monitoringService);
260
-
261
- this.usageMetricsServiceI = this._initServicesUsageMetrics();
262
- this._injectService(LibraryServerConstants.InjectorKeys.SERVICE_USAGE_METRIC, this.usageMetricsServiceI);
263
-
264
- this.resourceDiscoveryServiceI = this._initServicesDiscoveryResources();
265
- if (this.resourceDiscoveryServiceI)
266
- this._injectService(LibraryServerConstants.InjectorKeys.SERVICE_DISCOVERY_RESOURCES, this.resourceDiscoveryServiceI);
267
-
268
- this.mdnsDiscoveryServiceI = this._initServicesDiscoveryMdns();
269
- if (this.mdnsDiscoveryServiceI)
270
- this._injectService(LibraryServerConstants.InjectorKeys.SERVICE_DISCOVERY_MDNS, this.mdnsDiscoveryServiceI);
271
-
272
- for (const pluginService of plugins)
273
- await pluginService.initServices(this._services);
274
-
275
- await this._initServices();
276
-
277
- for (const [key, value] of this._repositories) {
278
- console.log(`repositories.init - ${key}`);
279
- await value.init(injector);
280
- }
281
-
282
- for (const [key, value] of this._services) {
283
- console.log(`services.init - ${key}`);
284
- await value.init(injector);
285
-
286
- this._servicesPost.set(key, value);
287
- }
288
-
289
- this._services = new Map();
290
-
291
- await this._initServicesSecondary();
292
-
293
- for (const pluginService of plugins)
294
- await pluginService.initServicesSecondary(this._services);
295
-
296
- for (const [key, value] of this._services) {
297
- if (value.initialized)
298
- continue;
299
-
300
- console.log(`services.init.secondary - ${key}`);
301
- await value.init(injector);
302
-
303
- this._servicesPost.set(key, value);
304
- }
305
-
306
- LibraryMomentUtility.initDateTime();
307
- }
308
- finally {
309
- this._repositories = null;
310
- this._services = null;
311
- }
312
- }
313
-
314
- _initCleanup(cleanupFuncs) {
315
- // your clean logic, like closing database connections
316
- }
317
-
318
- _initCleanupDiscovery(cleanupFuncs) {
319
- if (this.resourceDiscoveryServiceI)
320
- cleanupFuncs.push(this.resourceDiscoveryServiceI.cleanup());
321
- if (this.mdnsDiscoveryServiceI)
322
- cleanupFuncs.push(this.mdnsDiscoveryServiceI.cleanup());
323
- }
324
-
325
- _initPostAuth(app) {
326
- }
327
-
328
- _initPreAuth(app) {
329
- }
330
-
331
- _initPostRoutes(app) {
332
- }
333
-
334
- _initPreRoutes(app) {
335
- }
336
-
337
- async _initRepositories() {
338
- }
339
-
340
- _initRepositoriesUsageMetrics() {
341
- return new usageMetricsRepository();
342
- }
343
-
344
- _initRoute(route) {
345
- }
346
-
347
- async _initRoutes() {
348
- }
349
-
350
- async _initServices() {
351
- }
352
-
353
- async _initServicesSecondary() {
354
- }
355
-
356
- _initServicesDiscoveryResources() {
357
- return null;
358
- }
359
-
360
- _initServicesDiscoveryMdns() {
361
- return null;
362
- }
363
-
364
- _initServicesLogger() {
365
- return new loggerService();
366
- }
367
-
368
- _initServicesLoggers() {
369
- throw new NotImplementedError();
370
- }
371
-
372
- _initServicesMonitoring() {
373
- return null;
374
- }
375
-
376
- _initServicesUsageMetrics() {
377
- return new usageMetricsService();
378
- }
379
-
380
- async _initServer(serverHttp) {
381
- }
382
-
383
- async _initServerDiscovery() {
384
- if (!this.resourceDiscoveryServiceI && !this.mdnsDiscoveryServiceI)
385
- return;
386
-
387
- const opts = await this._initServerDiscoveryOpts();
388
-
389
- await this._initServerDiscoveryMdns(LibraryCommonUtility.cloneDeep(opts));
390
- await this._initServerDiscoveryResources(LibraryCommonUtility.cloneDeep(opts));
391
- }
392
-
393
- async _initServerDiscoveryMdns(opts) {
394
- if (!this.mdnsDiscoveryServiceI)
395
- return;
396
-
397
- await this.mdnsDiscoveryServiceI.initialize(LibraryCommonUtility.correlationId(), await this._initServerDiscoveryOptsMdns(opts));
398
- }
399
-
400
- async _initServerDiscoveryOpts() {
401
- const dns = this._appConfig.get('dns', null);
402
- const grpc = this._appConfig.get('grpc', null);
403
- const secure = this._appConfig.get('secure', false);
404
-
405
- const opts = {
406
- address: this.address,
407
- port: this.port,
408
- healthCheck: 'healthcheck',
409
- secure: secure ? secure : false,
410
- dns: dns
411
- };
412
-
413
- if (grpc) {
414
- opts.grpc = {
415
- port: grpc ? grpc.port : null,
416
- secure: grpc ? (grpc.secure ? grpc.secure : false) : false
417
- };
418
- }
419
-
420
- return opts;
421
- }
422
-
423
- async _initServerDiscoveryOptsMdns(opts) {
424
- return opts;
425
- }
426
-
427
- async _initServerDiscoveryOptsResources(opts) {
428
- return opts;
429
- }
430
-
431
- async _initServerDiscoveryResources(opts) {
432
- if (!this.resourceDiscoveryServiceI)
433
- return;
434
-
435
- await this.resourceDiscoveryServiceI.initialize(LibraryCommonUtility.correlationId(), await this._initServerDiscoveryOptsResources(opts));
436
- }
437
-
438
- async _initServerStart() {
439
- }
440
-
441
- _injectRepository(key, repository) {
442
- console.log(`repositories.inject - ${key}`);
443
- this._repositories.set(key, repository);
444
- injector.addSingleton(key, repository);
445
- }
446
-
447
- _injectService(key, service) {
448
- console.log(`services.inject - ${key}`);
449
- this._services.set(key, service);
450
- injector.addSingleton(key, service);
451
- }
452
-
453
- _initShutdown() {
454
- }
455
-
456
- _isObject(objValue) {
457
- return objValue && typeof objValue === 'object';
458
- }
459
-
460
- _registerServicesLogger(key, service) {
461
- this._injectService(key, service);
462
- this.loggerServiceI.register(key);
463
- }
464
- }
465
-
466
- 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._servicesPost = new Map();
79
+
80
+ const plugins = this._determinePlugins(args);
81
+ await await this._initPlugins(plugins);
82
+
83
+ // this.ip = this._appConfig.get('ip', null);
84
+ // this.loggerServiceI.info2(`config.ip.override: ${this.ip}`);
85
+ // this.port = this._appConfig.get('port');
86
+ // this.loggerServiceI.info2(`config.port.override: ${this.port}`);
87
+ // this.loggerServiceI.info2(`process.env.PORT: ${process.env.PORT}`);
88
+ // this.port = process.env.PORT || this.port;
89
+ // this.loggerServiceI.info2(`selected.port: ${this.port}`);
90
+
91
+ const results = await this._initApp(args, plugins);
92
+
93
+ function onSignal() {
94
+ this.loggerServiceI.info2(`server is starting cleanup`);
95
+ const cleanupFuncs = [];
96
+ this._initCleanup(cleanupFuncs);
97
+ this._initCleanupDiscovery(cleanupFuncs);
98
+ return Promise.all(cleanupFuncs);
99
+ }
100
+
101
+ function onShutdown() {
102
+ this._initShutdown();
103
+ this.loggerServiceI.info2(`cleanup finished, server is shutting down`);
104
+ }
105
+
106
+ function healthCheck() {
107
+ return Promise.resolve(
108
+ // optionally include a resolve value to be included as
109
+ // info in the health check response
110
+ )
111
+ }
112
+
113
+ const healthcheckPath = this._appConfig.get('healthcheck.path', LibraryServerConstants.HealthCheck.DefaultPath);
114
+ if (!healthcheckPath.startsWith('/'))
115
+ healthcheckPath = '/' + healthcheckPath;
116
+
117
+ const healthCheckOptions = {
118
+ verbatim: true // [optional = false] use object returned from /healthcheck verbatim in response
119
+ };
120
+ if (healthCheck)
121
+ healthCheckOptions[healthcheckPath] = healthCheck;
122
+
123
+ const terminusOptions = {
124
+ // health check options
125
+ // healthChecks: {
126
+ // healthcheckPath: healthCheck, // a function returning a promise indicating service health,
127
+ // verbatim: true // [optional = false] use object returned from /healthcheck verbatim in response
128
+ // },
129
+ healthChecks: healthCheckOptions,
130
+
131
+ // cleanup options
132
+ signals: [ 'SIGINT', 'SIGTERM' ],
133
+ onSignal: onSignal.bind(this), // [optional] cleanup function, returning a promise (used to be onSigterm)
134
+ onShutdown: onShutdown.bind(this) // [optional] called right before exiting
135
+ };
136
+
137
+ createTerminus(results.server, terminusOptions);
138
+
139
+ // const self = this;
140
+ // const listen = async (port, address) => new Promise((resolve, reject) => {
141
+ // self._initAppListen(results.app, results.server, address, port, (err) => {
142
+ // if (err) {
143
+ // reject(err);
144
+ // return;
145
+ // }
146
+
147
+ // resolve();
148
+ // });
149
+ // });
150
+ // await listen(this.port, this.ip);
151
+ // this.address = results.server.address() ? results.server.address().address : null;
152
+ // if (this.address === '::')
153
+ // this.address = await internalIpV4();
154
+
155
+ await this._initServer(results.server);
156
+
157
+ for (const [key, value] of this._servicesPost) {
158
+ console.log(`services.init.post - ${key}`);
159
+ if (value.initPost)
160
+ await value.initPost();
161
+ }
162
+ this._initAppPost(results.app, args);
163
+
164
+ console.log();
165
+ this.ip = this._appConfig.get('ip', null);
166
+ console.log(`config.ip.override: ${this.ip}`);
167
+ this.port = this._appConfig.get('port');
168
+ console.log(`config.port.override: ${this.port}`);
169
+ console.log(`process.env.PORT: ${process.env.PORT}`);
170
+ this.port = process.env.PORT || this.port;
171
+ console.log(`selected.port: ${this.port}`);
172
+ console.log();
173
+
174
+ const self = this;
175
+ const listen = async (port, address) => new Promise((resolve, reject) => {
176
+ self._initAppListen(results.app, results.server, address, port, (err) => {
177
+ if (err) {
178
+ reject(err);
179
+ return;
180
+ }
181
+
182
+ resolve();
183
+ });
184
+ });
185
+ await listen(this.port, this.ip);
186
+ this.address = results.server.address() ? results.server.address().address : null;
187
+ if (this.address === '::')
188
+ this.address = await internalIpV4();
189
+
190
+ console.log();
191
+ console.log(`Starting HTTP on: ${this.address}:${this.port}`);
192
+
193
+ await this._initServerDiscovery();
194
+
195
+ await this._initServerStart(injector);
196
+ }
197
+
198
+ _determinePlugins(args) {
199
+ let obj;
200
+ const results = [];
201
+ for (const plugin of args) {
202
+ obj = plugin;
203
+ if (!this._isObject(obj))
204
+ obj = new plugin();
205
+ obj.init(this._appConfig, injector);
206
+ results.push(obj);
207
+ }
208
+ return results;
209
+ }
210
+
211
+ async _initApp(args, plugins) {
212
+ throw new NotImplementedError();
213
+ }
214
+
215
+ _initAppListen(app, server, address, port, err) {
216
+ throw new NotImplementedError();
217
+ }
218
+
219
+ async _initAppPost(app, args) {
220
+ }
221
+
222
+ _initIdGenerator() {
223
+ return null;
224
+ }
225
+
226
+ _initIdGeneratorAlphabet() {
227
+ return null;
228
+ }
229
+
230
+ _initIdGeneratorLengthLong() {
231
+ return null;
232
+ }
233
+
234
+ _initIdGeneratorLengthShort() {
235
+ return null;
236
+ }
237
+
238
+ async _initPlugins(plugins) {
239
+ try {
240
+ injector.addSingleton(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG, this._appConfig);
241
+
242
+ this._repositories = new Map();
243
+
244
+ for (const pluginRepository of plugins)
245
+ await pluginRepository.initRepositories(this._repositories);
246
+
247
+ await this._initRepositories();
248
+ this._injectRepository(LibraryServerConstants.InjectorKeys.REPOSITORY_USAGE_METRIC, this._initRepositoriesUsageMetrics());
249
+
250
+ this._services = new Map();
251
+
252
+ this.loggerServiceI = this._initServicesLogger();
253
+ this._initServicesLoggers();
254
+ this._injectService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER, this.loggerServiceI);
255
+
256
+ let monitoringService = this._initServicesMonitoring();
257
+ if (!monitoringService)
258
+ monitoringService = new nullMonitoringService();
259
+ this._injectService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_MONITORING, monitoringService);
260
+
261
+ this.usageMetricsServiceI = this._initServicesUsageMetrics();
262
+ this._injectService(LibraryServerConstants.InjectorKeys.SERVICE_USAGE_METRIC, this.usageMetricsServiceI);
263
+
264
+ this.resourceDiscoveryServiceI = this._initServicesDiscoveryResources();
265
+ if (this.resourceDiscoveryServiceI)
266
+ this._injectService(LibraryServerConstants.InjectorKeys.SERVICE_DISCOVERY_RESOURCES, this.resourceDiscoveryServiceI);
267
+
268
+ this.mdnsDiscoveryServiceI = this._initServicesDiscoveryMdns();
269
+ if (this.mdnsDiscoveryServiceI)
270
+ this._injectService(LibraryServerConstants.InjectorKeys.SERVICE_DISCOVERY_MDNS, this.mdnsDiscoveryServiceI);
271
+
272
+ for (const pluginService of plugins)
273
+ await pluginService.initServices(this._services);
274
+
275
+ await this._initServices();
276
+
277
+ for (const [key, value] of this._repositories) {
278
+ console.log(`repositories.init - ${key}`);
279
+ await value.init(injector);
280
+ }
281
+
282
+ for (const [key, value] of this._services) {
283
+ console.log(`services.init - ${key}`);
284
+ await value.init(injector);
285
+
286
+ this._servicesPost.set(key, value);
287
+ }
288
+
289
+ this._services = new Map();
290
+
291
+ await this._initServicesSecondary();
292
+
293
+ for (const pluginService of plugins)
294
+ await pluginService.initServicesSecondary(this._services);
295
+
296
+ for (const [key, value] of this._services) {
297
+ if (value.initialized)
298
+ continue;
299
+
300
+ console.log(`services.init.secondary - ${key}`);
301
+ await value.init(injector);
302
+
303
+ this._servicesPost.set(key, value);
304
+ }
305
+
306
+ LibraryMomentUtility.initDateTime();
307
+ }
308
+ finally {
309
+ this._repositories = null;
310
+ this._services = null;
311
+ }
312
+ }
313
+
314
+ _initCleanup(cleanupFuncs) {
315
+ // your clean logic, like closing database connections
316
+ }
317
+
318
+ _initCleanupDiscovery(cleanupFuncs) {
319
+ if (this.resourceDiscoveryServiceI)
320
+ cleanupFuncs.push(this.resourceDiscoveryServiceI.cleanup());
321
+ if (this.mdnsDiscoveryServiceI)
322
+ cleanupFuncs.push(this.mdnsDiscoveryServiceI.cleanup());
323
+ }
324
+
325
+ _initPostAuth(app) {
326
+ }
327
+
328
+ _initPreAuth(app) {
329
+ }
330
+
331
+ _initPostRoutes(app) {
332
+ }
333
+
334
+ _initPreRoutes(app) {
335
+ }
336
+
337
+ async _initRepositories() {
338
+ }
339
+
340
+ _initRepositoriesUsageMetrics() {
341
+ return new usageMetricsRepository();
342
+ }
343
+
344
+ _initRoute(route) {
345
+ }
346
+
347
+ async _initRoutes() {
348
+ }
349
+
350
+ async _initServices() {
351
+ }
352
+
353
+ async _initServicesSecondary() {
354
+ }
355
+
356
+ _initServicesDiscoveryResources() {
357
+ return null;
358
+ }
359
+
360
+ _initServicesDiscoveryMdns() {
361
+ return null;
362
+ }
363
+
364
+ _initServicesLogger() {
365
+ return new loggerService();
366
+ }
367
+
368
+ _initServicesLoggers() {
369
+ throw new NotImplementedError();
370
+ }
371
+
372
+ _initServicesMonitoring() {
373
+ return null;
374
+ }
375
+
376
+ _initServicesUsageMetrics() {
377
+ return new usageMetricsService();
378
+ }
379
+
380
+ async _initServer(serverHttp) {
381
+ }
382
+
383
+ async _initServerDiscovery() {
384
+ if (!this.resourceDiscoveryServiceI && !this.mdnsDiscoveryServiceI)
385
+ return;
386
+
387
+ const opts = await this._initServerDiscoveryOpts();
388
+
389
+ await this._initServerDiscoveryMdns(LibraryCommonUtility.cloneDeep(opts));
390
+ await this._initServerDiscoveryResources(LibraryCommonUtility.cloneDeep(opts));
391
+ }
392
+
393
+ async _initServerDiscoveryMdns(opts) {
394
+ if (!this.mdnsDiscoveryServiceI)
395
+ return;
396
+
397
+ await this.mdnsDiscoveryServiceI.initialize(LibraryCommonUtility.correlationId(), await this._initServerDiscoveryOptsMdns(opts));
398
+ }
399
+
400
+ async _initServerDiscoveryOpts() {
401
+ const dns = this._appConfig.get('dns', null);
402
+ const grpc = this._appConfig.get('grpc', null);
403
+ const secure = this._appConfig.get('secure', false);
404
+
405
+ const opts = {
406
+ address: this.address,
407
+ port: this.port,
408
+ healthCheck: 'healthcheck',
409
+ secure: secure ? secure : false,
410
+ dns: dns
411
+ };
412
+
413
+ if (grpc) {
414
+ opts.grpc = {
415
+ port: grpc ? grpc.port : null,
416
+ secure: grpc ? (grpc.secure ? grpc.secure : false) : false
417
+ };
418
+ }
419
+
420
+ return opts;
421
+ }
422
+
423
+ async _initServerDiscoveryOptsMdns(opts) {
424
+ return opts;
425
+ }
426
+
427
+ async _initServerDiscoveryOptsResources(opts) {
428
+ return opts;
429
+ }
430
+
431
+ async _initServerDiscoveryResources(opts) {
432
+ if (!this.resourceDiscoveryServiceI)
433
+ return;
434
+
435
+ await this.resourceDiscoveryServiceI.initialize(LibraryCommonUtility.correlationId(), await this._initServerDiscoveryOptsResources(opts));
436
+ }
437
+
438
+ async _initServerStart() {
439
+ for(const service of injector.getServices()) {
440
+ if (service.dependency) {
441
+ console.log(service.key);
442
+ if (service.dependency && service.dependency.instantiate) {
443
+ console.log(`services.instantiate - ${service.key}`);
444
+ await service.dependency.instantiate();
445
+ }
446
+ }
447
+ }
448
+ }
449
+
450
+ _injectRepository(key, repository) {
451
+ console.log(`repositories.inject - ${key}`);
452
+ this._repositories.set(key, repository);
453
+ injector.addSingleton(key, repository);
454
+ }
455
+
456
+ _injectService(key, service) {
457
+ console.log(`services.inject - ${key}`);
458
+ this._services.set(key, service);
459
+ injector.addSingleton(key, service);
460
+ }
461
+
462
+ _initShutdown() {
463
+ }
464
+
465
+ _isObject(objValue) {
466
+ return objValue && typeof objValue === 'object';
467
+ }
468
+
469
+ _registerServicesLogger(key, service) {
470
+ this._injectService(key, service);
471
+ this.loggerServiceI.register(key);
472
+ }
473
+ }
474
+
475
+ export default BootMain;