-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
69 lines (56 loc) · 1.86 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const cron = require("node-cron");
const fetchPapers = require("./services/fetchPapers");
const summarizePapers = require("./services/summarizePapers");
const sendNewsletter = require("./services/sendNewsletter");
const subscribe = require("./routes/subscribe");
const path = require("path");
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.static("public"));
app.use("/subscribe", subscribe);
app.get("/fetch-papers", async (req, res) => {
try {
const papers = await fetchPapers();
res.json(papers);
} catch (err) {
res.status(500).json({ message: err.message });
}
});
cron.schedule(
"0 8 * * 1-5",
async () => {
const papers = await fetchPapers();
const summaries = await summarizePapers(papers);
await sendNewsletter(summaries);
},
{
timezone: "America/New_York",
}
);
app.get("/summarize-papers", async (req, res) => {
try {
const papers = await fetchPapers();
// Set the headers to create a Server-Sent Events connection
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
await summarizePapers(papers, (summary) => {
// Send the summary as a Server-Sent Event
res.write(`data: ${JSON.stringify(summary)}\n\n`);
});
// End the response when all papers have been summarized
res.end();
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to summarize papers" });
}
});
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "public", "index.html"));
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));