This repository was archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathhandlerService.js
More file actions
106 lines (100 loc) · 3.06 KB
/
Copy pathhandlerService.js
File metadata and controls
106 lines (100 loc) · 3.06 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
var fs = require('fs');
var utils = require('../../util/utils');
var Loader = require('pomelo-loader');
var pathUtil = require('../../util/pathUtil');
var logger = require('pomelo-logger').getLogger('pomelo', __filename);
var forwardLogger = require('pomelo-logger').getLogger('forward-log', __filename);
/**
* Handler service.
* Dispatch request to the relactive handler.
*
* @param {Object} app current application context
*/
var Service = function(app, opts) {
this.app = app;
this.handlerMap = {};
if(!!opts.reloadHandlers) {
watchHandlers(app, this.handlerMap);
}
};
module.exports = Service;
Service.prototype.name = 'handler';
/**
* Handler the request.
*/
Service.prototype.handle = function(routeRecord, msg, session, cb) {
// the request should be processed by current server
var handler = this.getHandler(routeRecord);
if(!handler) {
logger.error('[handleManager]: fail to find handler for %j', msg.__route__);
utils.invokeCallback(cb, new Error('fail to find handler for ' + msg.__route__));
return;
}
var start = Date.now();
try{
handler[routeRecord.method](msg, session, function(err, resp, opts) {
var log = {
route : msg.__route__,
args : msg,
time : utils.format(new Date(start)),
timeUsed : new Date() - start
};
if(typeof resp === 'undefined' && this.app.get('enfrceHandlerResponse')) resp = {};
forwardLogger.info(JSON.stringify(log));
utils.invokeCallback(cb, err, resp, opts);
});
}catch(err){
if(this.app.get('catchHandlerErrors')){
process.nextTick(function(){
var opts;
utils.invokeCallback(cb, err, {}, opts);
});
}else{
throw err;
}
}
return;
};
/**
* Get handler instance by routeRecord.
*
* @param {Object} handlers handler map
* @param {Object} routeRecord route record parsed from route string
* @return {Object} handler instance if any matchs or null for match fail
*/
Service.prototype.getHandler = function(routeRecord) {
var serverType = routeRecord.serverType;
if(!this.handlerMap[serverType]) {
loadHandlers(this.app, serverType, this.handlerMap);
}
var handlers = this.handlerMap[serverType] || {};
var handler = handlers[routeRecord.handler];
if(!handler) {
logger.warn('could not find handler for routeRecord: %j', routeRecord);
return null;
}
if(typeof handler[routeRecord.method] !== 'function') {
logger.warn('could not find the method %s in handler: %s', routeRecord.method, routeRecord.handler);
return null;
}
return handler;
};
/**
* Load handlers from current application
*/
var loadHandlers = function(app, serverType, handlerMap) {
var p = pathUtil.getHandlerPath(app.getBase(), serverType);
if(p) {
handlerMap[serverType] = Loader.load(p, app);
}
};
var watchHandlers = function(app, handlerMap) {
var p = pathUtil.getHandlerPath(app.getBase(), app.serverType);
if (!!p){
fs.watch(p, function(event, name) {
if(event === 'change') {
handlerMap[app.serverType] = Loader.load(p, app);
}
});
}
};