@webtypen/webframez-core 0.0.14 → 0.0.16
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/lib/BaseKernelConsole.d.ts +3 -0
- package/lib/BaseKernelConsole.js +7 -0
- package/lib/BaseKernelWeb.d.ts +9 -0
- package/lib/BaseKernelWeb.js +7 -0
- package/lib/Config.d.ts +16 -0
- package/lib/Config.js +36 -0
- package/lib/Database/DBConnection.js +15 -10
- package/lib/Database/QueryBuilder.d.ts +3 -3
- package/lib/Database/QueryBuilder.js +3 -0
- package/lib/Router/Request.d.ts +1 -0
- package/lib/Router/Router.d.ts +5 -0
- package/lib/Router/Router.js +48 -4
- package/lib/WebApplication.js +12 -1
- package/lib/index.d.ts +3 -1
- package/lib/index.js +3 -1
- package/package.json +2 -1
- package/src/BaseKernelConsole.ts +3 -0
- package/src/{BaseKernel.ts → BaseKernelWeb.ts} +1 -2
- package/src/Config.ts +37 -0
- package/src/Database/DBConnection.ts +13 -1
- package/src/Database/QueryBuilder.ts +3 -0
- package/src/Router/Request.ts +1 -0
- package/src/Router/Router.ts +55 -4
- package/src/WebApplication.ts +15 -1
- package/src/index.ts +3 -1
package/lib/Config.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type ConfigType = {
|
|
2
|
+
key: string;
|
|
3
|
+
component: any;
|
|
4
|
+
};
|
|
5
|
+
declare class ConfigFacade {
|
|
6
|
+
configs: {
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
};
|
|
9
|
+
register(key: string | ConfigType[], component: {
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
}): this;
|
|
12
|
+
get(key: string): any;
|
|
13
|
+
set(path: string, value: any | null | undefined): this;
|
|
14
|
+
}
|
|
15
|
+
export declare const Config: ConfigFacade;
|
|
16
|
+
export {};
|
package/lib/Config.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Config = void 0;
|
|
7
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
8
|
+
class ConfigFacade {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.configs = {};
|
|
11
|
+
}
|
|
12
|
+
register(key, component) {
|
|
13
|
+
if (typeof key === "string") {
|
|
14
|
+
this.configs[key] = component;
|
|
15
|
+
}
|
|
16
|
+
else if (Array.isArray(key)) {
|
|
17
|
+
for (let conf of key) {
|
|
18
|
+
if (!conf.key || !conf.component) {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
this.configs[conf.key] = conf.component;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
get(key) {
|
|
27
|
+
return lodash_1.default.get(this.configs, key);
|
|
28
|
+
}
|
|
29
|
+
set(path, value) {
|
|
30
|
+
const newConfig = Object.assign({}, this.configs);
|
|
31
|
+
lodash_1.default.set(newConfig, path, value);
|
|
32
|
+
this.configs = newConfig;
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.Config = new ConfigFacade();
|
|
@@ -8,29 +8,30 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
12
|
exports.DBConnection = void 0;
|
|
16
13
|
// @ts-ignore
|
|
17
|
-
const
|
|
14
|
+
const Config_1 = require("../Config");
|
|
18
15
|
const DBDrivers_1 = require("./DBDrivers");
|
|
19
16
|
class DBConnectionFacade {
|
|
20
17
|
constructor() {
|
|
21
18
|
this.connections = {};
|
|
22
19
|
}
|
|
23
20
|
getConnectionConfig(connection) {
|
|
21
|
+
const dbconfig = Config_1.Config.get("database");
|
|
22
|
+
if (!dbconfig) {
|
|
23
|
+
throw new Error("Missing database-config ...");
|
|
24
|
+
}
|
|
24
25
|
if (!connection) {
|
|
25
|
-
connection =
|
|
26
|
+
connection = dbconfig.defaultConnection;
|
|
26
27
|
}
|
|
27
28
|
if (!connection ||
|
|
28
|
-
!
|
|
29
|
-
!
|
|
30
|
-
!
|
|
29
|
+
!dbconfig ||
|
|
30
|
+
!dbconfig.connections ||
|
|
31
|
+
!dbconfig.connections[connection]) {
|
|
31
32
|
throw new Error("No connection given ...");
|
|
32
33
|
}
|
|
33
|
-
return
|
|
34
|
+
return dbconfig.connections[connection];
|
|
34
35
|
}
|
|
35
36
|
getConnectionDriver(connection) {
|
|
36
37
|
const config = this.getConnectionConfig(connection);
|
|
@@ -44,8 +45,12 @@ class DBConnectionFacade {
|
|
|
44
45
|
}
|
|
45
46
|
getConnection(connectionName) {
|
|
46
47
|
return __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
const dbconfig = Config_1.Config.get("database");
|
|
49
|
+
if (!dbconfig) {
|
|
50
|
+
throw new Error("Missing database-config ...");
|
|
51
|
+
}
|
|
47
52
|
if (!connectionName) {
|
|
48
|
-
connectionName =
|
|
53
|
+
connectionName = dbconfig.defaultConnection;
|
|
49
54
|
}
|
|
50
55
|
if (!connectionName) {
|
|
51
56
|
return null;
|
|
@@ -4,9 +4,9 @@ export declare class QueryBuilder {
|
|
|
4
4
|
queryTable: string;
|
|
5
5
|
mode: string;
|
|
6
6
|
modelMapping: null;
|
|
7
|
-
setModelMapping(model?: any | null):
|
|
8
|
-
table(table: string):
|
|
9
|
-
where(column: any, operator: any, value: any):
|
|
7
|
+
setModelMapping(model?: any | null): this;
|
|
8
|
+
table(table: string): this;
|
|
9
|
+
where(column: any, operator: any, value: any): this;
|
|
10
10
|
get(options?: any): Promise<any>;
|
|
11
11
|
first(options?: any): Promise<any>;
|
|
12
12
|
delete(options?: {
|
|
@@ -21,9 +21,11 @@ class QueryBuilder {
|
|
|
21
21
|
}
|
|
22
22
|
setModelMapping(model) {
|
|
23
23
|
this.modelMapping = model;
|
|
24
|
+
return this;
|
|
24
25
|
}
|
|
25
26
|
table(table) {
|
|
26
27
|
this.queryTable = table;
|
|
28
|
+
return this;
|
|
27
29
|
}
|
|
28
30
|
where(column, operator, value) {
|
|
29
31
|
this.query.push({
|
|
@@ -32,6 +34,7 @@ class QueryBuilder {
|
|
|
32
34
|
operator: operator,
|
|
33
35
|
value: value,
|
|
34
36
|
});
|
|
37
|
+
return this;
|
|
35
38
|
}
|
|
36
39
|
get(options) {
|
|
37
40
|
return __awaiter(this, void 0, void 0, function* () {
|
package/lib/Router/Request.d.ts
CHANGED
package/lib/Router/Router.d.ts
CHANGED
|
@@ -13,6 +13,10 @@ declare class RouterFacade {
|
|
|
13
13
|
* Basename
|
|
14
14
|
*/
|
|
15
15
|
basename: string | null;
|
|
16
|
+
/**
|
|
17
|
+
* Kernel-Class
|
|
18
|
+
*/
|
|
19
|
+
kernel: any | null;
|
|
16
20
|
/**
|
|
17
21
|
* GET Route Store
|
|
18
22
|
*/
|
|
@@ -99,6 +103,7 @@ declare class RouterFacade {
|
|
|
99
103
|
* @param res
|
|
100
104
|
*/
|
|
101
105
|
handleRequest(req: IncomingMessage, res: ServerResponse): Promise<void>;
|
|
106
|
+
requestConsoleLog(req: IncomingMessage): void;
|
|
102
107
|
}
|
|
103
108
|
export declare const Router: RouterFacade;
|
|
104
109
|
export {};
|
package/lib/Router/Router.js
CHANGED
|
@@ -15,14 +15,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.Router = void 0;
|
|
16
16
|
const qs_1 = __importDefault(require("qs"));
|
|
17
17
|
const Response_1 = require("./Response");
|
|
18
|
-
|
|
19
|
-
const Kernel_1 = require("../../../../../app/Kernel");
|
|
18
|
+
const Config_1 = require("../Config");
|
|
20
19
|
class RouterFacade {
|
|
21
20
|
constructor() {
|
|
22
21
|
/**
|
|
23
22
|
* Basename
|
|
24
23
|
*/
|
|
25
24
|
this.basename = null;
|
|
25
|
+
/**
|
|
26
|
+
* Kernel-Class
|
|
27
|
+
*/
|
|
28
|
+
this.kernel = null;
|
|
26
29
|
/**
|
|
27
30
|
* GET Route Store
|
|
28
31
|
*/
|
|
@@ -46,8 +49,11 @@ class RouterFacade {
|
|
|
46
49
|
*/
|
|
47
50
|
init(options) {
|
|
48
51
|
this.basename = options && options.basename ? options.basename : null;
|
|
52
|
+
this.kernel = options && options.kernel ? options.kernel : null;
|
|
49
53
|
// Load routes
|
|
54
|
+
console.log("loadRoutes");
|
|
50
55
|
require(process.cwd() + "/app/routes");
|
|
56
|
+
console.log("finishRoutes");
|
|
51
57
|
}
|
|
52
58
|
/**
|
|
53
59
|
* Register a route
|
|
@@ -174,13 +180,16 @@ class RouterFacade {
|
|
|
174
180
|
const route = this.dissolve(req);
|
|
175
181
|
const response = new Response_1.Response();
|
|
176
182
|
response.setServerResponse(res);
|
|
183
|
+
if (!this.kernel) {
|
|
184
|
+
throw new Error("Missing Kernel in Router ...");
|
|
185
|
+
}
|
|
177
186
|
// Check-Middleware
|
|
178
187
|
if (route &&
|
|
179
188
|
route.options &&
|
|
180
189
|
route.options.middleware &&
|
|
181
190
|
route.options.middleware.length > 0) {
|
|
182
191
|
for (let middlewareKey of route.options.middleware) {
|
|
183
|
-
const middleware =
|
|
192
|
+
const middleware = this.kernel.middleware[middlewareKey];
|
|
184
193
|
if (!middleware) {
|
|
185
194
|
return this.sendError(res, 500, "Unknown middleware `" + middlewareKey + "` ...");
|
|
186
195
|
}
|
|
@@ -221,7 +230,7 @@ class RouterFacade {
|
|
|
221
230
|
// Resolve route-component (handle controller definition)
|
|
222
231
|
if (route && typeof route.component === "string") {
|
|
223
232
|
const split = route.component.split("@");
|
|
224
|
-
const controller =
|
|
233
|
+
const controller = this.kernel.controller[split[0]];
|
|
225
234
|
if (!controller) {
|
|
226
235
|
res.writeHead(500, { "Content-Type": "text/html" });
|
|
227
236
|
res.end("Controller `" + split[0] + "` not found ...");
|
|
@@ -274,5 +283,40 @@ class RouterFacade {
|
|
|
274
283
|
}
|
|
275
284
|
});
|
|
276
285
|
}
|
|
286
|
+
requestConsoleLog(req) {
|
|
287
|
+
if (Config_1.Config.get("router.requests.logConsole") !== true) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
const date = new Date();
|
|
291
|
+
const month = date.getMonth() + 1;
|
|
292
|
+
const ip = req.headers["x-forwarded-for"]
|
|
293
|
+
? req.headers["x-forwarded-for"]
|
|
294
|
+
: req.headers["x-forwarded"]
|
|
295
|
+
? req.headers["x-forwarded"]
|
|
296
|
+
: req.headers["forwarded-for"]
|
|
297
|
+
? req.headers["forwarded-for"]
|
|
298
|
+
: req.socket && req.socket.remoteAddress
|
|
299
|
+
? req.socket.remoteAddress
|
|
300
|
+
: null;
|
|
301
|
+
console.log('{ date: "' +
|
|
302
|
+
date.getFullYear() +
|
|
303
|
+
"-" +
|
|
304
|
+
(month < 10 ? "0" + month : month) +
|
|
305
|
+
"-" +
|
|
306
|
+
date.getDate() +
|
|
307
|
+
" " +
|
|
308
|
+
(date.getHours() + 1) +
|
|
309
|
+
":" +
|
|
310
|
+
date.getMinutes() +
|
|
311
|
+
":" +
|
|
312
|
+
date.getSeconds() +
|
|
313
|
+
'", method: "' +
|
|
314
|
+
req.method +
|
|
315
|
+
'", url: "' +
|
|
316
|
+
req.url +
|
|
317
|
+
'", ip: "' +
|
|
318
|
+
ip +
|
|
319
|
+
'" }');
|
|
320
|
+
}
|
|
277
321
|
}
|
|
278
322
|
exports.Router = new RouterFacade();
|
package/lib/WebApplication.js
CHANGED
|
@@ -5,13 +5,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.WebApplication = void 0;
|
|
7
7
|
const http_1 = __importDefault(require("http"));
|
|
8
|
+
const Config_1 = require("./Config");
|
|
8
9
|
const Router_1 = require("./Router/Router");
|
|
9
10
|
class WebApplication {
|
|
10
11
|
/**
|
|
11
12
|
* Init the routes and start the http-server
|
|
12
13
|
*/
|
|
13
14
|
boot(options) {
|
|
15
|
+
if (options && options.config) {
|
|
16
|
+
for (let key in options.config) {
|
|
17
|
+
Config_1.Config.register(key, options.config[key]);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
14
20
|
Router_1.Router.init({
|
|
21
|
+
kernel: options && options.kernel ? options.kernel : null,
|
|
15
22
|
basename: options && options.basename ? options.basename : null,
|
|
16
23
|
});
|
|
17
24
|
const port = options && options.port ? options.port : 3000;
|
|
@@ -20,7 +27,11 @@ class WebApplication {
|
|
|
20
27
|
Router_1.Router.handleRequest(req, res);
|
|
21
28
|
})
|
|
22
29
|
.listen(port, () => {
|
|
23
|
-
console.log("Server started and listening on port " +
|
|
30
|
+
console.log("Server started and listening on port " +
|
|
31
|
+
port +
|
|
32
|
+
(options && options.basename
|
|
33
|
+
? " (Basename: " + options && options.basename + ")"
|
|
34
|
+
: ""));
|
|
24
35
|
});
|
|
25
36
|
}
|
|
26
37
|
}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -14,7 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./Config"), exports);
|
|
18
|
+
__exportStar(require("./BaseKernelWeb"), exports);
|
|
19
|
+
__exportStar(require("./BaseKernelConsole"), exports);
|
|
18
20
|
__exportStar(require("./Controller/Controller"), exports);
|
|
19
21
|
__exportStar(require("./Database/Model"), exports);
|
|
20
22
|
__exportStar(require("./WebApplication"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webtypen/webframez-core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.16",
|
|
4
4
|
"description": "The core package of the typescript based webframez (web-framework) by webtypen.de",
|
|
5
5
|
"homepage": "https://webtypen.de/",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"build:check": "tsc --noEmit"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
+
"@types/lodash": "^4.14.190",
|
|
25
26
|
"@types/node": "^18.11.9",
|
|
26
27
|
"@types/qs": "^6.9.7",
|
|
27
28
|
"ts-node": "^10.9.1",
|
package/src/Config.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import lodash from "lodash";
|
|
2
|
+
|
|
3
|
+
type ConfigType = {
|
|
4
|
+
key: string;
|
|
5
|
+
component: any;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
class ConfigFacade {
|
|
9
|
+
configs: { [key: string]: any } = {};
|
|
10
|
+
|
|
11
|
+
register(key: string | ConfigType[], component: { [key: string]: any }) {
|
|
12
|
+
if (typeof key === "string") {
|
|
13
|
+
this.configs[key] = component;
|
|
14
|
+
} else if (Array.isArray(key)) {
|
|
15
|
+
for (let conf of key) {
|
|
16
|
+
if (!conf.key || !conf.component) {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
this.configs[conf.key] = conf.component;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get(key: string) {
|
|
26
|
+
return lodash.get(this.configs, key);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
set(path: string, value: any | null | undefined) {
|
|
30
|
+
const newConfig = { ...this.configs };
|
|
31
|
+
lodash.set(newConfig, path, value);
|
|
32
|
+
this.configs = newConfig;
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const Config = new ConfigFacade();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// @ts-ignore
|
|
2
|
-
import
|
|
2
|
+
import { Config } from "../Config";
|
|
3
3
|
import { DBDrivers } from "./DBDrivers";
|
|
4
4
|
import { QueryBuilder } from "./QueryBuilder";
|
|
5
5
|
|
|
@@ -7,6 +7,12 @@ class DBConnectionFacade {
|
|
|
7
7
|
connections: { [key: string]: any } = {};
|
|
8
8
|
|
|
9
9
|
getConnectionConfig(connection: string) {
|
|
10
|
+
const dbconfig = Config.get("database");
|
|
11
|
+
|
|
12
|
+
if (!dbconfig) {
|
|
13
|
+
throw new Error("Missing database-config ...");
|
|
14
|
+
}
|
|
15
|
+
|
|
10
16
|
if (!connection) {
|
|
11
17
|
connection = dbconfig.defaultConnection;
|
|
12
18
|
}
|
|
@@ -36,6 +42,12 @@ class DBConnectionFacade {
|
|
|
36
42
|
}
|
|
37
43
|
|
|
38
44
|
async getConnection(connectionName?: string) {
|
|
45
|
+
const dbconfig = Config.get("database");
|
|
46
|
+
|
|
47
|
+
if (!dbconfig) {
|
|
48
|
+
throw new Error("Missing database-config ...");
|
|
49
|
+
}
|
|
50
|
+
|
|
39
51
|
if (!connectionName) {
|
|
40
52
|
connectionName = dbconfig.defaultConnection;
|
|
41
53
|
}
|
|
@@ -10,10 +10,12 @@ export class QueryBuilder {
|
|
|
10
10
|
|
|
11
11
|
setModelMapping(model?: any | null) {
|
|
12
12
|
this.modelMapping = model;
|
|
13
|
+
return this;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
table(table: string) {
|
|
16
17
|
this.queryTable = table;
|
|
18
|
+
return this;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
where(column: any, operator: any, value: any) {
|
|
@@ -23,6 +25,7 @@ export class QueryBuilder {
|
|
|
23
25
|
operator: operator,
|
|
24
26
|
value: value,
|
|
25
27
|
});
|
|
28
|
+
return this;
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
async get(options?: any) {
|
package/src/Router/Request.ts
CHANGED
package/src/Router/Router.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import qs from "qs";
|
|
2
2
|
import { IncomingMessage, ServerResponse } from "http";
|
|
3
3
|
import { Response } from "./Response";
|
|
4
|
-
|
|
5
|
-
import { Kernel } from "../../../../../app/Kernel";
|
|
4
|
+
import { Config } from "../Config";
|
|
6
5
|
|
|
7
6
|
type RouteObject = {
|
|
8
7
|
path: string;
|
|
@@ -17,6 +16,11 @@ class RouterFacade {
|
|
|
17
16
|
*/
|
|
18
17
|
basename: string | null = null;
|
|
19
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Kernel-Class
|
|
21
|
+
*/
|
|
22
|
+
kernel: any | null = null;
|
|
23
|
+
|
|
20
24
|
/**
|
|
21
25
|
* GET Route Store
|
|
22
26
|
*/
|
|
@@ -71,9 +75,12 @@ class RouterFacade {
|
|
|
71
75
|
*/
|
|
72
76
|
init(options?: any) {
|
|
73
77
|
this.basename = options && options.basename ? options.basename : null;
|
|
78
|
+
this.kernel = options && options.kernel ? options.kernel : null;
|
|
74
79
|
|
|
75
80
|
// Load routes
|
|
81
|
+
console.log("loadRoutes");
|
|
76
82
|
require(process.cwd() + "/app/routes");
|
|
83
|
+
console.log("finishRoutes");
|
|
77
84
|
}
|
|
78
85
|
|
|
79
86
|
/**
|
|
@@ -204,6 +211,10 @@ class RouterFacade {
|
|
|
204
211
|
const response = new Response();
|
|
205
212
|
response.setServerResponse(res);
|
|
206
213
|
|
|
214
|
+
if (!this.kernel) {
|
|
215
|
+
throw new Error("Missing Kernel in Router ...");
|
|
216
|
+
}
|
|
217
|
+
|
|
207
218
|
// Check-Middleware
|
|
208
219
|
if (
|
|
209
220
|
route &&
|
|
@@ -212,7 +223,7 @@ class RouterFacade {
|
|
|
212
223
|
route.options.middleware.length > 0
|
|
213
224
|
) {
|
|
214
225
|
for (let middlewareKey of route.options.middleware) {
|
|
215
|
-
const middleware =
|
|
226
|
+
const middleware = this.kernel.middleware[middlewareKey];
|
|
216
227
|
if (!middleware) {
|
|
217
228
|
return this.sendError(
|
|
218
229
|
res,
|
|
@@ -273,7 +284,7 @@ class RouterFacade {
|
|
|
273
284
|
// Resolve route-component (handle controller definition)
|
|
274
285
|
if (route && typeof route.component === "string") {
|
|
275
286
|
const split = route.component.split("@");
|
|
276
|
-
const controller =
|
|
287
|
+
const controller = this.kernel.controller[split[0]];
|
|
277
288
|
if (!controller) {
|
|
278
289
|
res.writeHead(500, { "Content-Type": "text/html" });
|
|
279
290
|
res.end("Controller `" + split[0] + "` not found ...");
|
|
@@ -330,6 +341,46 @@ class RouterFacade {
|
|
|
330
341
|
this.sendError(res, 404);
|
|
331
342
|
}
|
|
332
343
|
}
|
|
344
|
+
|
|
345
|
+
requestConsoleLog(req: IncomingMessage) {
|
|
346
|
+
if (Config.get("router.requests.logConsole") !== true) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const date = new Date();
|
|
351
|
+
const month = date.getMonth() + 1;
|
|
352
|
+
const ip = req.headers["x-forwarded-for"]
|
|
353
|
+
? req.headers["x-forwarded-for"]
|
|
354
|
+
: req.headers["x-forwarded"]
|
|
355
|
+
? req.headers["x-forwarded"]
|
|
356
|
+
: req.headers["forwarded-for"]
|
|
357
|
+
? req.headers["forwarded-for"]
|
|
358
|
+
: req.socket && req.socket.remoteAddress
|
|
359
|
+
? req.socket.remoteAddress
|
|
360
|
+
: null;
|
|
361
|
+
|
|
362
|
+
console.log(
|
|
363
|
+
'{ date: "' +
|
|
364
|
+
date.getFullYear() +
|
|
365
|
+
"-" +
|
|
366
|
+
(month < 10 ? "0" + month : month) +
|
|
367
|
+
"-" +
|
|
368
|
+
date.getDate() +
|
|
369
|
+
" " +
|
|
370
|
+
(date.getHours() + 1) +
|
|
371
|
+
":" +
|
|
372
|
+
date.getMinutes() +
|
|
373
|
+
":" +
|
|
374
|
+
date.getSeconds() +
|
|
375
|
+
'", method: "' +
|
|
376
|
+
req.method +
|
|
377
|
+
'", url: "' +
|
|
378
|
+
req.url +
|
|
379
|
+
'", ip: "' +
|
|
380
|
+
ip +
|
|
381
|
+
'" }'
|
|
382
|
+
);
|
|
383
|
+
}
|
|
333
384
|
}
|
|
334
385
|
|
|
335
386
|
export const Router = new RouterFacade();
|
package/src/WebApplication.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import http, { Server } from "http";
|
|
2
|
+
import { Config } from "./Config";
|
|
2
3
|
import { Router } from "./Router/Router";
|
|
3
4
|
|
|
4
5
|
export class WebApplication {
|
|
@@ -8,7 +9,14 @@ export class WebApplication {
|
|
|
8
9
|
* Init the routes and start the http-server
|
|
9
10
|
*/
|
|
10
11
|
boot(options?: any) {
|
|
12
|
+
if (options && options.config) {
|
|
13
|
+
for (let key in options.config) {
|
|
14
|
+
Config.register(key, options.config[key]);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
11
18
|
Router.init({
|
|
19
|
+
kernel: options && options.kernel ? options.kernel : null,
|
|
12
20
|
basename: options && options.basename ? options.basename : null,
|
|
13
21
|
});
|
|
14
22
|
|
|
@@ -18,7 +26,13 @@ export class WebApplication {
|
|
|
18
26
|
Router.handleRequest(req, res);
|
|
19
27
|
})
|
|
20
28
|
.listen(port, () => {
|
|
21
|
-
console.log(
|
|
29
|
+
console.log(
|
|
30
|
+
"Server started and listening on port " +
|
|
31
|
+
port +
|
|
32
|
+
(options && options.basename
|
|
33
|
+
? " (Basename: " + options && options.basename + ")"
|
|
34
|
+
: "")
|
|
35
|
+
);
|
|
22
36
|
});
|
|
23
37
|
}
|
|
24
38
|
}
|
package/src/index.ts
CHANGED