This repository has been archived by the owner on Apr 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
133 lines (114 loc) · 3.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
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
var _ = require('lodash'),
express = require('express'),
bodyParser = require('body-parser'),
moment = require('moment-timezone');
var app = express();
// Parse application/json
app.use(bodyParser.json())
// Stub data
var locale = "Pacific/Auckland";
function makeNight(dateStr) {
return moment.tz(dateStr, locale).startOf('day').unix();
}
var data = [
makeNight("2016-01-01"),
makeNight("2016-01-08"),
makeNight("2016-02-14"),
makeNight("2016-02-29"),
makeNight("2016-03-15"),
makeNight("2016-03-16"),
makeNight("2016-04-12"),
makeNight("2016-04-18"),
makeNight("2016-05-25"),
makeNight("2016-06-18"),
makeNight("2016-08-11"),
makeNight("2016-09-25"),
makeNight("2016-10-14"),
makeNight("2016-12-29"),
makeNight("2017-01-25"),
makeNight("2017-02-21"),
makeNight("2017-02-08"),
makeNight("2017-03-31")
];
// Probably not the safest way to handle CORS
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT');
res.header("Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept");
next();
});
// Simulate failure every so often
var failureProb = 0.1;
// Simulate latency
var maxLatency = 1500;
// JSON-encodes data for response and sends with random latency
function send(response, data) {
var latency = Math.floor(Math.random() * maxLatency);
setTimeout(function() {
response.send(JSON.stringify(data))
}, latency);
}
function badRequest(response) {
response.status(400);
response.send('Bad Request');
}
function internalError(response) {
response.status(500);
response.send('Internal Server Error');
}
// End-point to get booked nights
// * start and end are integers (seconds since Unix epoch)
app.get('/reserved/:start/:end', function(request, response) {
var start = parseInt(request.params.start);
var end = parseInt(request.params.end);
if (isNaN(start) || isNaN(end)) {
badRequest(response);
return;
}
if (Math.random() < failureProb) {
internalError(response);
return;
}
var reserved = _.filter(data, function(night) {
return night >= start && night <= end;
});
send(response, {
reserved: reserved
});
});
// End-point to change
app.put('/reserved/:date', function(request, response) {
var date = request.params.date;
var reserved = !!request.body.reserved;
if (isNaN(date)) {
badRequest(response);
return;
}
else if (Math.random() < failureProb) {
internalError(response);
return;
}
else {
var date = moment.unix(date).tz(locale).startOf('day').unix();
if (reserved) {
data.push(date);
data.sort();
data = _.sortedUniq(data);
} else {
_.pull(data, date);
}
send(response, {
ok: true
});
}
});
// Get server time
app.get('/now', function(request, response) {
send(response, {
time: moment(new Date()).unix()
});
});
var port = 3000;
console.info("API server listening at http://localhost:" + port)
app.listen(port);