diff --git a/lib/index.js b/lib/index.js index e97d867..0f9c776 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,8 +4,13 @@ const path = require('path') const favicon = require('serve-favicon') const rateLimit = require('express-rate-limit') -const { retrieveICS } = require('./services/ics-retriever') -const crawl = require('./services/crawler') +const { + error, + notFound, + download, + downloadHTML, +} = require('./routes') + const { genericErrorHandler, checkURLParameter, @@ -14,7 +19,6 @@ const { createErrorLogger, } = require('./middlewares') const { createAppLogger } = require('./log-utils') -const { getNormalizedUrl } = require('./utils') const port = process.env.PORT const certEndpoint = process.env.CERT_ENDPOINT || '' @@ -60,60 +64,22 @@ if (certEndpoint) { }) } -app.get('/error', (req, res) => { - const error = req.error || req.query.error || '' - - res - .status(500) - .render('error', { error }) -}) +app.get('/error', error) app.get('/about', (req, res) => { res.render('about', { version }) }) // NOTE: Capture all unkown URLs -app.get('*', (req, res) => { - res.status(400).render('404') -}) +app.get('*', notFound) app.use('/download/html', limiter) app.use('/download/html', checkURLParameter) -app.post('/download/html', async (req, res, next) => { - try { - const { url } = req.body - - const facebookURL = getNormalizedUrl(url) - const html = await crawl(facebookURL, { logger: appLogger }) - - res - .contentType('text/html') - .status(200) - .send(Buffer.from(html, 'utf8')) - } catch (err) { - next(err) - } -}) +app.post('/download/html', downloadHTML(appLogger)) app.use('/download', limiter) app.use('/download', checkURLParameter) -app.post('/download', async (req, res, next) => { - try { - const { url } = req.body - - const ics = await retrieveICS(url, { - logger: appLogger, - crawl, - }) - - res - .contentType('text/calendar') - .status(200) - .send(Buffer.from(ics, 'utf8')) - } catch (err) { - next(err) - } -}) +app.post('/download', download) app.use(createErrorLogger({ dev: isDevelopment })) app.use(genericErrorHandler) diff --git a/lib/routes/download.js b/lib/routes/download.js new file mode 100644 index 0000000..3733bec --- /dev/null +++ b/lib/routes/download.js @@ -0,0 +1,42 @@ +const { getNormalizedUrl } = require('../utils') +const crawl = require('../services/crawler') +const { retrieveICS } = require('../services/ics-retriever') + +const downloadHTML = (logger) => (async (req, res, next) => { + try { + const { url } = req.body + + const facebookURL = getNormalizedUrl(url) + const html = await crawl(facebookURL, { logger }) + + res + .contentType('text/html') + .status(200) + .send(Buffer.from(html, 'utf8')) + } catch (err) { + next(err) + } +}) + +const download = async (req, res, next) => { + try { + const { url } = req.body + + const ics = await retrieveICS(url, { + logger: appLogger, + crawl, + }) + + res + .contentType('text/calendar') + .status(200) + .send(Buffer.from(ics, 'utf8')) + } catch (err) { + next(err) + } +} + +module.exports = { + downloadHTML, + download, +} diff --git a/lib/routes/error.js b/lib/routes/error.js new file mode 100644 index 0000000..c45c373 --- /dev/null +++ b/lib/routes/error.js @@ -0,0 +1,16 @@ +const error = (req, res) => { + const error = req.error || req.query.error || '' + + res + .status(500) + .render('error', { error }) +} + +const notFound = (req, res) => { + res.status(400).render('404') +} + +module.exports = { + error, + notFound, +} diff --git a/lib/routes/index.js b/lib/routes/index.js new file mode 100644 index 0000000..30f31ac --- /dev/null +++ b/lib/routes/index.js @@ -0,0 +1,9 @@ +const { error, notFound } = require('./error') +const { download, downloadHTML } = require('./download') + +module.exports = { + error, + notFound, + download, + downloadHTML, +}