-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframesvr.ts
97 lines (87 loc) · 2.41 KB
/
framesvr.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
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;
}
async function getData(start: Date, end: Date): Promise<ActuData> {
const uri = "mongodb://192.168.0.128:27017";
const client = new MongoClient(uri);
// console.log("conn: starting mongo client", uri, client);
const db = client.db("actur");
const articles = db.collection("articles");
const ndocs = await articles.countDocuments();
let data: any;
try {
data = await articles
.find(
{ pubdate: { $gte: start, $lt: end } },
{
projection: {
_id: 0,
title: 1,
summary: 1,
pubdate: 1,
pubname: 1,
link: 1,
hash: 1,
cat: 1,
},
}
)
.sort({ pubdate: -1 })
.toArray();
} catch (error) {
console.log("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: "0.0.0.0", // defaults to "0.0.0.0"
fetch(req) {
// console.log("request", req);
const url = new URL(req.url);
const sparams = url.searchParams;
// console.log("timeframe", sparams);
const timeframe = sparams.get("timeframe") || "0";
const timewindow = sparams.get("timewindow") || "2";
// // db setup
// const timewindow = 2;
const tf = parseInt(timeframe, 10);
const tw = parseInt(timewindow, 10);
// console.log("params", tf, tw);
const now = new Date();
const end: Date = subHours(now, tf * tw);
const start: Date = subHours(end, tw);
console.log("actuproxy: start/end:", start, end);
const reply = getData(start, end).then((res) => Response.json(res));
return reply;
},
error(error) {
return new Response(`<pre>${error}\n${error.stack}</pre>`, {
headers: {
"Content-Type": "text/html",
},
});
},
});
console.log(`ActuProxy serving on ${server.hostname}:${server.port}`);