Simplify server switch/case and check if deck in decks list

This commit is contained in:
Adam Goldsmith 2017-10-02 14:37:56 -04:00
parent 837307ad4e
commit 6ce9aa70ef
2 changed files with 33 additions and 31 deletions

View File

@ -9,6 +9,8 @@ const http = require('http'),
phantom = require('phantom'), phantom = require('phantom'),
port = 8080; port = 8080;
const decks = ["the_Unholy_Priest_update_2", "NZoths_Invasion_1.2", "Puffer_Fish_input_1.3"];
const server = http.createServer((req, res) => { const server = http.createServer((req, res) => {
const uri = url.parse(req.url); const uri = url.parse(req.url);
@ -24,13 +26,9 @@ const server = http.createServer((req, res) => {
case 'js': case 'js':
switch (pathParts[2]) { switch (pathParts[2]) {
case 'playfield.js': case 'playfield.js':
sendFile(res, 'js/playfield.js', 'application/javascript');
break;
case 'editor.js': case 'editor.js':
sendFile(res, 'js/editor.js', 'application/javascript');
break;
case 'interact.js': case 'interact.js':
sendFile(res, 'interact.js', 'application/javascript'); sendFile(res, 'js/' + pathParts[2], 'application/javascript');
break; break;
default: default:
send404(res, uri); send404(res, uri);
@ -59,31 +57,36 @@ const server = http.createServer((req, res) => {
} }
break; break;
case 'deck': case 'deck':
if (pathParts.length === 3) { if (pathParts.length < 3 || pathParts[2] === '') {
let deckName = decodeURI(pathParts[2]); sendIndex(res);
sendDeckIndex(res, deckName); break;
} }
else if (pathParts.length === 4) { let deckName = decodeURI(pathParts[2]);
let deckName = decodeURI(pathParts[2]); if (!decks.includes(deckName)) {
switch (pathParts[3]) { send404(res, uri);
case 'play': break;
sendFile(res, 'html/playfield.html'); }
break; switch (pathParts[3] || '') {
case 'editor': case '':
sendFile(res, 'html/editor.html'); sendDeckIndex(res, deckName);
break; break;
case 'deck.png': case 'play':
sendFile(res, deckName + '.png', 'image/png'); sendFile(res, 'html/playfield.html');
break; break;
case 'deck.json': case 'editor':
sendFileJSON(res, deckName); sendFile(res, 'html/editor.html');
break; break;
case 'upload': case 'deck.png':
handleUpload(res, req, deckName); sendFile(res, deckName + '.png', 'image/png');
break; break;
default: case 'deck.json':
send404(res, uri); sendFileJSON(res, deckName);
} break;
case 'upload':
handleUpload(res, req, deckName);
break;
default:
send404(res, uri);
} }
break; break;
default: default:
@ -95,7 +98,6 @@ server.listen(process.env.PORT || port);
console.log('listening on 8080'); console.log('listening on 8080');
function sendIndex(res) { function sendIndex(res) {
let decks = ["the_Unholy_Priest_update_2", "NZoths_Invasion_1.1"];
const html = ` const html = `
<html> <html>
<head> <head>
@ -104,7 +106,7 @@ function sendIndex(res) {
</head> </head>
<body> <body>
<ul> <ul>
${(decks.map(d => `<li><a href="deck/${d}">${d}</a></li>`).join(' '))} ${(decks.map(d => `<li><a href="/deck/${d}">${d}</a></li>`).join(' '))}
</ul> </ul>
</body> </body>
</html>`; </html>`;