From 776b06af36f279a65f07ccb1199b5230d94c0cdb Mon Sep 17 00:00:00 2001 From: tgpethan Date: Tue, 26 Jan 2021 12:29:15 +0000 Subject: [PATCH] Add chat endpoint Adds an endpoint for fetching the 10 most recent messages from #osu --- Binato.js | 30 +++++++++++++++++++++++------- web/chatPageTemplate.html | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 web/chatPageTemplate.html diff --git a/Binato.js b/Binato.js index f86b467..8ebff04 100644 --- a/Binato.js +++ b/Binato.js @@ -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?|", 'DEBUG'); - 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?|", 'DEBUG'); + 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 += `
${global.chatHistory[i] == null ? "blank" : global.chatHistory[i]}
` + flip = !flip; + } + + res.send(data.toString().replace("|content|", lines)); + }); + } break; case "POST": diff --git a/web/chatPageTemplate.html b/web/chatPageTemplate.html new file mode 100644 index 0000000..665d551 --- /dev/null +++ b/web/chatPageTemplate.html @@ -0,0 +1,33 @@ + + + + + +
+ |content| +
+ + \ No newline at end of file