100 lines
2.3 KiB
JavaScript
100 lines
2.3 KiB
JavaScript
var debug = require('debug')('ff:tcrud:build:server:express');
|
|
|
|
var mod = (function () {
|
|
|
|
var walkRouteTree = function(result,stack,parent_path) {
|
|
if (parent_path == null) {
|
|
parent_path = '';
|
|
}
|
|
stack.forEach(function(middleware) {
|
|
if (middleware.route){
|
|
var path = parent_path + middleware.route.path;
|
|
for (var httpMethod in middleware.route.methods) {
|
|
var data = {
|
|
uriPath: path,
|
|
httpMethod: httpMethod
|
|
};
|
|
result.push(data);
|
|
}
|
|
} else if (middleware.name === 'router') {
|
|
var pp = parent_path + middleware.handle.path;
|
|
walkRouteTree(result,middleware.handle.stack,pp);
|
|
} else {
|
|
//log.info('route err: '+JSON.stringify(middleware));
|
|
}
|
|
});
|
|
return result;
|
|
}
|
|
|
|
var buildRouteLine = function(routes) {
|
|
var result = [];
|
|
for (var routeIdx in routes) {
|
|
|
|
}
|
|
return result;
|
|
}
|
|
|
|
var buildRouteDiff = function(obj,objOld) {
|
|
var result = [];
|
|
for (var objIdx in obj) {
|
|
var objResult = obj[objIdx];
|
|
var objAdd = true;
|
|
for (var objOldIdx in objOld) {
|
|
var objOldResult = objOld[objOldIdx];
|
|
|
|
if (objResult.uriPath === objOldResult.uriPath && objResult.httpMethod === objOldResult.httpMethod) {
|
|
objAdd = false;
|
|
break;
|
|
}
|
|
}
|
|
if (objAdd) {
|
|
result.push(objResult);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
return function BuildServerExpress() {
|
|
|
|
this.saveRoutes = function(server) {
|
|
if (!server) {
|
|
throw new Error('no server given');
|
|
}
|
|
var resultOld = server.get('ff_tcrud_route_list');
|
|
var result = walkRouteTree([],server._router.stack);
|
|
result.sort(function(a, b) {
|
|
return a.uriPath.localeCompare(b.uriPath);
|
|
});
|
|
server.set('ff_tcrud_route_list',result);
|
|
server.set('ff_tcrud_route_list_diff',buildRouteDiff(result,resultOld));
|
|
};
|
|
|
|
this.getRoutes = function(server) {
|
|
return server.get('ff_tcrud_route_list');
|
|
};
|
|
|
|
this.getRoutesDiff = function(server) {
|
|
return server.get('ff_tcrud_route_list_diff');
|
|
};
|
|
|
|
this.renderRedirect = function(location) {
|
|
if (!location) {
|
|
throw 'no location';
|
|
}
|
|
return function(req, res) {
|
|
res.redirect(location);
|
|
};
|
|
};
|
|
|
|
this.renderFunctionJSON = function(fn) {
|
|
return function (req, res, next) {
|
|
res.json({
|
|
data: fn()
|
|
});
|
|
};
|
|
};
|
|
};
|
|
})();
|
|
|
|
module.exports = new mod();
|