This repository has been archived by the owner on Jun 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
180 lines (148 loc) · 3.85 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
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
function server() {
var express = require('express');
var app = express();
var tracks = undefined;
var html = undefined;
function render(debug, unlockedTrack, focusedTrack) {
return html.render(tracks, unlockedTrack, focusedTrack, debug);
}
function setRealHTML(setHtml) {
html = setHtml;
}
function setRealTracks(setTracks) {
tracks = setTracks;
}
app.get('/', function(request, response) {
var query = request.query;
var debugHTML = false;
var focusTrack = undefined;
var unlockTrack = undefined;
if (query.doughnuts !== undefined)
{
debugHTML = query.doughnuts;
}
if (query.focus !== undefined)
{
var focusHash = query.focus;
var track = tracks.trackWithHash(focusHash);
if (track.isUnlocked() === true)
{
focusTrack = track;
}
}
if (query.unlock !== undefined)
{
var unlockName = query.unlock;
unlockTrack = tracks.trackWithName(unlockName);
if (unlockTrack === undefined)
{
// ok, maybe it's a hash
var unlockHash = query.unlock;
unlockTrack = tracks.trackWithHash(unlockHash);
}
}
mainPath(debugHTML, unlockTrack, focusTrack, response);
} );
/*
//////
app.get('/eat_doughnuts', function(request, response) {
tracks.setAllLocked(true);
response.send( render(true) );
} );
app.get('/bake_doughnuts', function(request, response) {
tracks.setAllLocked(false);
response.send( render(true) );
} );
*/
app.use(express.static(__dirname + '/public') );
app.get('/*', function(request, response) {
if (request.params[0] !== undefined)
{
var param = request.params[0];
var testFor = "images/tapes/";
if (param.substring(0, testFor.length) === testFor)
{
// since /public didn't catch this request then the file doesn't exist, let's pass the default!
serveDefaultTapeImage(param, response);
} else
{
var trackName = request.params[0];
var unlockTrack = tracks.trackWithName(trackName);
if (unlockTrack == undefined)
{
console.log("Unlock path called for params " + JSON.stringify(request.params) );
}
mainPath(false, unlockTrack, undefined, response);
}
}
} );
function serveDefaultTapeImage(requestPath, response) {
var options = {
root: __dirname + '/public/',
dotfiles: 'deny',
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
};
var testFor = "_unlocked.jpg";
if (requestPath.substring(requestPath.length - testFor.length, requestPath.length) === testFor)
{
response.sendFile("images/tapes/default_unlocked.jpg", options);
} else
{
response.sendFile("images/tapes/default_locked.jpg", options);
}
}
function mainPath(debugHTML, unlockTrack, focusTrack, response) {
if (unlockTrack != undefined)
{
console.log("Serving unlock: " + unlockTrack.name);
if (unlockTrack.isLocked() )
{
console.log("\tUnlocking " + unlockTrack.name);
unlockTrack.setUnlocked(true);
tracks.save();
} else
{
console.log("\tAlready Unlocked " + unlockTrack.name);
}
if (focusTrack === undefined)
{
focusTrack = unlockTrack;
}
}
if (focusTrack !== undefined)
{
analytics.sendEvent("track", "focus", focusTrack.name);
}
response.send( render(debugHTML, unlockTrack, focusTrack) );
}
app.use(function(request, response, next)
{
response.send( render(false) );
});
app.use(function(error, request, response, next) {
console.error(error.stack);
response.send( render(false) );
} );
return {
setHTML : function(html) {
setRealHTML(html);
},
setTracks : function(tracks) {
setRealTracks(tracks);
},
start : function(listenPort) {
if (listenPort === undefined)
{
listenPort = 3000;
}
var server = app.listen(listenPort, function() {
console.log('Listening on port %d', server.address().port);
analytics.sendEvent("app", "start");
});
}
};
}
module.exports = exports = new server();