-
Notifications
You must be signed in to change notification settings - Fork 0
/
restServer.js
145 lines (117 loc) · 3.12 KB
/
restServer.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var restify = require('restify');
var colors = require('colors'); // add colors for fun
var sys = require("sys");
var cp = require('child_process');
var config = require('konphyg')(__dirname + '/config');
var conf = config('main');
var history = {};
sys.puts("Rest Server Started and Setting up".blue);
//SETUP REST SERVER//
var server = restify.createServer();
server.use(restify.bodyParser());
server.use(restify.gzipResponse());
if(conf.allowJSONP){
server.use(restify.jsonp());
sys.puts("REST Server JSONP Active".blue);
}
server.use(restify.queryParser());
server.use(restify.authorizationParser());
if(conf.allowCORS){
server.use(restify.CORS());
sys.puts("REST Server CORS Active".blue);
}
if(conf.allowPreflight)
{
var preflight = require('se7ensky-restify-preflight');
preflight(server);
sys.puts("REST Server Preflight Active".blue);
}
/////
//DECLARE ROUTES//
server.post("/", function(req, res, next){
res.send(200,"OK");
});
server.post('/push', pushData);
server.get('/getHistory',getHistoryList);
server.get('/clearHistory', clearAllHistory);
server.get('/delHistory/:id', clearHistoryItem);
/////
sys.puts("REST Server Routes Declared".blue);
var socketServer;
//START SERVER ON PORT WITH SOCKET SERVER READY//
server.listen(conf.port, function() {
console.log('REST %s Server listening at %s', "Windpush", server.url);
sys.puts("----------------------------------------".blue);
sys.puts("Starting Socket.IO Server".yellow);
socketServer = cp.fork(__dirname + '/socketServer.js');
});
///
///DECLARE ALL ROUTE FUNCTIONS////
function pushData(req, res,next)
{
if(!auth(req.authorization.basic.username,req.authorization.basic.password))
{
res.send(401);
return next;
}
//TODO: Change to send to child Socket IO Server
// io.sockets.emit(req.body.data.id,req.body);
console.log("ABC");
var msgToSend = {type:"broadcast",id:req.body.data.id, body:req.body};
socketServer.send(msgToSend);
res.send(req.body.data);
addToHistory(req.body.data.id, req.body.data);
return next;
}
//
function addToHistory(id, data)
{
history[id] = data;
socketServer.send({type:"history",history:history});
}
function clearAllHistory(req, res, next)
{
if(!auth(req.authorization.basic.username,req.authorization.basic.password))
{
res.send(401);
return next;
}
history = {};
socketServer.send({type: "history", history:{}});
res.send(200);
return next;
}
function clearHistoryItem(req, res, next)
{
if(!auth(req.authorization.basic.username,req.authorization.basic.password))
{
res.send(401);
return next;
}
//history[req.params.id] = null;
delete history[req.params.id];
socketServer.send({type: "history", history:history});
res.send(200);
return next;
}
function getHistoryList(req, res, next)
{
if(!auth(req.authorization.basic.username,req.authorization.basic.password))
{
res.send(401);
return next;
}
res.send(200,history);
return next;
}
function auth(user, pass)
{
if(user == conf.auth.username && pass== conf.auth.password)
{
return true;
}
else
{
return false;
}
}