-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-resources.js
86 lines (82 loc) · 2.53 KB
/
rest-resources.js
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
var fs = require('fs');
var configure = require(__dirname + '/configure');
module.exports = {
routeFile: function(router,path,endpointBase) {
if (endpointBase===undefined) {
endpointBase = path.replace(__dirname, "");
router.get(endpointBase, function(req,res) {
res.status(403).send("Forbidden");
});
endpointBase = endpointBase.replace(".js","");
}
else {
var filePath = path.replace(__dirname + '/plugins', "");
// Prevent publish javascript file
router.get(filePath, function(req,res) {
res.status(403).send("Forbidden");
});
}
var routes = require(path).routes;
if (routes) {
for (var key in routes) {
var route = routes[key];
var method = null;
var callback = null;
var param = route.param;
for (method in route) {
var value = route[method];
if (typeof(value)=="function" || typeof(value)=="object") {
callback = value;
break;
}
}
var endpoint = endpointBase;
if (param) {
endpoint += '/:' + param;
}
console.log(endpoint + " > " + method);
router[method](endpoint,callback);
}
}
else {
console.log("Warning (" + endpointBase + "): No routes found");
}
},
routeDirectory: function(router,path,pluginName) {
var dirName = path.split('/').pop();
if (!fs.existsSync(path)) return;
var dir = fs.readdirSync(path);
var This = this;
dir.forEach(function(entry) {
var itemPath = path + "/" + entry;
var stats = fs.lstatSync(itemPath);
if (stats.isDirectory()) {
This.routeDirectory(router,itemPath);
}
else {
if (pluginName) {
var pluginEndpoint = configure.config && configure.config.plugins ? configure.config.plugins.endpoint:'/rest/plugins/';
endpoint = pluginEndpoint + pluginName + '/' + entry.split('.')[0];
This.routeFile(router,itemPath,endpoint);
}
else {
This.routeFile(router,itemPath);
}
}
});
},
routePlugins:function(router,pluginsPath) {
if (!fs.existsSync(pluginsPath)) return;
var pluginDir = fs.readdirSync(pluginsPath);
var This = this;
pluginDir.forEach(function(entry) {
var pluginPath = pluginsPath + "/" + entry + "/rest";
if (fs.existsSync(pluginPath)) {
var stats = fs.lstatSync(pluginPath);
if (stats.isDirectory()) {
This.routeDirectory(router,pluginPath,entry);
}
}
});
}
}