-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·642 lines (518 loc) · 21.6 KB
/
Copy pathindex.js
File metadata and controls
executable file
·642 lines (518 loc) · 21.6 KB
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
135
136
137
138
139
140
141
142
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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
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
353
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
* @Description: This is the core part of the module which will load all the modules
* specified in the config file
*
* @Author: zhiquan <x.zhiquan@gmail.com>
* @Date: 2021-08-03 08:42:06
* @LastEditTime: 2023-03-28 17:50:33
* @LastEditors: zhiquan
*/
const path = require("path");
const express = require(path.resolve('./') + "/node_modules/express");
const fs = require('fs');
const cookieParser = require("cookie-parser");
const logger = require(path.join(__dirname, "./lib/logger"));
const cache = require('memory-cache');
const morgan = require("morgan");
require('./lib/extend');
const builder = require('./builder');
/**
* Load a module from the system.
*
* @param {Object} app the global app instancee
* @param {String} name name of the module
*/
const _loadModule = function (app, md) {
// load md and it's dependencies
let mPath = "";
let name = "";
if (typeof md === "object") {
mPath = md.path;
name = md.name;
} else {
name = md;
if (app.config.modules.findIndex((m) => m === md) < 0) {
const objMdl = app.config.modules.find((m) => m.name === md);
if (objMdl && objMdl.path) {
mPath = objMdl.path;
}
}
}
if (app.moduleNames.findIndex(m => m === name) >= 0) return;
// try to load module with the order: module in the modules folder, npm module, customer modules
let mdl;
let mdlFromConfig;
let mdlPath;
let errMsg = '';
try {
// in modules folder
try{
mdlFromConfig = require((mPath && `${mPath}/_freemodule.json`) || `${app.projectRoot}/modules/free-be-${name}/_freemodule.json`);
} catch(ex){}
mdl = require(mPath || `${app.projectRoot}/modules/free-be-${name}`);
mdlPath = mPath || `${app.projectRoot}/modules/free-be-${name}`;
} catch (ex) {
errMsg += `\n${ex}\n`;
try {
if ([
'core'
].indexOf(name) >= 0) {
throw new Error('such module should not be loaded from here!')
}
// npm module
try {
mdlFromConfig = require((mPath && `${mPath}/_freemodule.json`) || `${app.projectRoot}/node_modules/free-be-${name}/_freemodule.json`);
} catch (ex) { }
mdl = require(mPath || `${app.projectRoot}/node_modules/free-be-${name}`);
mdlPath = mPath || `${app.projectRoot}/node_modules/free-be-${name}`;
} catch (exx) {
errMsg += `${exx}\n`;
try {
// customer moduels in modules folder
try {
mdlFromConfig = require((mPath && `${mPath}/_freemodule.json`) || `${app.projectRoot}/modules/${name}/_freemodule.json`);
} catch (ex) { }
mdl = require(mPath || `${app.projectRoot}/modules/${name}`);
mdlPath = mPath || `${app.projectRoot}/modules/${name}`;
} catch (exxx) {
errMsg += `${exxx}`;
app.logger.error(
`Failed to load module: ${name}. ${errMsg}`
);
return;
}
}
}
if (!mdl) return;
if (typeof mdl === 'function') mdl = mdl(app);
mdl = Object.merge({}, mdl, mdlFromConfig);
mdl.path = mdlPath;
// attach the app instance to the module instance.
mdl.app = app;
// set the name of the module, in case this name is different from the default one, which means the user changed the name in config.
// so that we can get the real name of the module from any code of itself.
mdl.name = name;
// set the merged config, the final one, of the module to the module instance.
mdl.config = Object.merge({}, mdl.config, app.config[name]);
// add all i18n translations
if (mdl.i18n) {
mdl.t = (v, l) => {
if (!l) l = app.ctx.locale || app.config['defaultLocale'] || 'zh-cn';
if (typeof v === 'string')
return mdl.i18n[l] ? (typeof mdl.i18n[l][v] === 'undefined' ? v : mdl.i18n[l][v]) : v;
else if (typeof v === 'object') {
const outObj = {};
Object.keys(v).forEach(s => {
outObj[s] = mdl.t(v[s]);
});
return outObj;
} else {
return v;
}
}
} else {
mdl.t = (v) => {
return v;
}
}
// add the module to the modules list in the app instance.
app.modules[name] = mdl;
mdl.config &&
mdl.config.dependencies &&
mdl.config.dependencies.forEach(d => {
_loadModule(app, d);
});
if (app.moduleNames.indexOf(name) < 0) {
app.moduleNames.push(name);
}
app.logger.debug(`Loaded module ${name}.`);
};
/**
* Run a specific hook function from all modules, in the order according to the dependency relationship.
*
* @param {Object} app the global app instance
* @param {String} name the hook name to be called
*/
const _runHook = function (app, name) {
for (let i = 0; i < app.moduleNames.length; i += 1) {
const m = app.modules[app.moduleNames[i]];
m && m.hooks && m.hooks[name] && m.hooks[name](app, m);
}
};
const _runAsyncHook = async function (app, name) {
for (let i = 0; i < app.moduleNames.length; i += 1) {
const m = app.modules[app.moduleNames[i]];
if (m && m.hooks && m.hooks[name]) {
await m.hooks[name](app, m);
}
}
};
module.exports = {
onBegin: app => {
app.logger = logger;
app.cache = cache;
app.projectRoot = path.resolve('./');
// application context, all context related information can be stored here.
app.ctx = {
version: require(path.resolve('./') + "/package.json").version || '0.0.1',
serviceList: {}
};
app.utils = require(path.resolve('./') + '/utils');
// all configurations stored in app.config, include config for each module which will overwrite the config in the module itself.
app.config = Object.merge(
{},
require(require('path').resolve('./') + "/config/config.default"),
require(`${require('path').resolve('./')}/config/config.${process.env.NODE_ENV}`)
);
// injection
require('./lib/injection')(app);
// load modules, merge configurations, get ordered modules according to the dependency relationship, etc.
app.config.modules = app.config.modules || [];
app.modules = {};
app.moduleNames = [];
app.config.modules.forEach(m => {
_loadModule(app, m);
});
// check each module that we have all the 'followedBy' in the module list
Object.keys(app.modules).forEach(mk => {
const m = app.modules[mk];
let followedBy = [];
m && m.config && (followedBy = m.config.followedBy || []);
const mIndex = app.moduleNames.indexOf(mk);
followedBy.forEach(f => {
if (app.moduleNames.indexOf(f) < mIndex) {
throw new Error(`${f} should be after ${mk} to be loaded!`);
}
});
});
// run onBegin hook of each module, include their dependencies
_runHook(app, "onBegin");
},
loadModules: () => {
// make a complete list of the dependencies and injections of each module
// app.logger.debug(JSON.stringify(app.config.modules));
},
onModulesReady: app => {
// hook!
_runHook(app, "onModulesReady");
// setup global middleware
morgan.token('date', function () {
return new Date().toISOString();
});
morgan.token('status-color', function (req, res) {
// 304 use blue, 4xx and 5xx use red, others use green
if (res.statusCode === 304) {
return '\x1b[34m304\x1b[0m';
}
const colorCode = [
400, // Bad Request
401, // Unauthorized
403, // Forbidden
404, // Not Found
405, // Method Not Allowed
408, // Request Timeout
410, // Gone
418, // I'm a teapot
500, // Internal Server Error
501 // Not Implemented
].includes(res.statusCode) ? 31 : 32; // Red for client/server errors, green otherwise
return `\x1b[${colorCode}m${res.statusCode}\x1b[0m`;
});
app.use(morgan(function (tokens, req, res) {
// from green to red, 0-100ms, 100-500ms, 500ms+
const resTime = parseInt(tokens['response-time'](req, res), 10);
const colorCode = resTime < 100 ? 32 : resTime < 500 ? 33 : 31;
return [
`\x1b[90m${tokens['date'](req, res, 'clf')}\x1b[0m`,
`\x1b[36m${tokens['remote-addr'](req, res)}\x1b[0m`,
tokens.method(req, res),
tokens.url(req, res),
tokens['status-color'](req, res),
tokens.res(req, res, 'content-length') || '-',
`\x1b[${colorCode}m${resTime}ms\x1b[0m`,
// tokens['user-agent'](req, res),
(req.user && req.user.id) ? req.user.id : '-'
].join(' ')
}));
app.use(express.json({ limit: app.config['bodySizeLimit'] || "10mb" }));
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// security
require("./lib/security")(app);
// add some common function to response
app.use(async function (req, res, next) {
res.locals = res.locals || {};
res.locals.data = res.locals.data || {};
res.locals.filter = res.locals.filter || {};
res.locals.options = res.locals.options || {};
res.locals.fields = res.locals.fields || [];
// add some function to the response
res.endWithErr = async function (code, msg) {
if (res._headerSent) return;
if (typeof msg === 'number') msg = { code: msg };
this.status(code).send({ msg: msg });
};
res.endWithData = function (data, msg = app.config['defaultResponseMessage'] || "OK") {
if (res._headerSent) return;
this.status(200).send({ data, msg: msg });
};
res.addData = function (data, overwrite = true) {
if (overwrite) {
res.locals.data = data;
} else {
if (typeof data !== 'object' || Array.isArray(data)) {
app.logger.error(`Data should be an object! (${req.originalUrl})`)
}
Object.merge(res.locals.data, data);
}
};
res.Module = (n) => {
if (!n) return undefined;
return res.app.modules && res.app.modules[n];
}
res.makeError = function (code, msg = "", mdl) {
if (typeof msg === 'number') msg = { code: msg };
res.locals.err = { code: code, msg: msg, mdl: mdl };
};
res.logger = logger;
return next();
});
// Handle unhandledRejection and pass error to next middleware
if (!app.__globalExceptionHookInstalled) {
app.__globalExceptionHookInstalled = true;
app.__onUnhandled = (reason) => {
const message = (reason && reason.message) ? reason.message : String(reason);
app.logger.error("Unhandled process exception: " + message);
};
process.on("unhandledRejection", app.__onUnhandled);
process.on("uncaughtException", app.__onUnhandled);
}
app.use(function (req, res, next) {
// Manage to get information from the response too, just like Connect.logger does:
const originalEnd = res.end;
let responseHooksCleaned = false;
function cleanupResponseHooks () {
if (responseHooksCleaned) return;
responseHooksCleaned = true;
res.end = originalEnd;
}
res.enableRawResponse = function () {
res.locals = res.locals || {};
res.locals.rawResponse = true;
return res;
};
res.disableRawResponse = function () {
res.locals = res.locals || {};
delete res.locals.rawResponse;
return res;
};
res.isRawResponseEnabled = function () {
return Boolean(res.locals && res.locals.rawResponse);
};
if (typeof res.once === 'function') {
res.once('finish', cleanupResponseHooks);
res.once('close', cleanupResponseHooks);
}
res.end = function (chunk, encoding, callback) {
const rawResponse = Boolean(this.locals && this.locals.rawResponse);
if (this.statusCode !== 200 && !rawResponse && !(this._headerSent || this.headersSent)) {
// run mws before ending with error
const mws = this.beforeReturnErrorMws || [];
for (let i = 0; i < mws.length; i += 1) {
mws[i](req, res, next);
}
this.beforeReturnErrorMws = [];
}
cleanupResponseHooks();
// Prevent real double-end after stream/response is already finished.
if (this.writableEnded || this.destroyed) {
return this;
}
// Keep old compatibility for framework-managed responses:
// once headers are already sent, later middlewares should not try
// to end/send another normal response.
// if (!rawResponse && (this._headerSent || this.headersSent)) {
// return this;
// }
return originalEnd.call(this, chunk, encoding, callback);
};
return next();
});
// by default canI will always return true;
app.post(`${app.config['baseUrl'] || ''}/can_i`,
(req, res, next) => {
res.addData({ can: true });
return next();
}
);
},
onAsyncModulesInit: app => _runAsyncHook(app, "onAsyncModulesInit"),
onAppReady: app => {
const { buildData } = app.freeBuilder || builder;
// hook!
_runHook(app, "onAppReady");
// init the database table schema if the module has.
Object.keys(app.modules).forEach(k => {
const m = app.modules[k];
if (!m) return;
// build from config
buildData(m);
m.data && app.db && app.db.initModuleSchema && app.db.initModuleSchema(app, m);
})
_runHook(app, "onDBSchemaReady");
// init the database models if the module has.
Object.keys(app.modules).forEach(k => {
const m = app.modules[k];
if (!m) return;
m.data && app.db && app.db.initModuleModel && app.db.initModuleModel(app, m);
})
_runHook(app, "onDBReady");
},
onLoadRouters: app => {
// hook!
_runHook(app, "onLoadRouters");
},
loadRouters: app => {
const { buildPreData, buildApis, buildActions, buildStore, buildForConfig } = app.freeBuilder || builder;
// load router from all the modules, according to the dependency relationship and the order in the config file.
const routeGenerator = require("./lib/routehelper").routeGenerator;
let service_list = [];
for (let i = 0; i < app.moduleNames.length; i += 1) {
const m = app.modules[app.moduleNames[i]];
let moduleServiceList = {};
let moduleService = {};
let mInfo = {};
if (!m) continue;
// build from config
buildPreData(m);
const routeFolderPath = path.join(m.path, "routers");
let routeRoot = (m.config && m.config["routeRoot"]);
if (typeof routeRoot === 'undefined')
routeRoot = m.name || "";
const existRouteRootIndex = service_list.findIndex(s => s.service[routeRoot]);
if (fs.existsSync(routeFolderPath)) {
mInfo = require(routeFolderPath);
// if this module is not a route service and we don't have any other route service with the same route root
// we don't need to load these routers.
if ((!m.config || !m.config['asRouteService']) && existRouteRootIndex < 0) {
continue;
}
const generator = routeGenerator(
app,
m,
`${app.config['baseUrl']}/${routeRoot}`,
routeFolderPath
);
generator(routeFolderPath, moduleServiceList);
}
// build from config
buildApis(m, moduleServiceList, `${app.config['baseUrl']}/${routeRoot}`);
buildActions(m);
buildStore(m);
buildForConfig(m);
// wrap the service list with the module, and set the module level service name if needed.
if (routeRoot) {
moduleService[routeRoot] = {
...moduleServiceList
};
} else {
// route root is not empty
Object.merge(moduleService, moduleServiceList);
}
if (m.config && m.config['asRouteService']) {
// this module is the service root (for routers), so we set the service title as this module
Object.merge(moduleService[routeRoot], {
title: (mInfo && mInfo.title) || m.name || app.moduleNames[i],
description: (mInfo && mInfo.description) || ''
});
}
// if we already have service with the same route root, we need to merge.
if (existRouteRootIndex >= 0) {
const existRouteRoot = service_list[existRouteRootIndex].service;
service_list[existRouteRootIndex].service = Object.merge(existRouteRoot, moduleService);
} else {
// merge the module level service list to the app level service list
service_list.push({ service: moduleService, mdl: m });
}
}
app.service_list = service_list;
app.ctx.serviceList = () => {
const list = {};
const sl = app.service_list;
for (let i = 0; i < sl.length; i += 1) {
const s = sl[i];
Object.merge(list, s.mdl.t(s.service));
}
return list;
};
},
onRoutersReady: app => {
// hook!
_runHook(app, "onRoutersReady");
// real db operations
app.db && app.use(app.db.dataProcessMiddleware);
(app.config['staticFolders'] || []).forEach(s => {
app.use(app.config['assetsUrlPrefix'] || '/assets', express.static(s, app.config['staticOptions'] || {}));
})
// hook!
_runHook(app, "beforeLastMiddleware");
app.use(function request_error_handler (err, req, res, next) {
if (!err) return next();
const reasonMsg = (err && err.message) ? err.message : String(err);
logger.error("Request exception: " + reasonMsg);
logger.error(req.originalUrl);
if (res.headersSent || res.writableEnded || res.destroyed) {
return next(err);
}
res.makeError(
500,
req.app.get("env") === "production"
? "System error, please contact with the system administrator!"
: reasonMsg
);
return next();
});
// return data to client or catch error and forward to error handler
app.use(function last_catch_middleware (req, res, next) {
// return client request
if (res._headerSent) {
return next();
}
let code = 0,
msg = "",
data = {};
if (res.locals.err) {
code = res.locals.err.code;
msg = res.locals.err.msg;
} else if (res.locals.data) {
code = 200;
data = res.locals.data;
msg = res.locals.msg;
}
code = code || 404;
if (code === 404) {
if (req.originalUrl.startsWith(app.config['assetsUrlPrefix'] ? app.config['assetsUrlPrefix'] + '/' : '/assets/')) code = 200;
}
if (code !== 200) {
// run mws before ending with error
const mws = res.beforeReturnErrorMws || []
for(let i = 0; i < mws.length; i += 1) {
mws[i](req, res, next);
}
res.beforeReturnErrorMws = [];
}
if (res._headerSent) return;
let returnData = (code === 200)
? { data, msg: (msg || app.config['defaultResponseMessage'] || "OK") }
: { msg };
returnData = Object.assign({}, returnData, res.locals.persData);
res.status(code).send(returnData);
res.locals.return = {
code, returnData
}
return next();
});
// hook!
_runHook(app, "afterLastMiddleware");
}
};