mirror of
https://github.com/comatory/fb2iCal
synced 2025-06-05 22:09:25 +02:00
Server now downloads the HTML file via new endpoint but the parsing logic now happens in the browser. The reason for this is to have a way same code for both environments. If the JavaScript is disabled, it's still possible to call the previous endpoint and download the file from the server.
122 lines
2.9 KiB
JavaScript
122 lines
2.9 KiB
JavaScript
const express = require('express')
|
|
const bodyParser = require('body-parser')
|
|
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 {
|
|
genericErrorHandler,
|
|
checkURLParameter,
|
|
forceSecure,
|
|
createRouteLogger,
|
|
createErrorLogger,
|
|
} = require('./middlewares')
|
|
const { createAppLogger } = require('./log-utils')
|
|
const { getNormalizedUrl } = require('./utils')
|
|
|
|
const port = process.env.PORT
|
|
const certEndpoint = process.env.CERT_ENDPOINT || ''
|
|
const certSecret = process.env.CERT_SECRET || ''
|
|
const isDevelopment = process.env.NODE_ENV === 'development'
|
|
const enforceHTTPS = Boolean(process.env.ENFORCE_HTTPS)
|
|
|
|
const app = express()
|
|
const appLogger = createAppLogger({ dev: isDevelopment })
|
|
const limiter = rateLimit({
|
|
windowMs: 60 * 1000,
|
|
max: 10,
|
|
})
|
|
const pkg = require('../package.json')
|
|
const version = pkg.version || ''
|
|
|
|
// Force app to always redirect to HTTPS
|
|
// use when you can't configure web server
|
|
if (enforceHTTPS) {
|
|
app.use(forceSecure)
|
|
}
|
|
|
|
// Server logs
|
|
// You can alternatively enable these to mimic logs created
|
|
// by your web server
|
|
if (isDevelopment) {
|
|
app.use(createRouteLogger({ dev: isDevelopment }))
|
|
}
|
|
|
|
app.set('view engine', 'ejs')
|
|
app.set('views', path.join(__dirname, 'views'))
|
|
app.set('trust proxy', 1)
|
|
|
|
app.use(express.static(path.join(__dirname, '..', 'dist')))
|
|
//app.use(favicon(path.join(__dirname, '..', 'dist', 'favicon.ico')))
|
|
app.use(bodyParser.urlencoded({ extended: true }))
|
|
|
|
const indexFile = path.join(__dirname, '..', 'dist', 'index.html')
|
|
|
|
if (certEndpoint) {
|
|
app.get(`/${certEndpoint}`, (req, res) => {
|
|
res.status(200).send(certSecret)
|
|
})
|
|
}
|
|
|
|
app.get('/error', (req, res) => {
|
|
const error = req.error || req.query.error || ''
|
|
|
|
res
|
|
.status(500)
|
|
.render('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.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) => {
|
|
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.use(createErrorLogger({ dev: isDevelopment }))
|
|
app.use(genericErrorHandler)
|
|
|
|
app.listen(port)
|