This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
server.js
107 lines (97 loc) · 3.37 KB
/
server.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright 2012 Research In Motion Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var DEFAULT_SERVICE = "default",
DEFAULT_ACTION = "exec";
function rebuildRequest(req) {
var originalURL = req.params.service + "/" +
req.params.action +
(req.params.ext ? "/" + req.params.ext : "") +
(req.params.method ? "/" + req.params.method : "") +
(req.params.args ? "?" + req.params.args : ""),
tokens = originalURL.split('/'),
//Handle the case where the method is multi-level
finalToken = (tokens[1] && tokens.length > 2) ? tokens.slice(1).join('/') : tokens[1],
args,
params;
// set args
if (finalToken && finalToken.indexOf("?") >= 0) {
// GET querystring to json
params = finalToken.split("?")[1].split("&");
if (params) {
args = {};
params.forEach(function (param) {
var parts = param.split("=");
args[parts[0]] = parts[1];
});
}
} else {
// POST body to json
if (req.body) {
args = JSON.parse(req.body);
}
}
return {
params : {
service : DEFAULT_SERVICE,
action : DEFAULT_ACTION,
ext : tokens[0],
method : (finalToken && finalToken.indexOf("?") >= 0) ? finalToken.split("?")[0] : finalToken,
args : args || null
},
body : req.body,
origin : req.origin
};
}
module.exports = {
handle: function (req, res) {
try {
var pluginName = "lib/plugins/" + req.params.service,
plugin;
if (frameworkModules.indexOf(pluginName + ".js") === -1) {
pluginName = "lib/plugins/" + DEFAULT_SERVICE;
req = rebuildRequest(req);
}
//Updating because some versions of node only work with relative paths
pluginName = pluginName.replace('lib', '.');
plugin = require("./utils").loadModule(pluginName);
plugin[req.params.action](req,
function (result) {
res.send(200, encodeURIComponent(JSON.stringify({
code: 1,
data: result
})));
},
function (code, error, httpCode) {
if (!httpCode) {
httpCode = 200;
}
res.send(httpCode, encodeURIComponent(JSON.stringify({
code: Math.abs(code) * -1 || -1,
data: null,
msg: error
})));
},
req.params.args,
{
"request": req,
"response": res
});
} catch (e) {
console.log(e);
res.send(404, "can't find the stuff");
}
}
};