Skip to content

Commit

Permalink
simplify routing
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Sep 18, 2024
1 parent be6ada3 commit f7fb1c0
Show file tree
Hide file tree
Showing 14 changed files with 12 additions and 221 deletions.
5 changes: 0 additions & 5 deletions client/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ import account from './scripts/accounts.js';

// Javascript code common to *all* pages of the site.

// Never use trailing slash, except for the root directory
if (window.location.pathname.endsWith('/') && window.location.pathname.length > 1) {
window.location.pathname = window.location.pathname.substring(0, window.location.pathname.length - 1);
}

// Always use www
if (['qbreader.herokuapp.com', 'qbreader-production.herokuapp.com', 'qbreader.org'].includes(window.location.hostname)) {
window.location.hostname = 'www.qbreader.org';
Expand Down
16 changes: 0 additions & 16 deletions routes/about/index.js

This file was deleted.

9 changes: 0 additions & 9 deletions routes/admin/category-reports.js

This file was deleted.

13 changes: 1 addition & 12 deletions routes/admin/index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
import isAdmin from '../../database/account-info/is-admin.js';
import { checkToken } from '../../server/authentication.js';

import categoryReportsRouter from './category-reports.js';
import geowordRouter from './geoword.js';
import leaderboardRouter from './leaderboard.js';

import { Router } from 'express';

const router = Router();

router.use(async (req, res, next) => {
const { username, token } = req.session;

if (!checkToken(username, token)) {
delete req.session;
res.redirect('/user/login?' + encodeURIComponent(req.originalUrl));
return;
}

const admin = await isAdmin(username);

if (!admin) {
res.redirect('/user/login');
return;
return res.redirect('/user/login');
}

next();
});

router.use('/category-reports', categoryReportsRouter);
router.use('/geoword', geowordRouter);
router.use('/leaderboard', leaderboardRouter);

router.get('/', (req, res) => {
res.sendFile('index.html', { root: './client/admin' });
});

export default router;
9 changes: 0 additions & 9 deletions routes/admin/leaderboard.js

This file was deleted.

30 changes: 0 additions & 30 deletions routes/api-docs.js

This file was deleted.

8 changes: 0 additions & 8 deletions routes/backups.js

This file was deleted.

8 changes: 0 additions & 8 deletions routes/database.js

This file was deleted.

94 changes: 9 additions & 85 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -1,117 +1,41 @@
import aboutRouter from './about/index.js';
import adminRouter from './admin/index.js';
import apiRouter from './api/index.js';
import apiDocsRouter from './api-docs.js';
import authRouter from './auth/index.js';
import backupsRouter from './backups.js';
import databaseRouter from './database.js';
import frequencyListRouter from './frequency-list.js';
import geowordRouter from './geoword/index.js';
import multiplayerRouter from './multiplayer.js';
import settingsRouter from './settings.js';
import singleplayerRouter from './singleplayer/index.js';
import userRouter from './user.js';
import webhookRouter from './api/webhook.js';

import { Router } from 'express';
import express, { Router } from 'express';
const router = Router();

router.get('/', (req, res) => {
res.sendFile('index.html', { root: './client' });
});

router.get('/react(-dom)?/umd/*.js', (req, res) => {
res.sendFile(req.url, { root: './node_modules' });
});

router.get('/react-highlight-words/build/static/*.js', (req, res) => {
res.sendFile(req.url, { root: './node_modules' });
});

router.get('/node_modules/*.scss', (req, res) => {
res.sendFile(req.url.substring(13), { root: './node_modules' });
});

router.get('/*.css', (req, res) => {
res.sendFile(req.url, { root: './client' });
});

router.get('/*.ico', (req, res) => {
res.sendFile(req.url, { root: './client' });
});

router.get('/*.js', (req, res) => {
res.sendFile(req.url, { root: './client' });
});

router.get('/*.jsx', (req, res) => {
res.sendFile(req.url, { root: './client' });
});

router.get('/*.map', (req, res) => {
res.sendFile(req.url, { root: './client' });
});

router.get('/*.png', (req, res) => {
res.sendFile(req.url, { root: './client' });
});

router.get('/*.scss', (req, res) => {
res.sendFile(req.url.substring(5), { root: './scss' });
res.sendFile(req.url, { root: './scss' });
});

/**
* Redirects:
*/
router.get('/*.html', (req, res) => {
res.redirect(req.url.substring(0, req.url.length - 5));
});

router.get('/api-info', (req, res) => {
res.redirect('/api-docs');
});

router.get('/bonuses', (_req, res) => {
res.redirect('/singleplayer/bonuses');
});

router.get('/database', (_req, res) => {
res.redirect('/db');
});

router.get('/index', (req, res) => {
res.redirect('/');
});

router.get('/tossups', (req, res) => {
res.redirect('/singleplayer/tossups');
});

router.get('/users', (req, res) => {
res.redirect(`/user${req.url}`);
});
router.get('/api-info', (req, res) => res.redirect('/api-docs'));
router.get('/bonuses', (req, res) => res.redirect('/singleplayer/bonuses'));
router.get('/db', (req, res) => res.redirect('/database'));
router.get('/tossups', (req, res) => res.redirect('/singleplayer/tossups'));
router.get('/user', (req, res) => res.redirect('/user/login'));

/**
* Routes:
*/

router.use('/about', aboutRouter);
router.use('/admin', adminRouter);
router.use('/api', apiRouter);
router.use('/api-docs', apiDocsRouter);
router.use('/auth', authRouter);
router.use('/backups', backupsRouter);
router.use('/db', databaseRouter);
router.use('/frequency-list', frequencyListRouter);
router.use('/geoword', geowordRouter);
router.use('/multiplayer', multiplayerRouter);
router.use('/settings', settingsRouter);
router.use('/singleplayer', singleplayerRouter);
router.use('/user', userRouter);
router.use('/webhook', webhookRouter);

router.use((req, res) => {
res.sendFile(req.url, { root: './client' });
});
router.use(express.static('client', { extensions: ['html'] }));
router.use(express.static('node_modules'));

export default router;
3 changes: 2 additions & 1 deletion routes/multiplayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ router.get('/', (req, res) => {
res.sendFile('index.html', { root: './client/multiplayer' });
});

router.get('/*', (req, res) => {
// only match paths that don't have a file extension
router.get(/^\/[^.]*$/, (req, res) => {
res.sendFile('room.html', { root: './client/multiplayer' });
});

Expand Down
8 changes: 0 additions & 8 deletions routes/settings.js

This file was deleted.

8 changes: 0 additions & 8 deletions routes/singleplayer/bonuses.js

This file was deleted.

14 changes: 0 additions & 14 deletions routes/singleplayer/index.js

This file was deleted.

8 changes: 0 additions & 8 deletions routes/singleplayer/tossups.js

This file was deleted.

0 comments on commit f7fb1c0

Please sign in to comment.