diff options
author | Tom Smeding <t.j.smeding@uu.nl> | 2025-07-31 13:30:11 +0200 |
---|---|---|
committer | Tom Smeding <t.j.smeding@uu.nl> | 2025-07-31 13:30:11 +0200 |
commit | e6cd47ba5233dce8af2aacde02ca1533a25493be (patch) | |
tree | 1178df3ca96d3b241a87e808c6be4ff98fefd873 | |
parent | 8b3c8795cffe922bfc5ab5962dce19b4804abd68 (diff) |
-rw-r--r-- | modules/up_log/index.html | 37 | ||||
-rw-r--r-- | modules/up_log/up_log.js | 27 |
2 files changed, 62 insertions, 2 deletions
diff --git a/modules/up_log/index.html b/modules/up_log/index.html new file mode 100644 index 0000000..d8371d9 --- /dev/null +++ b/modules/up_log/index.html @@ -0,0 +1,37 @@ +<!doctype html> +<html> +<head> +<meta charset="utf-8"> +<title>Uplog</title> +<style> +#log { + border: 1px gray solid; +} +</style> +<script> +function get() { + var xhr = new XMLHttpRequest(); + xhr.open("GET", location.href + "/log/" + document.getElementById("count").value); + xhr.responseType = "text"; + xhr.onreadystatechange = function() { + if (xhr.readyState == 4) { + if (xhr.status == 200) { + document.getElementById("log").innerHTML = ""; + document.getElementById("log").appendChild(document.createTextNode(xhr.responseText)); + } else { + alert(xhr.responseText); + } + } + }; + xhr.send(); +} +</script> +</head> +<body> +Count: <input type="number" id="count" value="100" min="1" onchange="get()"> +<input type="button" onclick="get()" value="Get"> +<br> +<pre id="log"></pre> +<script>get();</script> +</body> +</html> diff --git a/modules/up_log/up_log.js b/modules/up_log/up_log.js index 609d705..61d2af0 100644 --- a/modules/up_log/up_log.js +++ b/modules/up_log/up_log.js @@ -4,7 +4,7 @@ const bodyParser = require("body-parser"); let moddir = null; -module.exports=function(app,io,_moddir){ +module.exports = function(app, io, _moddir) { moddir = _moddir; let config, accounts; @@ -15,7 +15,7 @@ module.exports=function(app,io,_moddir){ return false; } - app.post("/up_log/log", bodyParser.json(), cmn.authgen(accounts), (req,res) => { + app.post("/up_log/log", bodyParser.json(), cmn.authgen(accounts), (req, res) => { if (typeof req.body.machine != "string") { return res.sendStatus(400); } @@ -26,4 +26,27 @@ module.exports=function(app,io,_moddir){ res.status(200).end(); }); + + app.get("/up_log/log/:count", cmn.authgen(), (req, res) => { + const reqln = +req.params.count; + if (reqln < 0 || reqln % 1 != 0 || isNaN(reqln)) { res.sendStatus(400); return; } + + fs.readFile(moddir + "/log.txt", (err, data) => { + if (err) { res.sendStatus(500); return; } + + let i = data.length - 1 - (data[data.length-1] == 10); + let nln = 0; + while (true) { + if (data[i] == 10) { nln++; if (nln >= reqln) { i++; break; } } + i--; + if (i <= 0) { nln++; break; } + } + + res.send(data.slice(i)); + }); + }); + + app.get("/up_log", cmn.authgen(), (req, res) => { + res.sendFile(moddir + "/index.html"); + }); }; |