2
0
Fork 0
tcrud/lib/plugin/server/config/config-routes.js

69 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2017-06-10 14:53:29 +00:00
var buildServerExpress = require('./../../../build-server-express');
module.exports = (function () {
var renderServerRoutes = function (server) {
if (!server) {
throw new Error('no server');
}
return function (req, res, next) {
var routeList = buildServerExpress.getRoutes(server);
if (!routeList) {
routeList = [];
}
var reqGroups = req.query.groups;
var groupList = [];
if (reqGroups) {
groupList = reqGroups.split(',');
}
var result = {
all: [],
};
for (var i = 0; i < groupList.length; i++) {
var groupName = groupList[i].split('/').join('_');
result[groupName] = [];
}
groupList.sort(function(a, b) {
return a.length < b.length; // longest first as we break on first hit
});
for (i = 0; i < routeList.length; i++) {
var route = routeList[i];
var added = false;
for (var ii = 0; ii < groupList.length; ii++) {
if (route.uriPath.indexOf(groupList[ii]) > 0) {
var groupName = groupList[ii].split('/').join('_');
result[groupName].push(route);
added = true;
break;
}
}
if (!added) {
result.all.push(route);
}
}
res.json({
data: result
});
};
}
return function ServerConfigRoutesPlugin() {
this.configPlugin = function (ctx) {
ctx.key = 'serverConfigRoutes';
ctx.description = 'Exports all server routes.';
ctx.localDir = __dirname;
ctx.localConfigTemplate = 'config-routes.json';
};
this.configServer = function(ctx) {
ctx.server.get(ctx.createSlugApiServerBase(),renderServerRoutes(ctx.server));
};
this.configPostBoot = function(ctx) {
buildServerExpress.saveRoutes(ctx.server);
};
};
})();