You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.7 KiB
JavaScript

#!/usr/bin/env node
'use strict';
const errorReporter = require("./src/error-reporting");
const path = require("path");
const express = require("express");
const expressWs = require("express-ws");
const config = require("./config.json");
const clientStore = require("./src/client-store")();
const scraper = require("./src/scraper")({
scrapePastebinCom: true,
scraperSettings: config.scraperSettings
});
scraper.on("newPaste", (paste) => {
clientStore.broadcast({
type: "newPaste",
data: paste
});
});
scraper.on("warning", (error) => {
console.error(error);
});
scraper.on("error", (error) => {
errorReporter.report(error, {
source: "scraper"
});
});
clientStore.on("error", (error) => {
errorReporter.report(error, {
source: "clientStore"
});
});
let app = express();
expressWs(app);
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug");
app.use(express.static(path.join(__dirname, "public")));
app.get("/", (req, res) => {
res.render("index");
});
app.ws("/stream", (ws, req) => {
let client = clientStore.add(ws);
client.on("message", (message) => {
if (message.type === "backlog") {
if (message.all === true) {
client.send({
type: "backlog",
results: scraper.pasteStore.all()
});
} else if (typeof message.since === "number") {
client.send({
type: "backlog",
results: scraper.pasteStore.since(message.since)
});
} else if (typeof message.last === "number") {
client.send({
type: "backlog",
results: scraper.pasteStore.last(message.last)
});
} else {
client.kill();
}
}
})
});
app.listen(3000, () => {
console.log("Listening on port 3000...");
});