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,
|
2019-10-24 10:21:41 +02:00
|
|
|
createRouteLogger,
|
|
|
|
createErrorLogger,
|
2019-10-23 20:37:06 +02:00
|
|
|
} = require('./middlewares')
|
2019-10-24 10:21:41 +02:00
|
|
|
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
|
2019-10-22 22:26:11 +02:00
|
|
|
const certEndpoint = process.env.CERT_ENDPOINT || ''
|
2019-10-23 15:15:20 +02:00
|
|
|
const certSecret = process.env.CERT_SECRET || ''
|
2019-10-24 10:21:41 +02:00
|
|
|
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()
|
2019-10-24 10:21:41 +02:00
|
|
|
const appLogger = createAppLogger({ dev: isDevelopment })
|
2019-10-15 09:22:08 +02:00
|
|
|
|
2019-10-23 15:15:20 +02:00
|
|
|
app.use(forceSecure)
|
|
|
|
|
2019-10-24 10:21:41 +02:00
|
|
|
// 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-22 22:26:11 +02:00
|
|
|
|
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) => {
|
2019-10-20 19:25:13 +02:00
|
|
|
const error = req.error || req.query.error || ''
|
|
|
|
|
2019-10-17 22:36:29 +02:00
|
|
|
res
|
|
|
|
.status(500)
|
2019-10-20 19:25:13 +02:00
|
|
|
.render('error', { error })
|
|
|
|
})
|
|
|
|
|
2019-10-23 22:29:01 +02:00
|
|
|
app.get('/about', (req, res) => {
|
|
|
|
res.render('about')
|
|
|
|
})
|
|
|
|
|
2019-10-20 19:25:13 +02:00
|
|
|
// NOTE: Capture all unkown URLs
|
|
|
|
app.get('*', (req, res) => {
|
|
|
|
res.status(400).render('404')
|
2019-10-17 22:36:29 +02:00
|
|
|
})
|
|
|
|
|
2019-10-22 20:25:09 +02:00
|
|
|
app.use('/download', checkURLParameter)
|
2019-10-21 20:44:34 +02:00
|
|
|
app.use('/download', rateLimit())
|
2019-10-22 20:25:09 +02:00
|
|
|
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
|
|
|
|
|
2019-10-24 10:21:41 +02:00
|
|
|
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) {
|
2019-10-22 20:25:09 +02:00
|
|
|
next(err)
|
2019-10-15 09:22:08 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-10-24 10:21:41 +02:00
|
|
|
app.use(createErrorLogger({ dev: isDevelopment }))
|
2019-10-17 22:36:29 +02:00
|
|
|
app.use(genericErrorHandler)
|
|
|
|
|
2019-10-24 10:21:41 +02:00
|
|
|
app.listen(port)
|