-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
50 lines (43 loc) · 1.28 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
import cron from "node-cron";
import run from "./count-contributors";
// Schedule of counting contributions every day at midnight
cron.schedule("0 0 * * *", async () => {
console.log("Started counting");
const data = await run();
console.log("Writing to files...");
await Bun.write("data.json", JSON.stringify(data));
console.log("Ended counting");
});
// TODO: add pagination
const server = Bun.serve({
port: process.env.PORT || 3000,
async fetch(_req) {
const dataFile = Bun.file("data.json", { type: "application/json" });
const dataString = await dataFile.text();
const data: any[] = JSON.parse(dataString);
const res = {
success: true,
message: "Success",
data: {
docs: data,
totalDocs: data.length,
limit: data.length,
page: 1,
totalPages: 1,
pagingCounter: 1,
hasPrevPage: false,
hasNextPage: false,
prevPage: null,
nextPage: null,
},
};
return new Response(JSON.stringify(res), {
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
},
});
},
});
console.log(`Listening on http://localhost:${server.port} ...`);