namirasoft-node 1.4.102 → 1.4.104
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/BaseApplication.d.ts +2 -1
- package/dist/BaseApplication.js +8 -7
- package/dist/BaseApplication.js.map +1 -1
- package/dist/BaseController.d.ts +2 -1
- package/dist/BaseController.js +1 -0
- package/dist/BaseController.js.map +1 -1
- package/dist/BaseFilterItemBuilder.d.ts +1 -0
- package/dist/BaseFilterItemBuilder.js +9 -8
- package/dist/BaseFilterItemBuilder.js.map +1 -1
- package/dist/BaseFilterItemBuilderObject.d.ts +1 -0
- package/dist/BaseFilterItemBuilderObject.js +3 -0
- package/dist/BaseFilterItemBuilderObject.js.map +1 -1
- package/package.json +43 -43
- package/src/AnomalyDetector.ts +84 -84
- package/src/BaseApplication.ts +443 -440
- package/src/BaseApplicationLink.ts +6 -6
- package/src/BaseController.ts +226 -225
- package/src/BaseCron.ts +104 -104
- package/src/BaseDatabase.ts +45 -45
- package/src/BaseEmailService.ts +38 -38
- package/src/BaseFilterItemBuilder.ts +191 -189
- package/src/BaseFilterItemBuilderDatabase.ts +45 -45
- package/src/BaseFilterItemBuilderObject.ts +95 -91
- package/src/BaseTable.ts +137 -137
- package/src/BaseTableColumnOptions.ts +8 -8
- package/src/CommandOperation.ts +32 -32
- package/src/GmailService.ts +22 -22
- package/src/IPOperation.ts +38 -38
- package/src/Meta.ts +36 -36
- package/src/OTPOperation.ts +94 -94
- package/src/RequestHeaderService.ts +27 -27
- package/src/SMTPService.ts +26 -26
- package/src/ServerToServerOperation.ts +23 -23
- package/src/Timer.ts +17 -17
- package/src/Validator.ts +15 -15
- package/src/index.ts +21 -21
package/src/BaseApplication.ts
CHANGED
|
@@ -1,441 +1,444 @@
|
|
|
1
|
-
import express, { Router } from 'express';
|
|
2
|
-
import cors from "cors";
|
|
3
|
-
import * as fs from "fs";
|
|
4
|
-
import * as path from 'path';
|
|
5
|
-
import serveIndex from "serve-index";
|
|
6
|
-
import swaggerUi from 'swagger-ui-express';
|
|
7
|
-
import swaggerJSDoc from 'swagger-jsdoc';
|
|
8
|
-
import { BaseDatabase } from './BaseDatabase';
|
|
9
|
-
import { ILogger } from "namirasoft-log";
|
|
10
|
-
import { BaseApplicationLink } from './BaseApplicationLink';
|
|
11
|
-
import { EnvService, HTTPMethod, ObjectService, PackageService } from 'namirasoft-core';
|
|
12
|
-
import { ApplicationSchema, BaseVariableSchema, ObjectSchema, SwaggerApplicationBuilder } from 'namirasoft-schema';
|
|
13
|
-
import { BaseController } from './BaseController';
|
|
14
|
-
import { BaseCron } from './BaseCron';
|
|
15
|
-
|
|
16
|
-
export abstract class BaseApplication<D extends { [name: string]: BaseDatabase }>
|
|
17
|
-
{
|
|
18
|
-
private linkLoader?: () => Promise<void>;
|
|
19
|
-
private links: BaseApplicationLink[] = [];
|
|
20
|
-
protected name_asset: string;
|
|
21
|
-
protected name_page: string;
|
|
22
|
-
protected base_path: string;
|
|
23
|
-
protected dir_asset: string;
|
|
24
|
-
protected dir_page: string;
|
|
25
|
-
protected dir_swagger: string;
|
|
26
|
-
protected path_asset: string;
|
|
27
|
-
protected path_page: string;
|
|
28
|
-
protected path_swagger: string;
|
|
29
|
-
protected pkg: PackageService;
|
|
30
|
-
protected title: string;
|
|
31
|
-
protected description: string;
|
|
32
|
-
protected logo: string;
|
|
33
|
-
protected version: string;
|
|
34
|
-
protected mode!: string;
|
|
35
|
-
public express!: express.Express;
|
|
36
|
-
public databases!: D;
|
|
37
|
-
public logger: ILogger;
|
|
38
|
-
constructor(base_path: string)
|
|
39
|
-
{
|
|
40
|
-
this.base_path = base_path;
|
|
41
|
-
this.name_asset = "asset";
|
|
42
|
-
this.name_page = "page";
|
|
43
|
-
this.dir_asset = "./" + this.name_asset;
|
|
44
|
-
this.dir_page = "./" + this.name_page;
|
|
45
|
-
this.dir_swagger = "./swagger";
|
|
46
|
-
this.path_asset = base_path + "/" + this.name_asset;
|
|
47
|
-
this.path_page = base_path + "/" + this.name_page;
|
|
48
|
-
this.path_swagger = this.path_page + "/swagger";
|
|
49
|
-
this.pkg = PackageService.getMain();
|
|
50
|
-
this.title = this.pkg?.getTitle() ?? "";
|
|
51
|
-
this.description = this.pkg?.getDescription() ?? "";
|
|
52
|
-
this.logo = this.pkg?.getLogo() ?? "";
|
|
53
|
-
this.version = this.pkg?.getVersion() ?? "";
|
|
54
|
-
this.mode = new EnvService("SERVER_MODE", false).getString("api").toLowerCase();
|
|
55
|
-
this.logger = this.getILogger();
|
|
56
|
-
this.addLink = this.addLink.bind(this);
|
|
57
|
-
this.addLink_Asset = this.addLink_Asset.bind(this);
|
|
58
|
-
this.addLink_Page = this.addLink_Page.bind(this);
|
|
59
|
-
this.addLink_Swagger = this.addLink_Swagger.bind(this);
|
|
60
|
-
|
|
61
|
-
// create folders
|
|
62
|
-
this.dir_asset = path.join(path.dirname(this.pkg.getPath()), this.dir_asset);
|
|
63
|
-
this.dir_page = path.join(path.dirname(this.pkg.getPath()), this.dir_page);
|
|
64
|
-
this.dir_swagger = path.join(path.dirname(this.pkg.getPath()), this.dir_swagger);
|
|
65
|
-
if (!fs.existsSync(this.dir_asset))
|
|
66
|
-
fs.mkdirSync(this.dir_asset);
|
|
67
|
-
if (!fs.existsSync(this.dir_page))
|
|
68
|
-
fs.mkdirSync(this.dir_page);
|
|
69
|
-
if (!fs.existsSync(this.dir_swagger))
|
|
70
|
-
fs.mkdirSync(this.dir_swagger, { recursive: true });
|
|
71
|
-
}
|
|
72
|
-
public abstract getDatabases(): D;
|
|
73
|
-
protected abstract getILogger(): ILogger;
|
|
74
|
-
protected abstract getPort(): number;
|
|
75
|
-
protected abstract getControllers(): any[];
|
|
76
|
-
protected abstract getTables(): any[];
|
|
77
|
-
protected abstract getCrons(): any[];
|
|
78
|
-
protected async foreachController(handler: (generator: (req: express.Request, res: express.Response) => any) => Promise<boolean
|
|
79
|
-
{
|
|
80
|
-
let controllers = this.getControllers();
|
|
81
|
-
for (let i = 0; i < controllers.length; i++)
|
|
82
|
-
{
|
|
83
|
-
const Template = controllers[i] as any;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
await this.
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
await this.
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
{
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
this.express
|
|
194
|
-
//
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
{
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
//
|
|
246
|
-
//
|
|
247
|
-
//
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
{
|
|
283
|
-
this.
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
{
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
{
|
|
299
|
-
this.
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
router.
|
|
353
|
-
else
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
1
|
+
import express, { Router } from 'express';
|
|
2
|
+
import cors from "cors";
|
|
3
|
+
import * as fs from "fs";
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
import serveIndex from "serve-index";
|
|
6
|
+
import swaggerUi from 'swagger-ui-express';
|
|
7
|
+
import swaggerJSDoc from 'swagger-jsdoc';
|
|
8
|
+
import { BaseDatabase } from './BaseDatabase';
|
|
9
|
+
import { ILogger } from "namirasoft-log";
|
|
10
|
+
import { BaseApplicationLink } from './BaseApplicationLink';
|
|
11
|
+
import { EnvService, HTTPMethod, ObjectService, PackageService } from 'namirasoft-core';
|
|
12
|
+
import { ApplicationSchema, BaseVariableSchema, ObjectSchema, SwaggerApplicationBuilder } from 'namirasoft-schema';
|
|
13
|
+
import { BaseController } from './BaseController';
|
|
14
|
+
import { BaseCron } from './BaseCron';
|
|
15
|
+
|
|
16
|
+
export abstract class BaseApplication<D extends { [name: string]: BaseDatabase }>
|
|
17
|
+
{
|
|
18
|
+
private linkLoader?: () => Promise<void>;
|
|
19
|
+
private links: BaseApplicationLink[] = [];
|
|
20
|
+
protected name_asset: string;
|
|
21
|
+
protected name_page: string;
|
|
22
|
+
protected base_path: string;
|
|
23
|
+
protected dir_asset: string;
|
|
24
|
+
protected dir_page: string;
|
|
25
|
+
protected dir_swagger: string;
|
|
26
|
+
protected path_asset: string;
|
|
27
|
+
protected path_page: string;
|
|
28
|
+
protected path_swagger: string;
|
|
29
|
+
protected pkg: PackageService;
|
|
30
|
+
protected title: string;
|
|
31
|
+
protected description: string;
|
|
32
|
+
protected logo: string;
|
|
33
|
+
protected version: string;
|
|
34
|
+
protected mode!: string;
|
|
35
|
+
public express!: express.Express;
|
|
36
|
+
public databases!: D;
|
|
37
|
+
public logger: ILogger;
|
|
38
|
+
constructor(base_path: string)
|
|
39
|
+
{
|
|
40
|
+
this.base_path = base_path;
|
|
41
|
+
this.name_asset = "asset";
|
|
42
|
+
this.name_page = "page";
|
|
43
|
+
this.dir_asset = "./" + this.name_asset;
|
|
44
|
+
this.dir_page = "./" + this.name_page;
|
|
45
|
+
this.dir_swagger = "./swagger";
|
|
46
|
+
this.path_asset = base_path + "/" + this.name_asset;
|
|
47
|
+
this.path_page = base_path + "/" + this.name_page;
|
|
48
|
+
this.path_swagger = this.path_page + "/swagger";
|
|
49
|
+
this.pkg = PackageService.getMain();
|
|
50
|
+
this.title = this.pkg?.getTitle() ?? "";
|
|
51
|
+
this.description = this.pkg?.getDescription() ?? "";
|
|
52
|
+
this.logo = this.pkg?.getLogo() ?? "";
|
|
53
|
+
this.version = this.pkg?.getVersion() ?? "";
|
|
54
|
+
this.mode = new EnvService("SERVER_MODE", false).getString("api").toLowerCase();
|
|
55
|
+
this.logger = this.getILogger();
|
|
56
|
+
this.addLink = this.addLink.bind(this);
|
|
57
|
+
this.addLink_Asset = this.addLink_Asset.bind(this);
|
|
58
|
+
this.addLink_Page = this.addLink_Page.bind(this);
|
|
59
|
+
this.addLink_Swagger = this.addLink_Swagger.bind(this);
|
|
60
|
+
|
|
61
|
+
// create folders
|
|
62
|
+
this.dir_asset = path.join(path.dirname(this.pkg.getPath()), this.dir_asset);
|
|
63
|
+
this.dir_page = path.join(path.dirname(this.pkg.getPath()), this.dir_page);
|
|
64
|
+
this.dir_swagger = path.join(path.dirname(this.pkg.getPath()), this.dir_swagger);
|
|
65
|
+
if (!fs.existsSync(this.dir_asset))
|
|
66
|
+
fs.mkdirSync(this.dir_asset);
|
|
67
|
+
if (!fs.existsSync(this.dir_page))
|
|
68
|
+
fs.mkdirSync(this.dir_page);
|
|
69
|
+
if (!fs.existsSync(this.dir_swagger))
|
|
70
|
+
fs.mkdirSync(this.dir_swagger, { recursive: true });
|
|
71
|
+
}
|
|
72
|
+
public abstract getDatabases(): D;
|
|
73
|
+
protected abstract getILogger(): ILogger;
|
|
74
|
+
protected abstract getPort(): number;
|
|
75
|
+
protected abstract getControllers(): any[];
|
|
76
|
+
protected abstract getTables(): any[];
|
|
77
|
+
protected abstract getCrons(): any[];
|
|
78
|
+
protected async foreachController(handler: (generator: (req: express.Request, res: express.Response) => BaseController<{}, any, any, any>) => Promise<boolean>, modes: string[])
|
|
79
|
+
{
|
|
80
|
+
let controllers = this.getControllers();
|
|
81
|
+
for (let i = 0; i < controllers.length; i++)
|
|
82
|
+
{
|
|
83
|
+
const Template = controllers[i] as any;
|
|
84
|
+
|
|
85
|
+
if (modes.length > 0)
|
|
86
|
+
if (!Template.modes.includes("*"))
|
|
87
|
+
if (!modes.some(mode => Template.modes.includes(mode)))
|
|
88
|
+
return;
|
|
89
|
+
|
|
90
|
+
let con = await handler((req: express.Request, res: express.Response) =>
|
|
91
|
+
{
|
|
92
|
+
if (ObjectService.isClass(Template))
|
|
93
|
+
return new Template(this, req, res);
|
|
94
|
+
return Template(this, req, res);
|
|
95
|
+
});
|
|
96
|
+
if (!con)
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
protected async foreachCron(handler: (generator: () => any) => Promise<boolean>)
|
|
101
|
+
{
|
|
102
|
+
let crons = this.getCrons();
|
|
103
|
+
for (let i = 0; i < crons.length; i++)
|
|
104
|
+
{
|
|
105
|
+
try
|
|
106
|
+
{
|
|
107
|
+
const Template = crons[i] as any;
|
|
108
|
+
let con = await handler(() =>
|
|
109
|
+
{
|
|
110
|
+
if (ObjectService.isClass(Template))
|
|
111
|
+
return new Template(this);
|
|
112
|
+
return Template(this);
|
|
113
|
+
});
|
|
114
|
+
if (!con)
|
|
115
|
+
break;
|
|
116
|
+
} catch (error)
|
|
117
|
+
{
|
|
118
|
+
this.logger.onCatchCritical(error);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
// start
|
|
123
|
+
async start()
|
|
124
|
+
{
|
|
125
|
+
this.logger.info(`Application was started in mode '${this.mode}'.`);
|
|
126
|
+
await this.startCrashHandler();
|
|
127
|
+
await this.startDatabase();
|
|
128
|
+
if (!process.env.NAMIRASOFT_MUTE)
|
|
129
|
+
{
|
|
130
|
+
await this.onBeforeStart();
|
|
131
|
+
await this.startExpress();
|
|
132
|
+
if (this.mode == "api")
|
|
133
|
+
{
|
|
134
|
+
await this.createSwagger();
|
|
135
|
+
await this.startSwagger();
|
|
136
|
+
await this.startHomePage();
|
|
137
|
+
await this.createAssets();
|
|
138
|
+
await this.startAssets();
|
|
139
|
+
await this.createPages();
|
|
140
|
+
await this.startPages();
|
|
141
|
+
}
|
|
142
|
+
await this.startCrons();
|
|
143
|
+
await this.startOthers();
|
|
144
|
+
await this.onAfterStart();
|
|
145
|
+
}
|
|
146
|
+
else
|
|
147
|
+
{
|
|
148
|
+
await this.saveSchema();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
protected async startCrashHandler(): Promise<void>
|
|
152
|
+
{
|
|
153
|
+
if (process.env.NAMIRASOFT_MUTE)
|
|
154
|
+
return;
|
|
155
|
+
|
|
156
|
+
process.on('unhandledRejection', (reason) =>
|
|
157
|
+
{
|
|
158
|
+
if (reason instanceof Error)
|
|
159
|
+
this.logger.onCatchCritical(reason);
|
|
160
|
+
else
|
|
161
|
+
this.logger.critical(reason + "");
|
|
162
|
+
});
|
|
163
|
+
process.on('uncaughtException', (error) =>
|
|
164
|
+
{
|
|
165
|
+
this.logger.onCatchFatal(error);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
public async foreachDatabase(handler: (d: BaseDatabase) => Promise<void>): Promise<void>
|
|
169
|
+
{
|
|
170
|
+
let names = Object.keys(this.databases);
|
|
171
|
+
for (let name of names)
|
|
172
|
+
await handler(this.databases[name]);
|
|
173
|
+
}
|
|
174
|
+
protected async startDatabase(): Promise<void>
|
|
175
|
+
{
|
|
176
|
+
this.databases = this.getDatabases();
|
|
177
|
+
await this.foreachDatabase(async database =>
|
|
178
|
+
{
|
|
179
|
+
await database.init();
|
|
180
|
+
await database.connect();
|
|
181
|
+
await database.sync(false);
|
|
182
|
+
if (!process.env.NAMIRASOFT_MUTE)
|
|
183
|
+
await database.seedBefore();
|
|
184
|
+
});
|
|
185
|
+
this.logger.success("Databases were connected.");
|
|
186
|
+
}
|
|
187
|
+
protected async onBeforeStart(): Promise<void>
|
|
188
|
+
{ }
|
|
189
|
+
protected async startExpress(): Promise<void>
|
|
190
|
+
{
|
|
191
|
+
if (!process.env.NAMIRASOFT_MUTE)
|
|
192
|
+
{
|
|
193
|
+
this.express = express();
|
|
194
|
+
// Express
|
|
195
|
+
this.express.use(express.static('static'));
|
|
196
|
+
// Cors
|
|
197
|
+
this.express.use(cors({ exposedHeaders: '*' }));
|
|
198
|
+
// api routes
|
|
199
|
+
this.express.use('/', await this.getRouter());
|
|
200
|
+
// start server
|
|
201
|
+
const port = this.getPort();
|
|
202
|
+
this.express.listen(port, async () =>
|
|
203
|
+
{
|
|
204
|
+
this.logger.success(`Server is listening on port ${port}`);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
protected async startCrons(): Promise<void>
|
|
209
|
+
{
|
|
210
|
+
if (!process.env.NAMIRASOFT_MUTE)
|
|
211
|
+
{
|
|
212
|
+
this.foreachCron(async generator =>
|
|
213
|
+
{
|
|
214
|
+
const template = generator() as BaseCron<D>;
|
|
215
|
+
template.run();
|
|
216
|
+
return true;
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
protected async createSwagger(): Promise<void>
|
|
221
|
+
{
|
|
222
|
+
let schema = await this.getSchema<BaseController<D, {}, {}, {}>>(c => c.generate_swagger);
|
|
223
|
+
let builder = new SwaggerApplicationBuilder(schema);
|
|
224
|
+
let result = builder.runSynch();
|
|
225
|
+
for (let name of Object.keys(result.controllers))
|
|
226
|
+
{
|
|
227
|
+
const lines = result.controllers[name];
|
|
228
|
+
fs.writeFileSync(this.dir_swagger + "/controller_" + name + ".swg", lines.join("\n"));
|
|
229
|
+
}
|
|
230
|
+
for (let name of Object.keys(result.classes))
|
|
231
|
+
{
|
|
232
|
+
const lines = result.classes[name];
|
|
233
|
+
fs.writeFileSync(this.dir_swagger + "/model_" + name + ".swg", lines.join("\n"));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
protected async startSwagger(): Promise<void>
|
|
237
|
+
{
|
|
238
|
+
const joptions = {
|
|
239
|
+
definition: {
|
|
240
|
+
openapi: "3.0.1",
|
|
241
|
+
info: {
|
|
242
|
+
title: this.title,
|
|
243
|
+
description: this.description,
|
|
244
|
+
version: this.version,
|
|
245
|
+
// license: {
|
|
246
|
+
// name: "MIT",
|
|
247
|
+
// url: "https://spdx.org/licenses/MIT.html",
|
|
248
|
+
// },
|
|
249
|
+
// contact: {
|
|
250
|
+
// name: "Amir Abolhasani",
|
|
251
|
+
// url: "https://namirasoft.com",
|
|
252
|
+
// email: "accounts@namirasoft.com",
|
|
253
|
+
// },
|
|
254
|
+
},
|
|
255
|
+
servers: [
|
|
256
|
+
{
|
|
257
|
+
url: this.base_path,
|
|
258
|
+
},
|
|
259
|
+
],
|
|
260
|
+
},
|
|
261
|
+
apis: [this.dir_swagger + '/*.swg'],
|
|
262
|
+
};
|
|
263
|
+
const swaggerSpec = swaggerJSDoc(joptions);
|
|
264
|
+
var options = {
|
|
265
|
+
explorer: true
|
|
266
|
+
};
|
|
267
|
+
this.express.use(this.path_swagger + "/", swaggerUi.serve, swaggerUi.setup(swaggerSpec, options));
|
|
268
|
+
}
|
|
269
|
+
protected async startHomePage(): Promise<void>
|
|
270
|
+
{
|
|
271
|
+
this.express.get(this.base_path + "/", (req, res) =>
|
|
272
|
+
{
|
|
273
|
+
this.handleHomePage(req, res);
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
protected abstract handleHomePage(_: express.Request, __: express.Response): Promise<void>;
|
|
277
|
+
public getDirOfAsset(name: string)
|
|
278
|
+
{
|
|
279
|
+
return this.dir_asset + "/" + name;
|
|
280
|
+
}
|
|
281
|
+
public getPathOfAsset(name: string)
|
|
282
|
+
{
|
|
283
|
+
return this.path_asset + "/" + name;
|
|
284
|
+
}
|
|
285
|
+
protected async createAssets(): Promise<void>
|
|
286
|
+
{ }
|
|
287
|
+
protected async startAssets(): Promise<void>
|
|
288
|
+
{
|
|
289
|
+
this.express.use(this.path_asset + '/',
|
|
290
|
+
express.static(this.name_asset, { dotfiles: "allow" }),
|
|
291
|
+
serveIndex(this.name_asset, { icons: true, view: "details", hidden: true }));
|
|
292
|
+
}
|
|
293
|
+
public getDirOfPage(name: string)
|
|
294
|
+
{
|
|
295
|
+
return this.dir_page + "/" + name;
|
|
296
|
+
}
|
|
297
|
+
public getPathOfPage(name: string)
|
|
298
|
+
{
|
|
299
|
+
return this.path_page + "/" + name;
|
|
300
|
+
}
|
|
301
|
+
protected async createPages(): Promise<void>
|
|
302
|
+
{ }
|
|
303
|
+
protected async startPages(): Promise<void>
|
|
304
|
+
{
|
|
305
|
+
this.express.use(this.path_page + '/',
|
|
306
|
+
express.static(this.name_page, {
|
|
307
|
+
dotfiles: "allow", index: false, extensions: ['html']
|
|
308
|
+
}),
|
|
309
|
+
serveIndex(this.name_page, {
|
|
310
|
+
icons: true,
|
|
311
|
+
view: "details",
|
|
312
|
+
hidden: false,
|
|
313
|
+
filter: (filename) => { return filename.endsWith('.html'); }
|
|
314
|
+
|
|
315
|
+
}));
|
|
316
|
+
}
|
|
317
|
+
protected async startOthers(): Promise<void>
|
|
318
|
+
{
|
|
319
|
+
}
|
|
320
|
+
protected async onAfterStart(): Promise<void>
|
|
321
|
+
{
|
|
322
|
+
if (!process.env.NAMIRASOFT_MUTE)
|
|
323
|
+
await this.foreachDatabase(async database =>
|
|
324
|
+
{
|
|
325
|
+
await database.seedAfter();
|
|
326
|
+
})
|
|
327
|
+
}
|
|
328
|
+
protected async getRouter(): Promise<Router>
|
|
329
|
+
{
|
|
330
|
+
const router = express.Router({ mergeParams: true });
|
|
331
|
+
await this.foreachController(async generator =>
|
|
332
|
+
{
|
|
333
|
+
try
|
|
334
|
+
{
|
|
335
|
+
const template = generator(null as any, null as any);
|
|
336
|
+
let info = template.getInfo();
|
|
337
|
+
let handlers = [];
|
|
338
|
+
if (template.bodyAs.json.enabled)
|
|
339
|
+
handlers.push(express.json({ limit: template.bodyAs.json.limit }));
|
|
340
|
+
if (template.bodyAs.raw.enabled)
|
|
341
|
+
handlers.push(express.raw({ type: template.bodyAs.raw.type }));
|
|
342
|
+
handlers.push((req: express.Request, res: express.Response) =>
|
|
343
|
+
{
|
|
344
|
+
let controller = generator(req, res);
|
|
345
|
+
controller.run();
|
|
346
|
+
});
|
|
347
|
+
let path = this.base_path + info.path;
|
|
348
|
+
let ps: BaseVariableSchema[] = await template.getParametersSchema();
|
|
349
|
+
for (let p of ps)
|
|
350
|
+
path = path.replace(`{${p.name}}`, `:${p.name}`);
|
|
351
|
+
if (info.method == HTTPMethod.GET)
|
|
352
|
+
router.get(path, ...handlers);
|
|
353
|
+
else if (info.method == HTTPMethod.POST)
|
|
354
|
+
router.post(path, ...handlers);
|
|
355
|
+
else if (info.method == HTTPMethod.PUT)
|
|
356
|
+
router.put(path, ...handlers);
|
|
357
|
+
else if (info.method == HTTPMethod.DELETE)
|
|
358
|
+
router.delete(path, ...handlers);
|
|
359
|
+
} catch (error)
|
|
360
|
+
{
|
|
361
|
+
this.logger.onCatchFatal(error);
|
|
362
|
+
}
|
|
363
|
+
return true;
|
|
364
|
+
}, [this.mode]);
|
|
365
|
+
return router;
|
|
366
|
+
}
|
|
367
|
+
// schema
|
|
368
|
+
protected async saveSchema()
|
|
369
|
+
{
|
|
370
|
+
let app_schema = await this.getSchema<BaseController<D, {}, {}, {}>>(c => c.generate_sdk);
|
|
371
|
+
app_schema.toFile("./application.schema");
|
|
372
|
+
}
|
|
373
|
+
async getSchema<C>(filter: (controller: C) => boolean): Promise<ApplicationSchema>
|
|
374
|
+
{
|
|
375
|
+
let schema = new ApplicationSchema(this.pkg);
|
|
376
|
+
await this.foreachController(async generator =>
|
|
377
|
+
{
|
|
378
|
+
const template = generator(null as any, null as any);
|
|
379
|
+
if (template instanceof BaseController)
|
|
380
|
+
if (!filter || filter(template as C))
|
|
381
|
+
schema.controllers.push(await template.getSchema());
|
|
382
|
+
return true;
|
|
383
|
+
}, []);
|
|
384
|
+
let tables = this.getTables();
|
|
385
|
+
for (let i = 0; i < tables.length; i++)
|
|
386
|
+
{
|
|
387
|
+
const table = tables[i];
|
|
388
|
+
let obj = table.getSchema(true, null) as ObjectSchema;
|
|
389
|
+
schema.tables.push({
|
|
390
|
+
meta: {
|
|
391
|
+
short: table.getShortName()
|
|
392
|
+
},
|
|
393
|
+
schema: obj
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
schema.check();
|
|
397
|
+
return schema;
|
|
398
|
+
}
|
|
399
|
+
// public
|
|
400
|
+
async getLinks(): Promise<BaseApplicationLink[]>
|
|
401
|
+
{
|
|
402
|
+
if (this.linkLoader)
|
|
403
|
+
{
|
|
404
|
+
this.links = [];
|
|
405
|
+
await this.linkLoader();
|
|
406
|
+
}
|
|
407
|
+
return this.links;
|
|
408
|
+
}
|
|
409
|
+
setLinkLoader(linkLoader: () => Promise<void>)
|
|
410
|
+
{
|
|
411
|
+
this.linkLoader = linkLoader;
|
|
412
|
+
}
|
|
413
|
+
addLink(link: BaseApplicationLink)
|
|
414
|
+
{
|
|
415
|
+
this.links.push(link);
|
|
416
|
+
}
|
|
417
|
+
addLink_Asset()
|
|
418
|
+
{
|
|
419
|
+
this.addLink({
|
|
420
|
+
name: "Assets",
|
|
421
|
+
url: this.path_asset + "/",
|
|
422
|
+
logo: "https://static.namirasoft.com/image/concept/file/blue-yellow.png",
|
|
423
|
+
description: "This shows the list of all assets of this API"
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
addLink_Page()
|
|
427
|
+
{
|
|
428
|
+
this.addLink({
|
|
429
|
+
name: "Pages",
|
|
430
|
+
url: this.path_page + "/",
|
|
431
|
+
logo: "https://static.namirasoft.com/image/concept/page/gray.png",
|
|
432
|
+
description: "This shows the list of all pages of this API"
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
addLink_Swagger()
|
|
436
|
+
{
|
|
437
|
+
this.addLink({
|
|
438
|
+
name: "Swagger",
|
|
439
|
+
url: this.path_swagger + "/",
|
|
440
|
+
logo: "https://static.namirasoft.com/image/application/swagger/logo/base.png",
|
|
441
|
+
description: this.description + " Swagger"
|
|
442
|
+
});
|
|
443
|
+
}
|
|
441
444
|
}
|