Optimize the creation of handlers for rest
This commit is contained in:
parent
e0e9d6ecff
commit
982e495632
|
@ -23,27 +23,27 @@ module.exports = rest;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function rest() {
|
function rest() {
|
||||||
|
var handlers; // Cached handlers
|
||||||
|
|
||||||
return function restApiHandler(req, res, next) {
|
return function restApiHandler(req, res, next) {
|
||||||
var app = req.app;
|
var app = req.app;
|
||||||
var restHandler = app.handler('rest');
|
|
||||||
|
|
||||||
if (req.url === '/routes') {
|
if (req.url === '/routes') {
|
||||||
return res.send(restHandler.adapter.allRoutes());
|
return res.send(app.handler('rest').adapter.allRoutes());
|
||||||
} else if (req.url === '/models') {
|
} else if (req.url === '/models') {
|
||||||
return res.send(app.remotes().toJSON());
|
return res.send(app.remotes().toJSON());
|
||||||
}
|
}
|
||||||
|
|
||||||
var preHandlers;
|
if (!handlers) {
|
||||||
|
handlers = [];
|
||||||
if (!preHandlers) {
|
|
||||||
preHandlers = [];
|
|
||||||
var remotingOptions = app.get('remoting') || {};
|
var remotingOptions = app.get('remoting') || {};
|
||||||
|
|
||||||
var contextOptions = remotingOptions.context;
|
var contextOptions = remotingOptions.context;
|
||||||
if (contextOptions !== false) {
|
if (contextOptions !== false) {
|
||||||
if (typeof contextOptions !== 'object')
|
if (typeof contextOptions !== 'object') {
|
||||||
contextOptions = {};
|
contextOptions = {};
|
||||||
preHandlers.push(loopback.context(contextOptions));
|
}
|
||||||
|
handlers.push(loopback.context(contextOptions));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (app.isAuthEnabled) {
|
if (app.isAuthEnabled) {
|
||||||
|
@ -54,11 +54,18 @@ function rest() {
|
||||||
// https://github.com/strongloop/loopback/pull/167
|
// https://github.com/strongloop/loopback/pull/167
|
||||||
// https://github.com/strongloop/loopback/commit/f07446a
|
// https://github.com/strongloop/loopback/commit/f07446a
|
||||||
var AccessToken = loopback.getModelByType(loopback.AccessToken);
|
var AccessToken = loopback.getModelByType(loopback.AccessToken);
|
||||||
preHandlers.push(loopback.token({ model: AccessToken }));
|
handlers.push(loopback.token({ model: AccessToken }));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async.eachSeries(preHandlers.concat(restHandler), function(handler, done) {
|
handlers.push(function(req, res, next) {
|
||||||
|
// Need to get an instance of the REST handler per request
|
||||||
|
return app.handler('rest')(req, res, next);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (handlers.length === 1) {
|
||||||
|
return handlers[0](req, res, next);
|
||||||
|
}
|
||||||
|
async.eachSeries(handlers, function(handler, done) {
|
||||||
handler(req, res, done);
|
handler(req, res, done);
|
||||||
}, next);
|
}, next);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue