aboutsummaryrefslogtreecommitdiff
path: root/server.js
diff options
context:
space:
mode:
authorJoelHMikael <joel.h.kronqvist@gmail.com>2022-01-08 16:49:37 +0200
committerJoelHMikael <joel.h.kronqvist@gmail.com>2022-01-08 16:49:37 +0200
commit24709fa862636702fd0430e832463b751fce323e (patch)
tree8bd4eabbf2c7fe4525f66bfe55e3c47994214d79 /server.js
parent4e5fe4711fe46a6af740aadbf23bde86bb5acf69 (diff)
downloadLYLLRuoka-24709fa862636702fd0430e832463b751fce323e.tar.gz
LYLLRuoka-24709fa862636702fd0430e832463b751fce323e.zip
Parsing the shifts (not classes) to the database + some GUI improvements
Diffstat (limited to 'server.js')
-rw-r--r--server.js82
1 files changed, 68 insertions, 14 deletions
diff --git a/server.js b/server.js
index 4b1e8a3..26ab9bb 100644
--- a/server.js
+++ b/server.js
@@ -3,7 +3,8 @@ const fs = require("fs");
const url = require("url");
const parse = require("./parse.js");
const scrape = require("./scrape.js");
-const mysql = require("mysql2");
+const SQL_DBS = require("./database.js");
+const DBPARSE = require("./dbparse.js");
async function init()
@@ -11,24 +12,36 @@ async function init()
const weekdays = [undefined, "MAANANTAI", "TIISTAI", "KESKIVIIKKO", "TORSTAI", "PERJANTAI", undefined];
const build = {
- "./index.html": buildMain,
- "./index.css": buildDefault,
- "./404/index.css": buildDefault,
- "./help.png": buildImage
+ "./Cont/index.html": buildMain,
+ "./Cont/index.css": buildDefault,
+ "./Cont/devs/index.html": buildDevs,
+ "./Cont/devs/index.css": buildDefault,
+ "./Cont/404/index.css": buildDefault,
+ "./Cont/non-main.css": buildDefault,
+ "./Cont/Images/help.png": buildImage,
+ "./Cont/Images/back.png": buildImage
};
- const errorPath = "./404/index.html";
+ const errorPath = "./Cont/404/index.html";
// await for needed things in async
- let [shiftCont, classCont, foodsThisWeek, foodsNextWeek] = await Promise.all([
+ let [shiftCont, classCont, foodsThisWeek, foodsNextWeek, dbcredentials] = await Promise.all([
openFile("./shifts.txt"),
openFile("./classes.txt"),
scrape.food(scrape.link(1)),
- scrape.food(scrape.link(2))
+ scrape.food(scrape.link(2)),
+ openFile("../dblogin.txt")
]);
+ // get the MySQL DB connection
+ const SQLDB = new SQL_DBS.Database(JSON.parse(dbcredentials));
+
// get the food shift "database"
shiftCont = shiftCont.toString("utf-8").replaceAll("\r", ""); // \r because of the \r\n newline on windows which creates problems
classCont = classCont.toString("utf-8").replaceAll("\r", "");
+
+ await DBPARSE.classes(classCont, SQLDB);
+ await DBPARSE.build(shiftCont, SQLDB);
+
let DB = parse.build(shiftCont);
parse.classes(classCont, DB);
@@ -54,9 +67,9 @@ async function init()
index: ind,
day: d
};
- let path = "." + antiXSS(q.pathname);
- if (path == "./")
- path = "./index.html";
+ let path = "./Cont" + antiXSS(q.pathname);
+ if (isDir(path))
+ path += ["/index.html", "index.html"][+(path[path.length - 1] === "/")];
// pack the data required by the builders
let data;
@@ -65,7 +78,8 @@ async function init()
"path404": errorPath,
"query": q.query,
"db": DB,
- "foods": foods
+ "foods": foods,
+ "sqldb": SQLDB
};
// build the page
@@ -77,10 +91,21 @@ async function init()
}
// start server
- http.createServer(server).listen(8080);
+ const runningServer = http.createServer(server).listen(8080);
+
+ // stop server
+ function closeServer() {
+ SQLDB.close();
+ runningServer.close();
+ }
+ process.on("SIGINT", closeServer);
+ process.on("SIGQUIT", closeServer);
+ process.on("SIGTERM", closeServer);
}
+
+
function validateIndex(sus)
{
return antiXSS(parse.cluttered(sus));
@@ -93,6 +118,12 @@ function antiXSS(sus)
return replace(sus, ["<", ">", "(", ")"], ["&lt;", "&gt;", "&#40;", "&#41;"]);
}
+function isDir(path)
+{
+ return (parse.getNextChar(path.substring(1), ".") === -1);
+}
+
+
function replace(s, from, to)
{
for (let i = 0; i < from.length; i++)
@@ -131,7 +162,7 @@ async function buildMain(args)
let day = d.getDay();
const actualDay = day;
day = +((day === 0) || (day === 6)) + (+(!(day === 0) && !(day === 6)) * day);
- if ((typeof query.day === "string") && (parseInt(query.day).toString() === query.day) && (!isNaN(parseInt(query.day))) && (parseInt(query.day) > 0) && (parseInt(query.day) < 7))
+ if ((typeof query.day === "string") && (parseInt(query.day).toString() === query.day) && (!isNaN(parseInt(query.day))) && (parseInt(query.day) > 0) && (parseInt(query.day) < 6))
day = parseInt(query.day);
data_string = data_string.replace(`<option value=\"${day}\">`, `<option value=\"${day}\" selected>`);
@@ -197,8 +228,31 @@ async function buildMain(args)
return data_string;
}
+async function buildDevs(args)
+{
+ const path = args["path"];
+ const data = await openFile(path);
+ const DB = args["sqldb"];
+
+ let res = "";
+ let devs = await DB.query_raw("SELECT name, description, contact FROM devs");
+ for (let dev = 0; dev < devs.length; dev++)
+ {
+ let devInfo = devs[dev];
+ res += '<div class="float-block">' +
+ `<p class="column">${devInfo.name}</p>` +
+ `<p class="column">${devInfo.description}</p>` +
+ `<a href="mailto:${devInfo.contact}" class="column" style="white-space: nowrap; overflow: hidden; overflow-wrap: normal; text-overflow: ellipsis;">${devInfo.contact}</a>` +
+ '</div>';
+ }
+
+ return build_replace(data.toString("utf-8"), {"devs": res});
+}
+
+
async function build404(args)
{
+ args["path"] = args["path"].substring("./Cont".length);
const data = await openFile(args["path404"]);
const data_string = data.toString("utf-8");
return data_string.replace("\\(path\\)", args["path"]);