beginning of large-scale redesign

This commit is contained in:
Alec
2021-11-08 18:26:07 -05:00
parent ee931f1034
commit f861339884
99 changed files with 436 additions and 13683 deletions

94
server/main.js Normal file
View File

@@ -0,0 +1,94 @@
const express = require('express');
const http = require('http');
const https = require('https');
const path = require('path');
const fs = require('fs');
const socketIO = require('socket.io');
const app = express();
let main;
const bodyParser = require('body-parser');
// const GameManager = require('./modules/managers/GameManager.js');
// const QueueManager = require('./modules/managers/QueueManager');
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
const debugMode = Array.from(process.argv.map((arg) => arg.trim().toLowerCase())).includes('debug');
const localServer = Array.from(process.argv.map((arg) => arg.trim().toLowerCase())).includes('local');
const useHttps = Array.from(process.argv.map((arg) => arg.trim().toLowerCase())).includes('https');
const port = process.env.PORT || Array
.from(process.argv.map((arg) => {
return arg.trim().toLowerCase();
}))
.filter((arg) => {
return /port=\d+/.test(arg);
})
.map((arg) => {
return /port=(\d+)/.exec(arg)[1];
})[0] || 5000;
const logger = require('./modules/Logger')(debugMode);
if (localServer) {
logger.log('starting main in LOCAL mode.');
if (useHttps && fs.existsSync(path.join(__dirname, './certs/localhost-key.pem')) && fs.existsSync(path.join(__dirname, './certs/localhost.pem'))) {
const key = fs.readFileSync(path.join(__dirname, './certs/localhost-key.pem'), 'utf-8');
const cert = fs.readFileSync(path.join(__dirname, './certs/localhost.pem'), 'utf-8');
logger.log('local certs detected. Using HTTPS.');
main = https.createServer({ key, cert }, app);
logger.log(`navigate to https://localhost:${port}`);
} else {
logger.log('https not specified or no local certs detected. Using HTTP.');
main = http.createServer(app);
logger.log(`navigate to http://localhost:${port}`);
}
} else {
logger.log('starting main in PRODUCTION mode. WARNING: This should not be used for local development.');
main = http.createServer(app);
const secure = require('express-force-https');
app.use(secure);
}
const io = socketIO(main);
app.set('port', port);
/* Instantiate the singleton game manager */
//const gameManager = new GameManager(logger).getInstance();
/* Instantiate the singleton queue manager */
//const queueManager = new QueueManager(matchmaking, logger).getInstance();
/* api endpoints */
// const games = require('./api/GamesAPI');
// const words = require('./api/WordsAPI');
// app.use('/api/games', games);
// app.use('/api/words', words);
/* serve all the app's pages */
app.use('/manifest.json', (req, res) => {
res.sendFile(path.join(__dirname, '../manifest.json'));
});
app.use('/favicon.ico', (req, res) => {
res.sendFile(path.join(__dirname, '../client/favicon_package/favicon.ico'));
});
const router = require('./routes/router');
const faviconRouter = require('./routes/favicon-router');
const staticRouter = require('./routes/static-router');
app.use('', router);
app.use('', staticRouter);
app.use('', faviconRouter);
app.use(function (req, res) {
res.sendFile(path.join(__dirname, '../client/views/404.html'));
});
// Starts the main.
main.listen(port, function () {
logger.log(`Starting server on port ${port} http://localhost:${port}` );
});

20
server/modules/Logger.js Normal file
View File

@@ -0,0 +1,20 @@
module.exports = function (debugMode = false) {
return {
log (message = '') {
const now = new Date();
console.log('LOG ', now.toGMTString(), ': ', message);
},
debug (message = '') {
if (!debugMode) return;
const now = new Date();
console.debug('DEBUG ', now.toGMTString(), ': ', message);
},
error (message = '') {
if (!debugMode) return;
const now = new Date();
console.error('ERROR ', now.toGMTString(), ': ', message);
}
};
};

View File

@@ -0,0 +1,18 @@
const express = require('express');
const faviconRouter = express.Router();
const path = require('path');
const checkIfFileExists = require("./util");
faviconRouter.use('/client/favicon_package/*', (req, res) => {
let filePath = path.join(__dirname, ('../../' + req.baseUrl));
let extension = path.extname(filePath);
checkIfFileExists(filePath).then((fileExists) => {
if (fileExists && (extension === '.png' || extension === '.ico' || extension === '.svg' || extension === 'xml')) {
res.sendFile(filePath);
} else {
res.sendStatus(404);
}
});
});
module.exports = faviconRouter;

13
server/routes/router.js Normal file
View File

@@ -0,0 +1,13 @@
const express = require('express');
const router = express.Router();
const path = require('path');
router.get('/', function (request, response) {
response.sendFile(path.join(__dirname, '../../client/views/home.html'));
});
router.get('/create', function (request, response) {
response.sendFile(path.join(__dirname, '../../client/views/create.html'));
});
module.exports = router;

View File

@@ -0,0 +1,66 @@
const express = require('express');
const staticRouter = express.Router();
const path = require('path');
const checkIfFileExists = require("./util");
staticRouter.use('/styles/*', (req, res) => {
let filePath = path.join(__dirname, ('../../client/' + req.baseUrl));
let extension = path.extname(filePath);
checkIfFileExists(filePath).then((fileExists) => {
if (fileExists && (extension === '.css')) {
res.sendFile(filePath);
} else {
res.sendStatus(404);
}
});
});
staticRouter.use('/client/webfonts/*', (req, res) => {
let filePath = path.join(__dirname, ('../' + req.baseUrl));
let extension = path.extname(filePath);
checkIfFileExists(filePath).then((fileExists) => {
if (fileExists && (extension === '.ttf' || extension === '.woff2')) {
res.sendFile(filePath);
} else {
res.sendStatus(404);
}
});
});
staticRouter.use('/client/images/*', (req, res) => {
let filePath = path.join(__dirname, ('../' + req.baseUrl));
let extension = path.extname(filePath);
checkIfFileExists(filePath).then((fileExists) => {
if (fileExists && (extension === '.svg' || extension === '.png' || extension === '.jpg')) {
res.sendFile(filePath);
} else {
res.sendStatus(404);
}
});
});
staticRouter.use('/scripts/*', (req, res) => {
let filePath = path.join(__dirname, ('../../client/' + req.baseUrl));
let extension = path.extname(filePath);
checkIfFileExists(filePath).then((fileExists) => {
if (fileExists && (extension === '.js')) {
res.sendFile(filePath);
} else {
res.sendStatus(404);
}
});
});
staticRouter.use('/views/*', (req, res) => {
let filePath = path.join(__dirname, ('../../client/' + req.baseUrl));
let extension = path.extname(filePath);
checkIfFileExists(filePath).then((fileExists) => {
if (fileExists && (extension === '.html')) {
res.sendFile(filePath);
} else {
res.sendFile('../views/404.html');
}
});
});
module.exports = staticRouter;

9
server/routes/util.js Normal file
View File

@@ -0,0 +1,9 @@
const fs = require('fs');
function checkIfFileExists(file) {
return fs.promises.access(file, fs.constants.F_OK)
.then(() => true)
.catch((e) => { console.error(e); return false });
}
module.exports = checkIfFileExists;