mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
middleware for content-type validation, refactoring
This commit is contained in:
@@ -2,15 +2,10 @@ import { DeckStateManager } from '../modules/DeckStateManager.js';
|
|||||||
import { GameCreationStepManager } from '../modules/GameCreationStepManager.js';
|
import { GameCreationStepManager } from '../modules/GameCreationStepManager.js';
|
||||||
import { injectNavbar } from '../modules/Navbar.js';
|
import { injectNavbar } from '../modules/Navbar.js';
|
||||||
import createTemplate from '../view_templates/CreateTemplate.js';
|
import createTemplate from '../view_templates/CreateTemplate.js';
|
||||||
import { io } from 'socket.io-client';
|
|
||||||
import { toast } from '../modules/Toast';
|
import { toast } from '../modules/Toast';
|
||||||
|
|
||||||
const create = () => {
|
const create = () => {
|
||||||
injectNavbar();
|
injectNavbar();
|
||||||
const socket = io();
|
|
||||||
socket.on('broadcast', (message) => {
|
|
||||||
toast(message, 'warning', true, false);
|
|
||||||
});
|
|
||||||
document.getElementById('game-creation-container').innerHTML = createTemplate;
|
document.getElementById('game-creation-container').innerHTML = createTemplate;
|
||||||
const deckManager = new DeckStateManager();
|
const deckManager = new DeckStateManager();
|
||||||
const gameCreationStepManager = new GameCreationStepManager(deckManager);
|
const gameCreationStepManager = new GameCreationStepManager(deckManager);
|
||||||
|
|||||||
@@ -1,13 +1,8 @@
|
|||||||
import { XHRUtility } from '../modules/XHRUtility.js';
|
import { XHRUtility } from '../modules/XHRUtility.js';
|
||||||
import { toast } from '../modules/Toast.js';
|
import { toast } from '../modules/Toast.js';
|
||||||
import { injectNavbar } from '../modules/Navbar.js';
|
import { injectNavbar } from '../modules/Navbar.js';
|
||||||
import { io } from 'socket.io-client';
|
|
||||||
|
|
||||||
const home = () => {
|
const home = () => {
|
||||||
const socket = io();
|
|
||||||
socket.on('broadcast', (message) => {
|
|
||||||
toast(message, 'warning', true, false);
|
|
||||||
});
|
|
||||||
injectNavbar();
|
injectNavbar();
|
||||||
document.getElementById('join-form').addEventListener('submit', attemptToJoinGame);
|
document.getElementById('join-form').addEventListener('submit', attemptToJoinGame);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,13 +1,7 @@
|
|||||||
import { injectNavbar } from '../modules/Navbar.js';
|
import { injectNavbar } from '../modules/Navbar.js';
|
||||||
import { io } from 'socket.io-client';
|
|
||||||
import { toast } from '../modules/Toast';
|
|
||||||
|
|
||||||
const howToUse = () => {
|
const howToUse = () => {
|
||||||
injectNavbar();
|
injectNavbar();
|
||||||
const socket = io();
|
|
||||||
socket.on('broadcast', (message) => {
|
|
||||||
toast(message, 'warning', true, false);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
|
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
|
||||||
|
|||||||
@@ -3,14 +3,9 @@ import { toast } from '../modules/Toast.js';
|
|||||||
import { XHRUtility } from '../modules/XHRUtility.js';
|
import { XHRUtility } from '../modules/XHRUtility.js';
|
||||||
import { UserUtility } from '../modules/UserUtility.js';
|
import { UserUtility } from '../modules/UserUtility.js';
|
||||||
import { globals } from '../config/globals.js';
|
import { globals } from '../config/globals.js';
|
||||||
import { io } from 'socket.io-client';
|
|
||||||
|
|
||||||
const join = () => {
|
const join = () => {
|
||||||
injectNavbar();
|
injectNavbar();
|
||||||
const socket = io();
|
|
||||||
socket.on('broadcast', (message) => {
|
|
||||||
toast(message, 'warning', true, false);
|
|
||||||
});
|
|
||||||
const splitUrl = window.location.pathname.split('/join/');
|
const splitUrl = window.location.pathname.split('/join/');
|
||||||
const accessCode = splitUrl[1];
|
const accessCode = splitUrl[1];
|
||||||
if (/^[a-zA-Z0-9]+$/.test(accessCode) && accessCode.length === globals.ACCESS_CODE_LENGTH) {
|
if (/^[a-zA-Z0-9]+$/.test(accessCode) && accessCode.length === globals.ACCESS_CODE_LENGTH) {
|
||||||
|
|||||||
@@ -1,13 +1,7 @@
|
|||||||
import { injectNavbar } from '../modules/Navbar.js';
|
import { injectNavbar } from '../modules/Navbar.js';
|
||||||
import { io } from 'socket.io-client';
|
|
||||||
import { toast } from '../modules/Toast';
|
|
||||||
|
|
||||||
const notFound = () => {
|
const notFound = () => {
|
||||||
injectNavbar();
|
injectNavbar();
|
||||||
const socket = io();
|
|
||||||
socket.on('broadcast', (message) => {
|
|
||||||
toast(message, 'warning', true, false);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
|
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
|
||||||
|
|||||||
6
index.js
6
index.js
@@ -3,16 +3,12 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const app = express();
|
const app = express();
|
||||||
const bodyParser = require('body-parser');
|
|
||||||
const GameManager = require('./server/modules/GameManager.js');
|
const GameManager = require('./server/modules/GameManager.js');
|
||||||
const SocketManager = require('./server/modules/SocketManager.js');
|
const SocketManager = require('./server/modules/SocketManager.js');
|
||||||
const globals = require('./server/config/globals');
|
const globals = require('./server/config/globals');
|
||||||
const ServerBootstrapper = require('./server/modules/ServerBootstrapper');
|
const ServerBootstrapper = require('./server/modules/ServerBootstrapper');
|
||||||
|
|
||||||
app.use(bodyParser.json());
|
app.use(express.json());
|
||||||
app.use(bodyParser.urlencoded({
|
|
||||||
extended: true
|
|
||||||
}));
|
|
||||||
|
|
||||||
const args = ServerBootstrapper.processCLIArgs();
|
const args = ServerBootstrapper.processCLIArgs();
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ if (process.env.NODE_ENV.trim() === 'production') {
|
|||||||
router.use(cors(globals.CORS));
|
router.use(cors(globals.CORS));
|
||||||
|
|
||||||
router.use((req, res, next) => {
|
router.use((req, res, next) => {
|
||||||
|
req.accepts()
|
||||||
if (isAuthorized(req)) {
|
if (isAuthorized(req)) {
|
||||||
next();
|
next();
|
||||||
} else {
|
} else {
|
||||||
@@ -33,6 +34,14 @@ router.use((req, res, next) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.post('/sockets/broadcast', (req, res, next) => {
|
||||||
|
globals.CONTENT_TYPE_VALIDATOR(req, res, next);
|
||||||
|
});
|
||||||
|
router.put('/games/state', (req, res, next) => {
|
||||||
|
globals.CONTENT_TYPE_VALIDATOR(req, res, next);
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO: implement client-side display of this message.
|
||||||
router.post('/sockets/broadcast', function (req, res) {
|
router.post('/sockets/broadcast', function (req, res) {
|
||||||
logger.info('admin user broadcasting message: ' + req.body?.message);
|
logger.info('admin user broadcasting message: ' + req.body?.message);
|
||||||
socketManager.broadcast(req.body?.message);
|
socketManager.broadcast(req.body?.message);
|
||||||
@@ -44,7 +53,7 @@ router.get('/games/state', function (req, res) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.put('/games/state', function (req, res) {
|
router.put('/games/state', function (req, res) {
|
||||||
// TODO: validate the request body - can break the application if malformed.
|
// TODO: validate the JSON object sent - ones that don't match the expected model could break the application.
|
||||||
gameManager.activeGameRunner.activeGames = req.body;
|
gameManager.activeGameRunner.activeGames = req.body;
|
||||||
res.status(201).send(gameManager.activeGameRunner.activeGames);
|
res.status(201).send(gameManager.activeGameRunner.activeGames);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -28,6 +28,16 @@ router.options('/:code/players', cors(globals.CORS));
|
|||||||
router.options('/create', cors(globals.CORS));
|
router.options('/create', cors(globals.CORS));
|
||||||
router.options('/restart', cors(globals.CORS));
|
router.options('/restart', cors(globals.CORS));
|
||||||
|
|
||||||
|
router.post('/create', (req, res, next) => {
|
||||||
|
globals.CONTENT_TYPE_VALIDATOR(req, res, next);
|
||||||
|
});
|
||||||
|
router.patch('/players', (req, res, next) => {
|
||||||
|
globals.CONTENT_TYPE_VALIDATOR(req, res, next);
|
||||||
|
});
|
||||||
|
router.patch('/restart', (req, res, next) => {
|
||||||
|
globals.CONTENT_TYPE_VALIDATOR(req, res, next);
|
||||||
|
});
|
||||||
|
|
||||||
if (process.env.NODE_ENV.trim() === 'production') {
|
if (process.env.NODE_ENV.trim() === 'production') {
|
||||||
router.use(apiLimiter);
|
router.use(apiLimiter);
|
||||||
router.use('/create', gameEndpointLimiter);
|
router.use('/create', gameEndpointLimiter);
|
||||||
|
|||||||
@@ -12,6 +12,14 @@ const globals = {
|
|||||||
origin: 'https://play-werewolf.app',
|
origin: 'https://play-werewolf.app',
|
||||||
optionsSuccessStatus: 200
|
optionsSuccessStatus: 200
|
||||||
},
|
},
|
||||||
|
CONTENT_TYPE_VALIDATOR: (req, res, next) => {
|
||||||
|
req.accepts()
|
||||||
|
if (req.is('application/json')) {
|
||||||
|
next();
|
||||||
|
} else {
|
||||||
|
res.status(400).send('Request has invalid content type.');
|
||||||
|
}
|
||||||
|
},
|
||||||
STALE_GAME_HOURS: 12,
|
STALE_GAME_HOURS: 12,
|
||||||
CLIENT_COMMANDS: {
|
CLIENT_COMMANDS: {
|
||||||
FETCH_GAME_STATE: 'fetchGameState',
|
FETCH_GAME_STATE: 'fetchGameState',
|
||||||
|
|||||||
Reference in New Issue
Block a user