This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (81 loc) · 2.96 KB
/
index.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
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
const fs = require("fs");
const express = require('express')
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const app = express();
const port = 8080;
const lastResults = [];
let lastFetch = 0;
const githubCommitApis = [
"https://api.github.com/repos/Snail-IDE/snail-ide.github.io/commits?per_page=50",
"https://api.github.com/repos/Snail-IDE/Snail-IDE-Vm/commits?per_page=50",
"https://api.github.com/repos/Snail-IDE/Snail-IDE-Website/commits?per_page=50",
"https://api.github.com/repos/Snail-IDE/Snail-IDE-Paint/commits?per_page=50",
"https://api.github.com/repos/Snail-IDE/Snail-IDE-Packager/commits?per_page=50",
"https://api.github.com/repos/Snail-IDE/edu/commits?per_page=50",
"https://api.github.com/repos/Snail-IDE/Desktop-Download/commits?per_page=50",
"https://api.github.com/repos/Snail-IDE/examples/commits?per_page=50",
"https://api.github.com/repos/Snail-IDE/ext-create/commits?per_page=50",
"https://api.github.com/repos/Snail-IDE/Snail-IDE-ObjectLibraries/commits?per_page=50",
"https://api.github.com/repos/Snail-IDE/OpenSnail/commits?per_page=50",
"https://api.github.com/repos/Snail-IDE/SnailPy/commits?per_page=50"
]
function getDateMs(date) {
return (new Date(date)).getTime();
}
function commitSort(f, s) {
return getDateMs(s.commit.author.date) - getDateMs(f.commit.author.date);
}
app.use(cors({
origin: '*',
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}));
app.use(bodyParser.urlencoded({
limit: "25mb",
extended: false
}));
app.use(bodyParser.json({ limit: "25mb" }));
app.get('/', async function(req, res) {
res.status(200)
res.header("Content-Type", 'text/plain')
res.send("online")
})
app.get('/status', async function(req, res) {
res.status(200)
res.header("Content-Type", 'text/plain')
res.send("online")
})
app.get('/commits', async function(req, res) {
const difference = (Date.now() - lastFetch);
if ((difference <= 120000) && (lastResults.length > 0)) {
res.status(200)
res.json(lastResults.sort(commitSort))
return
}
try {
const fetchPromises = githubCommitApis.map(api => fetch(api));
const responses = await Promise.all(fetchPromises);
lastResults.splice(0, lastResults.length);
for (let i = 0; i < responses.length; i++) {
const response = responses[i];
if (response.ok) {
const json = await response.json();
json.forEach(commit => {
lastResults.push(commit);
});
} else {
const text = await response.text();
console.log("error getting commits;", text);
}
}
console.log("got new commits")
lastFetch = Date.now()
res.status(200)
res.json(lastResults.sort(commitSort))
} catch (error) {
console.error("Error fetching commits:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.listen(port, () => console.log('Started server on port ' + port));