@thzero/library_server 0.17.1 → 0.17.3

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