-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
359 lines (282 loc) · 10.2 KB
/
server.js
File metadata and controls
359 lines (282 loc) · 10.2 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env node
/**
* @file run_server.js
* @author Benjamin Barbesange && Benoît Garçon
*
* @brief This file creates the web apps and handle the routing.
*/
/// Packages imports
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var winston = require('winston');
/// Service files imports
var service = require('./service.methods.js');
var serviceEntities = require('./service.entities.js');
/// Configuration of constants
var SERVER_PORT = process.env.PORT || 80;
// Set the title to stop it more simply
process.title = "watchdogzz";
/******************************************************************************/
// Config winston
/******************************************************************************/
/* Info: winston levels
{ error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5 }
*/
winston.configure({
exitOnError: false,
transports: [
/*
Errors are in a separated file
*/
new (winston.transports.File)({
name: 'error-file',
level: 'error',
filename: './logs/server-error.log',
maxsize: 5242880, // 5MB
json: false,
handleExceptions: true,
humanReadableUnhandledException: true
}),
/*
Info file will contain info and errors
*/
new (winston.transports.File)({
name: 'info-file',
level: 'info',
filename: './logs/server-info.log',
maxsize: 5242880, // 5MB
json: false
}),
/*
Debug file will contain anything
*/
new (winston.transports.File)({
name: 'debug-file',
level: 'debug',
filename: './logs/server-debug.log',
maxsize: 5242880, // 5MB
json: false
})
]
});
/******************************************************************************/
// Config app
/******************************************************************************/
/// Create the app
var app = express();
app.locals.title = 'WatchDogZZ Web service';
app.set('port', SERVER_PORT);
app.use(bodyParser.json()); // Parse JSON and place it in body
// Middleware for each request
// Adding header properties
// app.use(function(req, res, next) {
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
// next();
// });
/******************************************************************************/
// Errors
/******************************************************************************/
const ERROR_503 = {
'status': 'fail',
'error': 'Database error'
}
const ERROR_400 = {
'status': 'fail',
'error': 'Internal server error'
}
/******************************************************************************/
// Routing
/******************************************************************************/
// GET '/'
// Handle get request on root
// Return simple html with greetings
app.get('/', function (request, response) {
response.send(`
<html>
<body>
<h1>Welcome to WatchDogZZ web service home!</h1>
</body>
</html>
`);
response.end();
});
// POST '/login'
// Will parse the JSON body and create the user in the db
// If the user uses other methods without the login, it will receive an error
app.post('/login', function (request, response) {
// Get required parameters
var name = request.body.name || null;
var location = request.body.location || null;
var token = request.body.token || null;
// Get optional parameters
var email = request.body.email || null;
var photo = request.body.photo || null;
// Check the parameters
if (null != name && null != location && null != token) {
// Create user with required parameters
var user = new serviceEntities.User(name, location, token);
// Adding optional fields
if (null != email) { user.setEmail(email); }
if (null != photo) { user.setPhoto(photo); }
service.login(user, function (err, res) {
if (null == err) {
response.status(200);
response.send({
'status': 'ok',
'id': res['_id'] // TODO: get the id oh the created user
});
} else {
response.status(503);
response.send(ERROR_503);
winston.error('Error 503 on login request', request.ip, request.headers, JSON.stringify(request.body));
}
response.end();
});
} else {
response.status(400)
response.send(ERROR_400);
winston.error('Error 400 on login request ', request.ip, request.headers, JSON.stringify(request.body));
response.end();
}
});
// POST '/logout'
// Parse the JSON body and delete the user from the database
// The user will disapear from the map on the phone
app.post('/logout', function (request, response) {
// Get parameters
var token = request.body.token || null;
if (null != token) {
service.logout(token, function (err, res) {
if (null == err) {
response.status(200);
response.send({
'status': 'ok'
});
} else {
response.status(503);
response.send(ERROR_503);
winston.error('Error 503 on logout request ', request.ip, request.headers, JSON.stringify(request.body));
}
response.end();
});
} else {
response.status(400);
response.send(ERROR_400);
winston.error('Error 400 on logout request ', request.ip, request.headers, JSON.stringify(request.body));
response.end();
}
});
// GET '/where/user'
// Return the location of all users connected
app.get('/where', function (request, response) {
service.getUserList(function (err, serviceList) {
if (null == err) {
response.status(200);
response.send({
'status': 'ok',
'list': serviceList
});
} else {
response.status(503);
response.send(ERROR_503);
winston.error('Error 503 on where request', request.ip, request.headers, JSON.stringify(request.body));
}
response.end();
});
});
// POST '/where'
// Update the user position
// Pass a JSON containing the name, and the location
app.post('/where', function (request, response) {
var name = request.body.name || null;
var location = request.body.location || null;
var token = request.body.token || null;
/*
If the user is already connected, just update the location
If the user is not connected, log him (but he will have the default
information for optional fields)
*/
if (null != location && null != token) {
service.checkToken(token, function (err, res) {
if (false == res) {
// Not connected
if (null != name) {
// We can login be cause we have the correct information
var user = new serviceEntities.User(name, location, token);
service.login(user, function (err, res) {
if (null == err) {
response.status(200);
response.send({
'status': 'ok'
});
} else {
response.status(503);
response.send(ERROR_503);
winston.error('Error 503 on login with where request ', request.ip, request.headers, JSON.stringify(request.body));
}
response.end();
});
} else {
// Notify the user we cant handle the request
response.status(400);
response.send(ERROR_400);
response.end();
winston.error('Error 400 on where request ', request.ip, request.headers, JSON.stringify(request.body));
}
} else {
// Connected
service.updateUserLocation(token, location, function (err, res) {
if (null == err) {
response.status(200);
response.send({
'status': 'ok'
});
} else {
response.status(503);
response.send(ERROR_503);
winston.error('Error 503 on where request ', request.ip, request.headers, JSON.stringify(request.body));
}
response.end();
});
}
});
} else {
response.status(400);
response.send(ERROR_400);
response.end();
winston.error('Error 400 on where request ', request.ip, request.headers, JSON.stringify(request.body));
}
});
// GET '/users'
// Return the list of all connected users
app.get('/who', function (request, response) {
service.getUsernameList(function (err, res) {
if (null == err) {
response.status(200);
response.send({
'status': 'ok',
'list': res
});
response.end();
} else {
response.status(200);
response.send({
'status': 'ok',
'list': res
});
response.end();
}
});
});
/******************************************************************************/
// Create the http server with the app
/******************************************************************************/
// The main http app
winston.info('Server is starting');
http.createServer(app).listen(app.get('port'), function () {
// When the app is running
winston.info('Server running on port ' + app.get('port'));
});