2013-05-24 14:59:59 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2013-07-16 17:42:47 +00:00
|
|
|
var loopback = require('../loopback');
|
2013-05-24 14:59:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export the middleware.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = rest;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a temp app for mounting resources.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function rest() {
|
2014-02-04 15:17:32 +00:00
|
|
|
var tokenParser = null;
|
2013-05-24 14:59:59 +00:00
|
|
|
return function (req, res, next) {
|
2013-07-17 21:29:43 +00:00
|
|
|
var app = req.app;
|
2013-07-25 23:24:00 +00:00
|
|
|
var handler = app.handler('rest');
|
2014-02-04 15:17:32 +00:00
|
|
|
|
2013-05-24 14:59:59 +00:00
|
|
|
if(req.url === '/routes') {
|
|
|
|
res.send(handler.adapter.allRoutes());
|
|
|
|
} else if(req.url === '/models') {
|
2014-02-19 23:14:31 +00:00
|
|
|
return res.send(app.remotes().toJSON());
|
2014-02-04 15:17:32 +00:00
|
|
|
} else if (app.isAuthEnabled) {
|
|
|
|
if (!tokenParser) {
|
|
|
|
// NOTE(bajtos) It would be better to search app.models for a model
|
|
|
|
// of type AccessToken instead of searching all loopback models.
|
|
|
|
// Unfortunately that's not supported now.
|
|
|
|
// Related discussions:
|
|
|
|
// https://github.com/strongloop/loopback/pull/167
|
|
|
|
// https://github.com/strongloop/loopback/commit/f07446a
|
|
|
|
var AccessToken = loopback.getModelByType(loopback.AccessToken);
|
|
|
|
tokenParser = loopback.token({ model: AccessToken });
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenParser(req, res, function(err) {
|
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
} else {
|
|
|
|
handler(req, res, next);
|
|
|
|
}
|
|
|
|
});
|
2013-05-24 14:59:59 +00:00
|
|
|
} else {
|
|
|
|
handler(req, res, next);
|
|
|
|
}
|
2014-02-04 15:17:32 +00:00
|
|
|
};
|
2013-05-24 14:59:59 +00:00
|
|
|
}
|
|
|
|
|