namirasoft-node 1.2.11 → 1.2.13
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 +15 -12
- package/dist/BaseApplication.js +95 -34
- package/dist/BaseApplication.js.map +1 -1
- package/package.json +1 -1
- package/src/BaseApplication.ts +110 -48
|
@@ -2,36 +2,39 @@ import express, { Router } from 'express';
|
|
|
2
2
|
import { BaseDatabase } from './BaseDatabase';
|
|
3
3
|
import { ILogger } from "namirasoft-log";
|
|
4
4
|
import { BaseApplicationLink } from './BaseApplicationLink';
|
|
5
|
+
import { PackageService } from 'namirasoft-core';
|
|
5
6
|
import { ApplicationSchema } from 'namirasoft-schema';
|
|
6
7
|
export declare abstract class BaseApplication<D extends BaseDatabase> {
|
|
7
|
-
private
|
|
8
|
+
private linkLoader?;
|
|
9
|
+
private links;
|
|
10
|
+
protected pkg: PackageService;
|
|
11
|
+
protected title: string;
|
|
8
12
|
protected description: string;
|
|
9
13
|
protected logo: string;
|
|
10
14
|
protected version: string;
|
|
11
15
|
protected base_path: string;
|
|
12
16
|
protected swagger_path: string;
|
|
13
17
|
protected swagger_dir: string;
|
|
14
|
-
|
|
15
|
-
private links;
|
|
16
|
-
app: express.Express;
|
|
18
|
+
express: express.Express;
|
|
17
19
|
database: D;
|
|
18
20
|
logger: ILogger;
|
|
21
|
+
constructor(base_path: string, swagger_path?: string | null);
|
|
19
22
|
protected abstract getDatabase(): D;
|
|
20
23
|
protected abstract getLogger(): ILogger;
|
|
21
|
-
protected abstract getRouter(): Promise<Router>;
|
|
22
24
|
protected abstract getPort(): number;
|
|
23
|
-
|
|
24
|
-
setLinkLoader(linkLoader: () => Promise<void>): void;
|
|
25
|
-
addLink(link: BaseApplicationLink): void;
|
|
26
|
-
addSwaggerLink(): void;
|
|
25
|
+
protected abstract getControllers(): any[];
|
|
27
26
|
start(): Promise<void>;
|
|
28
27
|
protected startCrashHandler(): Promise<void>;
|
|
29
28
|
protected startDatabase(): Promise<void>;
|
|
30
|
-
protected
|
|
29
|
+
protected startExpress(): Promise<void>;
|
|
31
30
|
protected createSwagger(): Promise<void>;
|
|
32
31
|
protected startSwagger(): Promise<void>;
|
|
33
32
|
protected startHomePage(): Promise<void>;
|
|
33
|
+
protected startOthers(): Promise<void>;
|
|
34
|
+
protected getRouter(): Promise<Router>;
|
|
34
35
|
protected saveSchema(): Promise<void>;
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
getSchema(): Promise<ApplicationSchema>;
|
|
37
|
+
setLinkLoader(linkLoader: () => Promise<void>): void;
|
|
38
|
+
addLink(link: BaseApplicationLink): void;
|
|
39
|
+
addSwaggerLink(): void;
|
|
37
40
|
}
|
package/dist/BaseApplication.js
CHANGED
|
@@ -44,15 +44,17 @@ const swagger_ui_express_1 = __importDefault(require("swagger-ui-express"));
|
|
|
44
44
|
const swagger_jsdoc_1 = __importDefault(require("swagger-jsdoc"));
|
|
45
45
|
const namirasoft_core_1 = require("namirasoft-core");
|
|
46
46
|
const namirasoft_schema_1 = require("namirasoft-schema");
|
|
47
|
+
const EnvService_1 = require("./EnvService");
|
|
48
|
+
const BaseController_1 = require("./BaseController");
|
|
47
49
|
class BaseApplication {
|
|
48
50
|
constructor(base_path, swagger_path = null) {
|
|
49
|
-
var _a, _b, _c, _d;
|
|
51
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
50
52
|
this.links = [];
|
|
51
|
-
|
|
52
|
-
this.title = (_a =
|
|
53
|
-
this.description = (
|
|
54
|
-
this.logo = (
|
|
55
|
-
this.version = (
|
|
53
|
+
this.pkg = namirasoft_core_1.PackageService.getMain();
|
|
54
|
+
this.title = (_b = (_a = this.pkg) === null || _a === void 0 ? void 0 : _a.getTitle()) !== null && _b !== void 0 ? _b : "";
|
|
55
|
+
this.description = (_d = (_c = this.pkg) === null || _c === void 0 ? void 0 : _c.getDescription()) !== null && _d !== void 0 ? _d : "";
|
|
56
|
+
this.logo = (_f = (_e = this.pkg) === null || _e === void 0 ? void 0 : _e.getLogo()) !== null && _f !== void 0 ? _f : "";
|
|
57
|
+
this.version = (_h = (_g = this.pkg) === null || _g === void 0 ? void 0 : _g.getVersion()) !== null && _h !== void 0 ? _h : "";
|
|
56
58
|
this.base_path = base_path;
|
|
57
59
|
this.swagger_path = swagger_path !== null && swagger_path !== void 0 ? swagger_path : (base_path + "/swagger");
|
|
58
60
|
this.swagger_dir = "./swagger";
|
|
@@ -60,30 +62,20 @@ class BaseApplication {
|
|
|
60
62
|
this.addLink = this.addLink.bind(this);
|
|
61
63
|
this.addSwaggerLink = this.addSwaggerLink.bind(this);
|
|
62
64
|
}
|
|
63
|
-
|
|
64
|
-
this.linkLoader = linkLoader;
|
|
65
|
-
}
|
|
66
|
-
addLink(link) {
|
|
67
|
-
this.links.push(link);
|
|
68
|
-
}
|
|
69
|
-
addSwaggerLink() {
|
|
70
|
-
this.addLink({
|
|
71
|
-
name: "Swagger",
|
|
72
|
-
url: this.swagger_path + "/",
|
|
73
|
-
logo: "https://static.namirasoft.com/logo/swagger/base.png",
|
|
74
|
-
description: this.description + " Swagger"
|
|
75
|
-
});
|
|
76
|
-
}
|
|
65
|
+
// start
|
|
77
66
|
start() {
|
|
78
67
|
return __awaiter(this, void 0, void 0, function* () {
|
|
79
68
|
yield this.startCrashHandler();
|
|
80
69
|
yield this.startDatabase();
|
|
81
70
|
if (!process.env.NAMIRASOFT_MUTE) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
71
|
+
let SERVER_MODE = new EnvService_1.EnvService("SERVER_MODE", false).getString("api").toLowerCase();
|
|
72
|
+
if (SERVER_MODE == "api") {
|
|
73
|
+
yield this.startExpress();
|
|
74
|
+
yield this.createSwagger();
|
|
75
|
+
yield this.startSwagger();
|
|
76
|
+
yield this.startHomePage();
|
|
77
|
+
}
|
|
78
|
+
yield this.startOthers();
|
|
87
79
|
}
|
|
88
80
|
else {
|
|
89
81
|
yield this.saveSchema();
|
|
@@ -113,11 +105,11 @@ class BaseApplication {
|
|
|
113
105
|
yield this.database.sync(false);
|
|
114
106
|
});
|
|
115
107
|
}
|
|
116
|
-
|
|
108
|
+
startExpress() {
|
|
117
109
|
return __awaiter(this, void 0, void 0, function* () {
|
|
118
110
|
if (!process.env.NAMIRASOFT_MUTE) {
|
|
119
|
-
this.
|
|
120
|
-
this.
|
|
111
|
+
this.express = (0, express_1.default)();
|
|
112
|
+
this.express.use((req, res, next) => {
|
|
121
113
|
let excludes = [];
|
|
122
114
|
if (excludes.includes(req.path))
|
|
123
115
|
next();
|
|
@@ -125,14 +117,14 @@ class BaseApplication {
|
|
|
125
117
|
express_1.default.json({ limit: '100kb' })(req, res, next);
|
|
126
118
|
});
|
|
127
119
|
// Express
|
|
128
|
-
this.
|
|
120
|
+
this.express.use(express_1.default.static('static'));
|
|
129
121
|
// Cors
|
|
130
|
-
this.
|
|
122
|
+
this.express.use((0, cors_1.default)({ exposedHeaders: '*', }));
|
|
131
123
|
// api routes
|
|
132
|
-
this.
|
|
124
|
+
this.express.use('/', yield this.getRouter());
|
|
133
125
|
// start server
|
|
134
126
|
const port = this.getPort();
|
|
135
|
-
this.
|
|
127
|
+
this.express.listen(port, () => __awaiter(this, void 0, void 0, function* () {
|
|
136
128
|
this.logger.info(`Server listening on port ${port}`);
|
|
137
129
|
}));
|
|
138
130
|
}
|
|
@@ -186,12 +178,12 @@ class BaseApplication {
|
|
|
186
178
|
var options = {
|
|
187
179
|
explorer: true
|
|
188
180
|
};
|
|
189
|
-
this.
|
|
181
|
+
this.express.use(this.swagger_path + "/", swagger_ui_express_1.default.serve, swagger_ui_express_1.default.setup(swaggerSpec, options));
|
|
190
182
|
});
|
|
191
183
|
}
|
|
192
184
|
startHomePage() {
|
|
193
185
|
return __awaiter(this, void 0, void 0, function* () {
|
|
194
|
-
this.
|
|
186
|
+
this.express.get(this.base_path + "/", (_, res) => {
|
|
195
187
|
const htmlFilePath = path.join(__dirname, '../public/index.html');
|
|
196
188
|
fs.readFile(htmlFilePath, 'utf8', (err, data) => __awaiter(this, void 0, void 0, function* () {
|
|
197
189
|
if (err)
|
|
@@ -224,12 +216,81 @@ class BaseApplication {
|
|
|
224
216
|
});
|
|
225
217
|
});
|
|
226
218
|
}
|
|
219
|
+
startOthers() {
|
|
220
|
+
return __awaiter(this, void 0, void 0, function* () { });
|
|
221
|
+
}
|
|
222
|
+
getRouter() {
|
|
223
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
224
|
+
const router = express_1.default.Router({ mergeParams: true });
|
|
225
|
+
let controllers = this.getControllers();
|
|
226
|
+
for (let i = 0; i < controllers.length; i++) {
|
|
227
|
+
try {
|
|
228
|
+
const Template = controllers[i];
|
|
229
|
+
const template = new Template(this, null, null);
|
|
230
|
+
let info = template.getInfo();
|
|
231
|
+
let func = (req, res) => {
|
|
232
|
+
let controller = new Template(this, req, res);
|
|
233
|
+
controller.run();
|
|
234
|
+
};
|
|
235
|
+
let path = this.base_path + info.path;
|
|
236
|
+
let ps = yield template.getParametersSchema();
|
|
237
|
+
for (let p of ps)
|
|
238
|
+
path = path.replace(`{${p.name}}`, `:${p.name}`);
|
|
239
|
+
if (info.method == namirasoft_core_1.HTTPMethod.GET)
|
|
240
|
+
router.get(path, func);
|
|
241
|
+
else if (info.method == namirasoft_core_1.HTTPMethod.POST)
|
|
242
|
+
router.post(path, func);
|
|
243
|
+
else if (info.method == namirasoft_core_1.HTTPMethod.PUT)
|
|
244
|
+
router.put(path, func);
|
|
245
|
+
else if (info.method == namirasoft_core_1.HTTPMethod.DELETE)
|
|
246
|
+
router.delete(path, func);
|
|
247
|
+
else
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
this.logger.onCatchError(error);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return router;
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
// schema
|
|
227
258
|
saveSchema() {
|
|
228
259
|
return __awaiter(this, void 0, void 0, function* () {
|
|
229
260
|
let schema = yield this.getSchema();
|
|
230
261
|
schema.toFile("./application.schema");
|
|
231
262
|
});
|
|
232
263
|
}
|
|
264
|
+
getSchema() {
|
|
265
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
266
|
+
let schema = new namirasoft_schema_1.ApplicationSchema(this.pkg);
|
|
267
|
+
let controllers = this.getControllers();
|
|
268
|
+
for (let i = 0; i < controllers.length; i++) {
|
|
269
|
+
const Template = controllers[i];
|
|
270
|
+
const template = new Template(this, null, null);
|
|
271
|
+
template.database = this.database;
|
|
272
|
+
if (template instanceof BaseController_1.BaseController)
|
|
273
|
+
schema.controllers.push(yield template.getSchema());
|
|
274
|
+
}
|
|
275
|
+
schema.check();
|
|
276
|
+
return schema;
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
// public
|
|
280
|
+
setLinkLoader(linkLoader) {
|
|
281
|
+
this.linkLoader = linkLoader;
|
|
282
|
+
}
|
|
283
|
+
addLink(link) {
|
|
284
|
+
this.links.push(link);
|
|
285
|
+
}
|
|
286
|
+
addSwaggerLink() {
|
|
287
|
+
this.addLink({
|
|
288
|
+
name: "Swagger",
|
|
289
|
+
url: this.swagger_path + "/",
|
|
290
|
+
logo: "https://static.namirasoft.com/logo/swagger/base.png",
|
|
291
|
+
description: this.description + " Swagger"
|
|
292
|
+
});
|
|
293
|
+
}
|
|
233
294
|
}
|
|
234
295
|
exports.BaseApplication = BaseApplication;
|
|
235
296
|
//# sourceMappingURL=BaseApplication.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseApplication.js","sourceRoot":"","sources":["../src/BaseApplication.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sDAA0C;AAC1C,gDAAwB;AACxB,uCAAyB;AACzB,2CAA6B;AAC7B,4EAA2C;AAC3C,kEAAyC;AAIzC,
|
|
1
|
+
{"version":3,"file":"BaseApplication.js","sourceRoot":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IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;IACD,cAAc;QAEV,IAAI,CAAC,OAAO,CAAC;YACT,IAAI,EAAE,SAAS;YACf,GAAG,EAAE,IAAI,CAAC,YAAY,GAAG,GAAG;YAC5B,IAAI,EAAE,qDAAqD;YAC3D,WAAW,EAAE,IAAI,CAAC,WAAW,GAAG,UAAU;SAC7C,CAAC,CAAC;IACP,CAAC;CACJ;AApRD,0CAoRC"}
|
package/package.json
CHANGED
package/src/BaseApplication.ts
CHANGED
|
@@ -7,34 +7,33 @@ import swaggerJSDoc from 'swagger-jsdoc';
|
|
|
7
7
|
import { BaseDatabase } from './BaseDatabase';
|
|
8
8
|
import { ILogger } from "namirasoft-log";
|
|
9
9
|
import { BaseApplicationLink } from './BaseApplicationLink';
|
|
10
|
-
import { PackageService } from 'namirasoft-core';
|
|
11
|
-
import { ApplicationSchema, SwaggerApplicationBuilder } from 'namirasoft-schema';
|
|
10
|
+
import { HTTPMethod, PackageService } from 'namirasoft-core';
|
|
11
|
+
import { ApplicationSchema, BaseVariableSchema, SwaggerApplicationBuilder } from 'namirasoft-schema';
|
|
12
|
+
import { EnvService } from './EnvService';
|
|
13
|
+
import { BaseController } from './BaseController';
|
|
12
14
|
|
|
13
15
|
export abstract class BaseApplication<D extends BaseDatabase>
|
|
14
16
|
{
|
|
15
|
-
private
|
|
17
|
+
private linkLoader?: () => Promise<void>;
|
|
18
|
+
private links: BaseApplicationLink[] = [];
|
|
19
|
+
protected pkg: PackageService;
|
|
20
|
+
protected title: string;
|
|
16
21
|
protected description: string;
|
|
17
22
|
protected logo: string;
|
|
18
23
|
protected version: string;
|
|
19
24
|
protected base_path: string;
|
|
20
25
|
protected swagger_path: string;
|
|
21
26
|
protected swagger_dir: string;
|
|
22
|
-
|
|
23
|
-
private links: BaseApplicationLink[] = [];
|
|
24
|
-
public app!: express.Express;
|
|
27
|
+
public express!: express.Express;
|
|
25
28
|
public database!: D;
|
|
26
29
|
public logger: ILogger;
|
|
27
|
-
protected abstract getDatabase(): D;
|
|
28
|
-
protected abstract getLogger(): ILogger;
|
|
29
|
-
protected abstract getRouter(): Promise<Router>;
|
|
30
|
-
protected abstract getPort(): number;
|
|
31
30
|
constructor(base_path: string, swagger_path: string | null = null)
|
|
32
31
|
{
|
|
33
|
-
|
|
34
|
-
this.title =
|
|
35
|
-
this.description =
|
|
36
|
-
this.logo =
|
|
37
|
-
this.version =
|
|
32
|
+
this.pkg = PackageService.getMain();
|
|
33
|
+
this.title = this.pkg?.getTitle() ?? "";
|
|
34
|
+
this.description = this.pkg?.getDescription() ?? "";
|
|
35
|
+
this.logo = this.pkg?.getLogo() ?? "";
|
|
36
|
+
this.version = this.pkg?.getVersion() ?? "";
|
|
38
37
|
this.base_path = base_path;
|
|
39
38
|
this.swagger_path = swagger_path ?? (base_path + "/swagger");
|
|
40
39
|
this.swagger_dir = "./swagger";
|
|
@@ -42,34 +41,26 @@ export abstract class BaseApplication<D extends BaseDatabase>
|
|
|
42
41
|
this.addLink = this.addLink.bind(this);
|
|
43
42
|
this.addSwaggerLink = this.addSwaggerLink.bind(this);
|
|
44
43
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
{
|
|
51
|
-
this.links.push(link);
|
|
52
|
-
}
|
|
53
|
-
addSwaggerLink()
|
|
54
|
-
{
|
|
55
|
-
this.addLink({
|
|
56
|
-
name: "Swagger",
|
|
57
|
-
url: this.swagger_path + "/",
|
|
58
|
-
logo: "https://static.namirasoft.com/logo/swagger/base.png",
|
|
59
|
-
description: this.description + " Swagger"
|
|
60
|
-
});
|
|
61
|
-
}
|
|
44
|
+
protected abstract getDatabase(): D;
|
|
45
|
+
protected abstract getLogger(): ILogger;
|
|
46
|
+
protected abstract getPort(): number;
|
|
47
|
+
protected abstract getControllers(): any[];
|
|
48
|
+
// start
|
|
62
49
|
async start()
|
|
63
50
|
{
|
|
64
51
|
await this.startCrashHandler();
|
|
65
52
|
await this.startDatabase();
|
|
66
53
|
if (!process.env.NAMIRASOFT_MUTE)
|
|
67
54
|
{
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
55
|
+
let SERVER_MODE = new EnvService("SERVER_MODE", false).getString("api").toLowerCase();
|
|
56
|
+
if (SERVER_MODE == "api")
|
|
57
|
+
{
|
|
58
|
+
await this.startExpress();
|
|
59
|
+
await this.createSwagger();
|
|
60
|
+
await this.startSwagger();
|
|
61
|
+
await this.startHomePage();
|
|
62
|
+
}
|
|
63
|
+
await this.startOthers();
|
|
73
64
|
}
|
|
74
65
|
else
|
|
75
66
|
{
|
|
@@ -100,12 +91,12 @@ export abstract class BaseApplication<D extends BaseDatabase>
|
|
|
100
91
|
await this.database.connect();
|
|
101
92
|
await this.database.sync(false);
|
|
102
93
|
}
|
|
103
|
-
protected async
|
|
94
|
+
protected async startExpress(): Promise<void>
|
|
104
95
|
{
|
|
105
96
|
if (!process.env.NAMIRASOFT_MUTE)
|
|
106
97
|
{
|
|
107
|
-
this.
|
|
108
|
-
this.
|
|
98
|
+
this.express = express();
|
|
99
|
+
this.express.use((req, res, next) =>
|
|
109
100
|
{
|
|
110
101
|
let excludes: string[] = [];
|
|
111
102
|
if (excludes.includes(req.path))
|
|
@@ -114,14 +105,14 @@ export abstract class BaseApplication<D extends BaseDatabase>
|
|
|
114
105
|
express.json({ limit: '100kb' })(req, res, next);
|
|
115
106
|
});
|
|
116
107
|
// Express
|
|
117
|
-
this.
|
|
108
|
+
this.express.use(express.static('static'));
|
|
118
109
|
// Cors
|
|
119
|
-
this.
|
|
110
|
+
this.express.use(cors({ exposedHeaders: '*', }));
|
|
120
111
|
// api routes
|
|
121
|
-
this.
|
|
112
|
+
this.express.use('/', await this.getRouter());
|
|
122
113
|
// start server
|
|
123
114
|
const port = this.getPort();
|
|
124
|
-
this.
|
|
115
|
+
this.express.listen(port, async () =>
|
|
125
116
|
{
|
|
126
117
|
this.logger.info(`Server listening on port ${port}`);
|
|
127
118
|
});
|
|
@@ -176,11 +167,11 @@ export abstract class BaseApplication<D extends BaseDatabase>
|
|
|
176
167
|
var options = {
|
|
177
168
|
explorer: true
|
|
178
169
|
};
|
|
179
|
-
this.
|
|
170
|
+
this.express.use(this.swagger_path + "/", swaggerUi.serve, swaggerUi.setup(swaggerSpec, options));
|
|
180
171
|
}
|
|
181
172
|
protected async startHomePage(): Promise<void>
|
|
182
173
|
{
|
|
183
|
-
this.
|
|
174
|
+
this.express.get(this.base_path + "/", (_, res) =>
|
|
184
175
|
{
|
|
185
176
|
const htmlFilePath = path.join(__dirname, '../public/index.html');
|
|
186
177
|
fs.readFile(htmlFilePath, 'utf8', async (err: any, data: string) =>
|
|
@@ -219,11 +210,82 @@ export abstract class BaseApplication<D extends BaseDatabase>
|
|
|
219
210
|
});
|
|
220
211
|
});
|
|
221
212
|
}
|
|
213
|
+
protected async startOthers(): Promise<void>
|
|
214
|
+
{ }
|
|
215
|
+
protected async getRouter(): Promise<Router>
|
|
216
|
+
{
|
|
217
|
+
const router = express.Router({ mergeParams: true });
|
|
218
|
+
let controllers = this.getControllers();
|
|
219
|
+
for (let i = 0; i < controllers.length; i++)
|
|
220
|
+
{
|
|
221
|
+
try
|
|
222
|
+
{
|
|
223
|
+
const Template = controllers[i] as any;
|
|
224
|
+
const template = new Template(this, null, null);
|
|
225
|
+
let info = template.getInfo();
|
|
226
|
+
let func = (req: express.Request, res: express.Response) =>
|
|
227
|
+
{
|
|
228
|
+
let controller = new Template(this, req, res);
|
|
229
|
+
controller.run();
|
|
230
|
+
};
|
|
231
|
+
let path = this.base_path + info.path;
|
|
232
|
+
let ps: BaseVariableSchema[] = await template.getParametersSchema();
|
|
233
|
+
for (let p of ps)
|
|
234
|
+
path = path.replace(`{${p.name}}`, `:${p.name}`);
|
|
235
|
+
if (info.method == HTTPMethod.GET)
|
|
236
|
+
router.get(path, func);
|
|
237
|
+
else if (info.method == HTTPMethod.POST)
|
|
238
|
+
router.post(path, func);
|
|
239
|
+
else if (info.method == HTTPMethod.PUT)
|
|
240
|
+
router.put(path, func);
|
|
241
|
+
else if (info.method == HTTPMethod.DELETE)
|
|
242
|
+
router.delete(path, func);
|
|
243
|
+
else
|
|
244
|
+
break;
|
|
245
|
+
} catch (error)
|
|
246
|
+
{
|
|
247
|
+
this.logger.onCatchError(error);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return router;
|
|
251
|
+
}
|
|
252
|
+
// schema
|
|
222
253
|
protected async saveSchema()
|
|
223
254
|
{
|
|
224
255
|
let schema = await this.getSchema();
|
|
225
256
|
schema.toFile("./application.schema");
|
|
226
257
|
}
|
|
227
|
-
|
|
228
|
-
|
|
258
|
+
async getSchema(): Promise<ApplicationSchema>
|
|
259
|
+
{
|
|
260
|
+
let schema = new ApplicationSchema(this.pkg);
|
|
261
|
+
let controllers = this.getControllers();
|
|
262
|
+
for (let i = 0; i < controllers.length; i++)
|
|
263
|
+
{
|
|
264
|
+
const Template = controllers[i] as any;
|
|
265
|
+
const template = new Template(this, null, null);
|
|
266
|
+
template.database = this.database;
|
|
267
|
+
if (template instanceof BaseController)
|
|
268
|
+
schema.controllers.push(await template.getSchema());
|
|
269
|
+
}
|
|
270
|
+
schema.check();
|
|
271
|
+
return schema;
|
|
272
|
+
}
|
|
273
|
+
// public
|
|
274
|
+
setLinkLoader(linkLoader: () => Promise<void>)
|
|
275
|
+
{
|
|
276
|
+
this.linkLoader = linkLoader;
|
|
277
|
+
}
|
|
278
|
+
addLink(link: BaseApplicationLink)
|
|
279
|
+
{
|
|
280
|
+
this.links.push(link);
|
|
281
|
+
}
|
|
282
|
+
addSwaggerLink()
|
|
283
|
+
{
|
|
284
|
+
this.addLink({
|
|
285
|
+
name: "Swagger",
|
|
286
|
+
url: this.swagger_path + "/",
|
|
287
|
+
logo: "https://static.namirasoft.com/logo/swagger/base.png",
|
|
288
|
+
description: this.description + " Swagger"
|
|
289
|
+
});
|
|
290
|
+
}
|
|
229
291
|
}
|