Add chat endpoint

Adds an endpoint for fetching the 10 most recent messages from #osu
This commit is contained in:
tgpethan 2021-01-26 12:29:15 +00:00
parent 4d062101f0
commit 776b06af36
2 changed files with 56 additions and 7 deletions

View file

@ -16,13 +16,29 @@ app.use((req, res) => {
req.on("end", () => {
switch (req.method) {
case "GET":
fs.readFile("serverPage.html", (err, data) => {
if (err) throw err;
if (debugMode) data = data.toString().replace("|isdebug?|", '<b style="color:red;">DEBUG</b>');
else data = data.toString().replace("|isdebug?|", '');
res.send(data);
});
if (req.url == "/" || req.url == "/index.html" || req.url == "/index.html") {
fs.readFile("./web/serverPage.html", (err, data) => {
if (err) throw err;
if (debugMode) data = data.toString().replace("|isdebug?|", '<b style="color:red;">DEBUG</b>');
else data = data.toString().replace("|isdebug?|", '');
res.send(data);
});
} else if (req.url == "/chat") {
fs.readFile("./web/chatPageTemplate.html", (err, data) => {
if (err) throw err;
let lines = "", flip = false;
const limit = global.chatHistory.length < 10 ? 10 : global.chatHistory.length;
for (let i = global.chatHistory.length - 10; i < limit; i++) {
if (i < 0) i = 0;
lines += `<div class="line line${flip ? 1 : 0}">${global.chatHistory[i] == null ? "<hidden>blank</hidden>" : global.chatHistory[i]}</div>`
flip = !flip;
}
res.send(data.toString().replace("|content|", lines));
});
}
break;
case "POST":

33
web/chatPageTemplate.html Normal file
View file

@ -0,0 +1,33 @@
<html>
<head>
<style>
.container {
width: 482px;
height: 165px;
}
hidden {
visibility: hidden;
}
.line {
padding: 2px;
font-size: 8pt;
font-family: sans-serif;
}
.line0 {
background-color: #edebfa;
}
.line1 {
background-color: #e3e1fa;
}
</style>
</head>
<body>
<div class="container">
|content|
</div>
</body>
</html>