-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
103 lines (95 loc) · 2.01 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
var path = require('path');
var express = require('express');
var validate = require('jsonschema').validate;
var nikeplus = require('./nikeplus');
var app = express();
app.configure(function() {
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, 'public')));
});
var schema = {
type: 'object',
properties: {
email: {
type: 'string',
required: true,
},
password: {
type: 'string',
required: true,
},
date: {
type: 'object',
required: true,
properties: {
year: {
type: 'integer',
required: true,
maximum: 9999
},
month: {
type: 'integer',
required: true,
minimum: 1,
maximum: 12,
},
day: {
type: 'integer',
required: true,
minimum: 1,
maximum: 31,
},
hour: {
type: 'integer',
required: true,
minimum: 0,
maximum: 23,
},
minute: {
type: 'integer',
required: true,
minimum: 0,
maximum: 59,
}
}
},
duration: {
type: 'integer',
required: true,
minimum: 0,
},
distance: {
type: 'number',
required: true,
minimum: 0,
},
},
};
app.post('/', function(req, res) {
// This is so disgusting. Just give me the damn raw data already.
try {
var data = JSON.parse(req.body.data);
} catch (e) {
res.json({ success: false, reason: 'invalid_message' });
return;
}
var validation = validate(data, schema);
if (validation.length !== 0) {
res.json({ success: false, reason: 'invalid_message' });
return;
}
var email = data.email;
var password = data.password;
var date = new Date(data.date.year, data.date.month - 1, data.date.day, data.date.hour, data.date.minute);
var duration = data.duration;
var distance = data.distance;
nikeplus.addRun(email, password, date, duration, distance, function() {
res.json({ success: true });
}, function(err) {
res.json({ success: false, reason: err.reason });
});
});
var port = process.env.PORT || 8000;
app.listen(port, function() {
console.log('Listening on port ' + port);
});