-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
212 lines (183 loc) · 3.88 KB
/
app.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var sys = require('sys');
var asciimo = require('asciimo').Figlet;
var colors = require('colors'); // add colors for fun
var font = 'Thin';
var welcome = "Raindrops Server";
asciimo.write(welcome,font,function(art){
sys.puts(art.green);
});
var fq = require("./lib/fileQueue.js");
var restify = require("restify");
var notify = require("./lib/notify.js");
var config = require('konphyg')('./config');
var serverConfigs = config("main");
var blockops = require('./lib/blockops.js');
var ips = serverConfigs.ips;
function length(req, res, next)
{
fq.length(function(len){
res.send(200,len);
});
return next;
}
function pushQ(req, res, next)
{
if(!(req.params.data === undefined))
{
fq.push(req.params.data);
res.send(201);
notify.notify();
}
else
{
res.send(406,"Data Json Object must be spesified");
}
return next;
}
function popQ(req, res, next)
{
fq.pop(function(data){
if(data == null)
{
res.send(204);
}
else
{
res.send(200,data);
}
});
return next;
}
function tPopQ(req, res, next)
{
fq.tpop(function(data){
if(data == null)
{
res.send(204);
}
else
{
res.send(200,data);
}
});
return next;
}
function commitKey(req, res, next)
{
fq.commit(req.params.key, function(err){
if(err == false)
{
res.send(200);
}
else if (err = -1)
{
res.send(404,"Key Not Found");
}
else
{
res.send(500, "Key was not commited, error occured, " + err.toString());
}
});
return next;
}
function rollBack(req, res, next)
{
fq.rollback(req.params.key, function(err){
if(err == false)
{
res.send(200);
}
else if (err = -1)
{
res.send(404,"Key Not Found");
}
else
{
res.send(500, "Key was not commited, error occured, " + err.toString());
}
});
}
function clearAll(req, res, next)
{
blockops.block();
fq.clearAll(function(resp){
if(resp)
{
blockops.unBlock(function(resp){
if(resp)
{
res.send(200,"Cleared All Data, Server is unblocked");
}
});
}
});
return next;
}
function commitAll(req, res, next){
blockops.block();
fq.commitAll(function(resp){
if(resp)
{
blockops.unBlock(function(resp){
res.send(200,"All Items Commited, Block removed from server");
});
}
});
};
function rollbackAll(req, res, next)
{
blockops.block();
fq.rollbackAll(function(err){
if(err)
{
blockops.unBlock();
send.res(500,"Can not rollback, error has occured");
}
else
{
blockops.unBlock(function(resp){
if(resp){
res.send(200,"All Items rolledback, Block on reuests removed");
}
else
{
res.send(500, "An Error has occured");
}
});
}
})
return next;
}
var server = restify.createServer();
server.use(restify.bodyParser())
server.use(restify.jsonp());
server.pre(function(req, res, next) {
if((ips.indexOf(req.connection.remoteAddress)) == -1)
{
res.send(403,"Remote Address not allowed");
}
if (blockops.isBlocked())
{
res.send(500,"Server currently Blocked for a Transactional Process");
}
return next();
});
server.use(restify.jsonp());
server.use(restify.gzipResponse());
server.post('/push', pushQ);
server.get('/pop', popQ);
server.get('/tpop', tPopQ);
server.get('/commit/:key',commitKey);
server.get('/rollback/:key', rollBack);
server.get('/length', length);
server.get('/clearAll',clearAll);
server.get('/commitAll', commitAll);
server.get('/rollbackAll', rollbackAll);
fq.init(function(){
server.listen(serverConfigs.port, function() {
console.log('%s listening at %s', "Raindrops Server", server.url);
//Server Start Notify with push
notify.notify();
blockops.unBlock(function(resp){});
});
});