55 lines
1.3 KiB
JavaScript
55 lines
1.3 KiB
JavaScript
import { Readability } from "@mozilla/readability";
|
|
import { JSDOM } from "jsdom";
|
|
import express from "express";
|
|
import morgan from "morgan";
|
|
import bodyParser from "body-parser";
|
|
import createDOMPurify from "dompurify";
|
|
import * as Sentry from "@sentry/node";
|
|
import * as Tracing from "@sentry/tracing";
|
|
|
|
const app = express();
|
|
app.use(morgan('combined'));
|
|
app.use(bodyParser.text({
|
|
type: "text/html",
|
|
limit: '1mb',
|
|
}));
|
|
|
|
if (process.env.SENTRY_DSN) {
|
|
Sentry.init({
|
|
dsn: process.env.SENTRY_DSN,
|
|
integrations: [
|
|
new Sentry.Integrations.Http({tracing: true}),
|
|
new Tracing.Integrations.Express({app}),
|
|
],
|
|
tracesSampleRate: 0.2
|
|
});
|
|
|
|
app.use(Sentry.Handlers.requestHandler());
|
|
app.use(Sentry.Handlers.tracingHandler());
|
|
}
|
|
|
|
app.get('/status', (req, res) => {
|
|
res.type('text/plain').send('OK');
|
|
});
|
|
|
|
app.post('/readability', (req, res) => {
|
|
const doc = new JSDOM(req.body, {
|
|
url: req.params.url,
|
|
});
|
|
const reader = new Readability(doc.window.document);
|
|
const article = reader.parse();
|
|
const purifier = createDOMPurify(doc.window);
|
|
const cleaned = purifier.sanitize(article.content);
|
|
res.send(cleaned);
|
|
});
|
|
|
|
if (process.env.SENTRY_DSN) {
|
|
app.use(Sentry.Handlers.errorHandler());
|
|
}
|
|
|
|
const port = process.env.PORT || 4001;
|
|
// only accept local requests
|
|
app.listen(port, "localhost", () => {
|
|
console.log(`listening on port ${port}`);
|
|
});
|