31 lines
851 B
JavaScript
31 lines
851 B
JavaScript
|
|
import { Router } from 'express';
|
||
|
|
import { v4 as uuidv4 } from 'uuid';
|
||
|
|
import { createJob } from '../db.js';
|
||
|
|
import { enqueue } from '../queue.js';
|
||
|
|
|
||
|
|
const router = Router();
|
||
|
|
|
||
|
|
router.post('/', (req, res) => {
|
||
|
|
let { url, browser, mobile, runs } = req.body;
|
||
|
|
|
||
|
|
// Validate
|
||
|
|
if (!url || !url.startsWith('http')) {
|
||
|
|
return res.status(400).render('error', {
|
||
|
|
title: 'Invalid URL',
|
||
|
|
message: 'Please provide a valid URL starting with http:// or https://',
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
browser = ['chrome', 'firefox'].includes(browser) ? browser : 'chrome';
|
||
|
|
mobile = mobile === '1' || mobile === 'on' || mobile === true;
|
||
|
|
runs = Math.min(Math.max(parseInt(runs) || 3, 1), 9);
|
||
|
|
|
||
|
|
const id = uuidv4();
|
||
|
|
createJob(id, url, browser, mobile, runs);
|
||
|
|
enqueue({ id, url, browser, mobile, runs });
|
||
|
|
|
||
|
|
res.redirect(`/status/${id}`);
|
||
|
|
});
|
||
|
|
|
||
|
|
export default router;
|