Skip to content
Snippets Groups Projects
main.ts 1.11 KiB
Newer Older
Jack Hu's avatar
Jack Hu committed
import express from 'express';
import expressWs from 'express-ws';
import path from 'path';
import { runTests } from './testing';

const { app } = expressWs(express());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

const resultsCache = {};

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'index.html'));
});

app.post('/viewResults', (req, res) => {
    const url = req.body.url;
    const id = btoa(url);
    res.redirect(`/viewResults/${id}`);
});

app.get('/viewResults/:id', (req, res) => {
    res.sendFile(path.join(__dirname, 'results.html'));
});

app.ws('/results/:id', async (ws, req) => {
    const awsUrl = atob(req.params.id);
    const iterations: number = Number(req.query.iterations) || 1;
    if (resultsCache[awsUrl]) {
        ws.send(JSON.stringify(resultsCache[awsUrl]));
    } else {
        runTests(awsUrl, resultsCache, iterations, ws);
    }

    ws.onmessage = (msg) => {
        const iterations = Number(msg.data);
        runTests(awsUrl, resultsCache, iterations, ws);
    };
});

app.listen(80, () => {
    console.log('Server is running on port 80');
});