move app routes to separate directory for better composition in
preparation of firebase integration
This commit is contained in:
parent
c7175382b6
commit
0ad6bba142
56
lib/index.js
56
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)
|
||||
|
|
|
@ -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,
|
||||
}
|
|
@ -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,
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
const { error, notFound } = require('./error')
|
||||
const { download, downloadHTML } = require('./download')
|
||||
|
||||
module.exports = {
|
||||
error,
|
||||
notFound,
|
||||
download,
|
||||
downloadHTML,
|
||||
}
|
Loading…
Reference in New Issue