From 24709fa862636702fd0430e832463b751fce323e Mon Sep 17 00:00:00 2001 From: JoelHMikael Date: Sat, 8 Jan 2022 16:49:37 +0200 Subject: Parsing the shifts (not classes) to the database + some GUI improvements --- server.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 68 insertions(+), 14 deletions(-) (limited to 'server.js') 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, ["<", ">", "(", ")"], ["<", ">", "(", ")"]); } +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(`