-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
134 lines (115 loc) · 3.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
const http = require('http');
const fs = require('fs');
const path = require('path');
const hooks = require('./hooks');
const allowCors = require("./hooks/allow-cors");
class Zinky {
get defaultSettings() {
return {
port: 3000,
aliases: {},
staticModuleName: "file",
staticFolder: "public",
logRequestDate: false,
stopPugLayout: false,
logPoweredBy: true,
logRequest: true,
env: "development",
}
}
constructor(settings = {}) {
Object.assign(this, this.defaultSettings, settings);
this.hooks = hooks(this);
if (settings.allowCors) this.addHook(allowCors);
for (var key in settings) {
if (settings.hasOwnProperty(key) && !this.hasOwnProperty(key)) {
this[key] = settings[key];
}
}
this.server = http.createServer((req, res) => {
this.handleRequest(req, res)
});
if (!settings.patientMode) this.loadModules();
}
loadModules() {
var modules = {};
var dir = fs.readdirSync('./app_modules');
dir.forEach((moduleName) => {
var modulePath = path.resolve('.', 'app_modules', moduleName);
var stat = fs.lstatSync(modulePath);
if (stat.isDirectory()) {
try {
var importedClass = require(modulePath);
modules[moduleName] = new importedClass(modulePath, this);
} catch (error) {
console.warn(error);
}
}
});
this.modules = modules;
this.mds = this.modules;
}
promisify(fn) {
return (req, res) => new Promise(done => fn(req,res, () => { done() }))
}
addHook(fn) {
this.hooks.splice(-4, 0, fn);
}
addHookFromModule(mdName, fn) {
this.addHook(this.mds[mdName][fn].bind(this.mds[mdName]));
}
addTardyHook(fn) {
this.hooks.splice(-1, 0, fn);
}
addTardyHookFromModule(mdName, fn) {
this.addTardyHook(this.mds[mdName][fn].bind(this.mds[mdName]));
}
catcher(req, res) {
console.log(req.error);
res.deliver(500, 'Internal Server Error');
}
async runORCatch(fn, req, res, explicitParams) {
try {
let r = await fn(req, res, ...(explicitParams || req.params || []));
if (r !== undefined) res.send(r);
} catch (e) {
req.error = e;
this.catcher(req, res);
}
}
async handleRequest(req, res) {
req.app = this;
req.A = req.app;
res.on('finish', () => { this.onFinishRequest(req, res); })
let index = 0;
while (index < this.hooks.length && !res.finished) {
await this.runORCatch(this.hooks[index], req, res);
index++;
}
}
async onFinishRequest(req, res) {
const hookName = 'AFTER_' + req.moduleName + '_' + req.operation;
for (var moduleName in req.A.modules) {
if (req.A.modules[moduleName][hookName]) {
let m = req.A.modules[moduleName];
this.runORCatch(m[hookName].bind(m), req, res)
}
}
const hName = `AFTER_${req.operation}`;
if(!req.module) return;
if(req.module[hName])
this.runORCatch(req.module[hName].bind(req.module), req, res)
const ghostHName = `AFTER_${req.ghostOperation}`;
if(!req.module[ghostHName]) return;
const [, ...ghostParams] = req.params;
const method = req.module[ghostHName];
this.runORCatch(method.bind(req.module), req, res, ghostParams)
}
listen(port) {
if (port) this.port = port;
this.server.listen(this.port, () => {
console.log("Server listening on: http://localhost:%s", this.port);
});
}
}
module.exports = Zinky;