-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
90 lines (76 loc) · 2.4 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
var path = require("path");
var fs = require("fs");
var port = process.env.PORT || 8080;
var ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'
function processStatic(resp) {
var filename = path.join(process.cwd(), resp.uri);
fs.exists(filename, function(exists) {
if (!exists) {
resp.sendText(404, "404 - Not Found");
return;
}
if (fs.statSync(filename).isDirectory())
filename = path.join(filename, 'index.html');
fs.readFile(filename, "binary", function(err, file) {
if (err) {
resp.sendText(500, err);
}
var contentType = resp.header(filename);
resp.sendSuccess(file, contentType);
});
});
}
function pong(result) {
result.sendText(200, 'pong');
}
function processService(resp) {
resp.onReceivedData(function(body){
try{
var service = require("./server/" + resp.urlArg(2) + "Service");
var func = service[resp.urlArg(3)];
if(!func){
resp.sendText(500, "Method not found");
return;
}
if(body){
body = JSON.parse(body);
}
var ret = func.apply(service, [ body ]);
ret.done(function(result){
result = JSON.stringify(result);
resp.sendSuccess(result || 'void', 'text/json');
});
}catch(e){
console.log(e);
resp.sendText(500, "Service not found");
return;
}
});
}
function mongoReset (resp, chain){
require('./server/MongoHelper').init(function() {
var mongoHelper = require('./server/MongoHelper');
if (!mongoHelper.db) {
console.log("DB is undefined");
resp.sendText(500, "Sorry!! Something went wrong!!!");
return;
}
chain.continue();
});
}
function clearRequireCache(resp, chain){
for(var i in require.cache){
delete require.cache[i];
}
chain.continue();
}
var serv = require('./server/controller/Server').server();
if(process.argv[2] == "clearCache"){
console.log("Clear Require Cache Active");
serv.before('/', clearRequireCache);
}
serv.action('/ping', pong);
serv.action('/s', processService);
serv.before('/s', mongoReset);
serv.action('/', processStatic);
serv.listen(parseInt(port, 10), ip_address);