forked from collingo/hackthebarbican
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
119 lines (101 loc) · 2.97 KB
/
app.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
var http = require("http"),
path = require("path"),
url = require("url"),
fs = require("fs"),
moment = require("moment");
function twoDigitNumber(num) {
return ("0" + num).slice(-2);
}
http.createServer(function(request, response) {
var req_path = url.parse(request.url).pathname,
full_path,
stat,
readStream,
time,
hour,
minute,
assetBaseUrl,
responseValue;
switch(req_path) {
case "/time":
time = moment();
hour = twoDigitNumber(time.hour());
minute = twoDigitNumber(time.minute());
// assetBaseUrl = "/assets/"+hour+minute;
assetBaseUrl = "/assets/1345";
fs.exists(path.join(process.cwd(), assetBaseUrl), function(exists) {
var responseValue = hour+":"+minute;
if(exists) {
var content = require(path.join(process.cwd(), assetBaseUrl, "/content.json"));
responseValue = (content.glitchDate || hour+":"+minute) + "|"+assetBaseUrl+"/audio.mp3|"+assetBaseUrl+"/image.jpg|"+assetBaseUrl+"/content.json";
}
response.writeHeader(200, {"Content-Type": "text/plain"});
response.write(responseValue);
response.end();
});
break;
case "/assets":
time = moment();
hour = twoDigitNumber(time.hour());
minute = twoDigitNumber(time.minute());
assetBaseUrl = "/assets/"+hour+minute;
fs.exists(path.join(process.cwd(), assetBaseUrl), function(exists) {
var responseValue = hour+":"+minute;
if(exists) {
responseValue += "|"+assetBaseUrl+"/audio.mp3";
}
response.writeHeader(200, {"Content-Type": "text/plain"});
response.write(responseValue);
response.end();
});
break;
case "/favicon.ico":
// ignore favicon request
console.log("Ignored favicon request");
break;
default:
var baseFilePath = "/www";
if(req_path.split('/')[1] === "assets") {
baseFilePath = "";
}
full_path = path.join(process.cwd(), baseFilePath+((req_path === "/") ? "/index.html" : req_path));
fs.exists(full_path, function(exists) {
if(!exists) {
response.writeHeader(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
} else {
fs.readFile(full_path, "binary", function(err, file) {
if(err) {
response.writeHeader(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
} else {
switch(full_path.split(".")[1]) {
case "mp3":
console.log("mp3 bitches");
stat = fs.statSync(full_path);
response.writeHead(200, {
'Content-Type': 'audio/mpeg',
'Content-Length': stat.size
});
readStream = fs.createReadStream(full_path);
readStream.on('data', function(data) {
response.write(data);
});
readStream.on('end', function() {
response.end();
});
break;
default:
console.log("anyting else");
response.writeHeader(200);
response.write(file, "binary");
response.end();
}
}
});
}
});
}
}).listen(process.env.VMC_APP_PORT || 1337, null);