1
0
mirror of https://github.com/comatory/fb2iCal synced 2024-12-27 08:52:58 +01:00
Facebook-Events-iCal-Converter/lib/index.js

87 lines
1.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
2019-10-23 20:37:06 +02:00
const retrieveICS = require('./services/ics-retriever')
const {
genericErrorHandler,
checkURLParameter,
forceSecure,
createRouteLogger,
createErrorLogger,
2019-10-23 20:37:06 +02:00
} = require('./middlewares')
const { createAppLogger } = 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'
2019-10-23 15:15:20 +02:00
2019-10-15 09:22:08 +02:00
const app = express()
const appLogger = createAppLogger({ dev: isDevelopment })
2019-10-15 09:22:08 +02:00
2019-10-23 15:15:20 +02:00
app.use(forceSecure)
// Server logs
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'))
2019-10-20 19:30:32 +02:00
2019-10-20 20:32:25 +02:00
app.use(express.static(path.join(__dirname, 'public')))
2019-10-20 20:47:19 +02:00
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
2019-10-15 09:22:08 +02:00
app.use(bodyParser())
2019-10-23 15:15:20 +02:00
if (certEndpoint) {
app.get(`/${certEndpoint}`, (req, res) => {
res.status(200).send(certSecret)
})
}
2019-10-15 09:22:08 +02:00
app.get('/', (req, res) => {
res.render('index')
})
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) => {
res.render('about')
})
// 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', checkURLParameter)
2019-10-21 20:44:34 +02:00
app.use('/download', rateLimit())
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,
})
2019-10-23 20:37:06 +02:00
res
.contentType('text/calendar')
.status(200)
.send(new Buffer(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)