Facebook-Events-iCal-Converter/lib/index.js

122 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-10-15 09:22:08 +02:00
const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
2019-10-20 19:30:32 +02:00
const favicon = require('serve-favicon')
2019-10-21 20:44:34 +02:00
const rateLimit = require('express-rate-limit')
2019-10-15 09:22:08 +02:00
const { retrieveICS } = require('./services/ics-retriever')
const crawl = require('./services/crawler')
2019-10-23 20:37:06 +02:00
const {
genericErrorHandler,
checkURLParameter,
forceSecure,
createRouteLogger,
createErrorLogger,
2019-10-23 20:37:06 +02:00
} = require('./middlewares')
2020-07-17 21:34:52 +02:00
const { createAppLogger } = require('./log-utils')
const { getNormalizedUrl } = require('./utils')
2019-10-15 09:22:08 +02:00
2019-10-15 22:55:12 +02:00
const port = process.env.PORT
const certEndpoint = process.env.CERT_ENDPOINT || ''
2019-10-23 15:15:20 +02:00
const certSecret = process.env.CERT_SECRET || ''
const isDevelopment = process.env.NODE_ENV === 'development'
const enforceHTTPS = Boolean(process.env.ENFORCE_HTTPS)
2019-10-23 15:15:20 +02:00
2019-10-15 09:22:08 +02:00
const app = express()
const appLogger = createAppLogger({ dev: isDevelopment })
const limiter = rateLimit({
windowMs: 60 * 1000,
max: 10,
})
2019-11-14 08:56:30 +01:00
const pkg = require('../package.json')
const version = pkg.version || ''
2019-10-15 09:22:08 +02:00
// Force app to always redirect to HTTPS
// use when you can't configure web server
if (enforceHTTPS) {
app.use(forceSecure)
}
2019-10-23 15:15:20 +02:00
// Server logs
// You can alternatively enable these to mimic logs created
// by your web server
if (isDevelopment) {
app.use(createRouteLogger({ dev: isDevelopment }))
}
2019-10-15 09:22:08 +02:00
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'))
app.set('trust proxy', 1)
2019-10-20 19:30:32 +02:00
app.use(express.static(path.join(__dirname, '..', 'dist')))
//app.use(favicon(path.join(__dirname, '..', 'dist', 'favicon.ico')))
2019-10-24 21:55:05 +02:00
app.use(bodyParser.urlencoded({ extended: true }))
2019-10-15 09:22:08 +02:00
const indexFile = path.join(__dirname, '..', 'dist', 'index.html')
2019-10-23 15:15:20 +02:00
if (certEndpoint) {
app.get(`/${certEndpoint}`, (req, res) => {
res.status(200).send(certSecret)
})
}
2019-10-17 22:36:29 +02:00
app.get('/error', (req, res) => {
const error = req.error || req.query.error || ''
2019-10-17 22:36:29 +02:00
res
.status(500)
.render('error', { error })
})
2019-10-23 22:29:01 +02:00
app.get('/about', (req, res) => {
2019-11-14 08:56:30 +01:00
res.render('about', { version })
2019-10-23 22:29:01 +02:00
})
// NOTE: Capture all unkown URLs
app.get('*', (req, res) => {
res.status(400).render('404')
2019-10-17 22:36:29 +02:00
})
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.use('/download', limiter)
app.use('/download', checkURLParameter)
app.post('/download', async (req, res, next) => {
2019-10-15 09:22:08 +02:00
try {
2019-10-23 20:37:06 +02:00
const { url } = req.body
const ics = await retrieveICS(url, {
logger: appLogger,
crawl,
})
2019-10-23 20:37:06 +02:00
res
.contentType('text/calendar')
.status(200)
.send(Buffer.from(ics, 'utf8'))
2019-10-15 09:22:08 +02:00
} catch (err) {
next(err)
2019-10-15 09:22:08 +02:00
}
})
app.use(createErrorLogger({ dev: isDevelopment }))
2019-10-17 22:36:29 +02:00
app.use(genericErrorHandler)
app.listen(port)