forked from tpowell/jsadvent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestHandler.js
151 lines (125 loc) · 4.32 KB
/
requestHandler.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
// Copyright 2011 Thomas Oberndörfer (toberndo@yarkon.de)
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
var url = require('url');
var querystring = require('querystring');
var time = require('./time');
var fs = require('fs');
// cheat parameter to set a certain dayData
var CHEAT_PARA = 'should_be_secret';
var PARTICIPANTS_FN = 'participants';
exports.getDayDetails = getDayDetails;
exports.getDays = getDays;
exports.takePart = takePart;
function getDayDetails(request, response) {
var urlQuery = url.parse(request.url).query;
var qs = querystring.parse(urlQuery);
var day = qs.day;
var cheat = qs[CHEAT_PARA];
// only valid days
var regDay = /^([1-9]|[0-2][0-9]|3[0-1])$/;
console.log('Day: ' + day);
if (!regDay.test(day)) throw { name: 'wrong day', message: 'not in range 1-31' };
// omit leading 0
day = +day;
var currLocalDate;
console.log('cheat: ' + cheat);
if (cheat === undefined) {
currLocalDate = time.getLocalDate();
} else {
currLocalDate = time.getCheatDate(cheat);
}
console.log('currLocalDate: ' + currLocalDate);
var targetDate = time.getTargetDate(day);
if (targetDate.getTime() > currLocalDate.getTime()) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write('Not yet!');
response.end();
} else {
fs.readFile('./details.json', 'utf8', function(err, data) {
if (err) throw { name: 'read file error', message: 'can\'t open details.json' };
var details = JSON.parse(data);
var dayData = details['day' + day];
dayData.tst = currLocalDate.toLocaleString();
dayData.day = currLocalDate.getDate();
var dayStr = JSON.stringify(dayData);
if (!dayStr) dayStr = '{ }';
endResponse(response, dayStr, qs.callback);
});
}
}
function endResponse(res, content, jsonp) {
if (jsonp) {
// JSONP
res.writeHead(200, {"Content-Type": "application/javascript; charset=utf-8", "Access-Control-Allow-Origin": "*"});
} else {
// JSON
res.writeHead(200, {"Content-Type": "application/json; charset=utf-8", "Access-Control-Allow-Origin": "*"});
}
if (jsonp) content = jsonp + '(' + content + ')'
res.write(content);
res.end('\n');
}
function getDays(req, result) {
fs.readFile('./details.json', 'utf8', function(err, data) {
if (err) throw { name: 'read file error', message: 'can\'t open details.json' };
var details = JSON.parse(data);
// add active flag
var urlQuery = url.parse(req.url).query;
var qs = querystring.parse(urlQuery);
var cheat = qs[CHEAT_PARA];
var currLocalDate;
if (cheat === undefined) {
currLocalDate = time.getLocalDate();
} else {
currLocalDate = time.getCheatDate(cheat);
}
// date when calendar starts
var targetDate = time.getTargetDate(1);
var currDay = currLocalDate.getDate();
if (targetDate.getTime() <= currLocalDate.getTime()) {
for(i = 1; i <= currLocalDate.getDate(); i++) {
if (details['day' + i]) details['day' + i].active = true;
}
}
result(details);
});
}
function takePart(req, res) {
var answer = {};
var urlQuery = url.parse(req.url).query;
var qs = querystring.parse(urlQuery);
var cheat = qs[CHEAT_PARA];
if (cheat === undefined) {
currLocalDate = time.getLocalDate();
} else {
currLocalDate = time.getCheatDate(cheat);
}
var inDay = new Date(req.body.tst);
console.log('inDay: ' + inDay);
// check if timestamp is from today
if (!(inDay.getDate() == currLocalDate.getDate() && inDay.getMonth() == currLocalDate.getMonth())) {
answer.type = 'error';
answer.message = 'It\'s too late for this door!';
res.send(answer);
return;
}
var participant = JSON.stringify(req.body);
var filename = PARTICIPANTS_FN;
if (process.env.VMC_APP_PORT) {
filename += '_vmc.txt';
} else {
filename += '_local.txt';
}
fs.open(filename, 'a', 0666, function( e, id ) {
fs.write( id, participant + '\n', null, 'utf8', function(){
fs.close(id, function(){
console.log('file closed');
answer.type = 'success';
answer.day = currLocalDate.getDate();
answer.message = 'You take part in the tombola!';
res.send(answer);
});
});
});
}