-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
126 lines (113 loc) · 3.03 KB
/
index.ts
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { subHours, format } from "date-fns";
import { MongoClient } from "mongodb";
interface Timespan {
start: string;
end: string;
}
interface ActuData {
ndocs: number;
articles: any[];
count: number;
timespan: Timespan;
}
const uri = process.env.MONGO_URI || "mongodb://127.0.0.1:27017";
const timestamp = () => `[${new Date().toUTCString()}]`;
const tsLog = (...args: any[]) => console.log(timestamp(), ...args);
async function getData(
start: Date,
end: Date,
txtquery: string | null
): Promise<ActuData> {
const client = new MongoClient(uri);
const db = client.db("actur");
const articles = db.collection("articles");
const ndocs = await articles.estimatedDocumentCount();
let data: any;
let query = {};
if (txtquery === null || txtquery === undefined || txtquery.length === 0) {
query = { pubdate: { $gte: start, $lt: end } };
} else {
tsLog("txtquery", txtquery);
query = {
pubdate: { $gte: start, $lt: end },
$text: {
$search: txtquery,
$caseSensitive: false,
$diacriticSensitive: false,
},
};
}
try {
data = await articles
.find(
// { pubdate: { $gte: start, $lt: end } },
query,
{
projection: {
_id: 0,
title: 1,
summary: 1,
pubdate: 1,
pubname: 1,
link: 1,
hash: 1,
cat: 1,
},
}
)
.sort({ pubdate: -1 })
.toArray();
} catch (error) {
tsLog("Mongodb error:", error);
} finally {
// console.log("closing connection");
await client.close();
}
// console.log("debug getData", data.length);
return {
ndocs: ndocs,
articles: data,
count: data.length,
timespan: {
start: format(start, "EEE HH:mm O"),
end: format(end, "EEE HH:mm O"),
},
};
}
const server = Bun.serve({
port: 33433, // defaults to $BUN_PORT, $PORT, $NODE_PORT otherwise 3000
hostname: "localhost",
// hostname: "0.0.0.0",
fetch(req) {
// console.log("request", req);
const url = new URL(req.url);
const sparams = url.searchParams;
// console.log("headers", req.headers);
// console.log("timeframe", sparams);
const timeframe = sparams.get("timeframe") || "0";
const timewindow = sparams.get("timewindow") || "2";
const txtquery = sparams.get("txtquery");
tsLog("tq", txtquery);
// // db setup
// const timewindow = 2;
const tf = parseInt(timeframe, 10);
const tw = parseInt(timewindow, 10);
tsLog("params", tf, tw);
const now = new Date();
const end: Date = subHours(now, tf * tw);
const start: Date = subHours(end, tw);
// console.log("start/end", start, end);
const reply = getData(start, end, txtquery).then((res) =>
Response.json(res)
);
return reply;
},
error(error) {
return new Response(`<pre>${error}\n${error.stack}</pre>`, {
headers: {
"Content-Type": "text/html",
},
});
},
});
tsLog(`ActuProxy serving on ${server.hostname}:${server.port}`);