const http = require("http"); const fs = require("fs"); const url = require("url"); const parse = require("./parse.js"); const scrape = require("./scrape.js"); async function init() { const weekdays = [undefined, "MAANANTAI", "TIISTAI", "KESKIVIIKKO", "TORSTAI", "PERJANTAI", undefined]; const build = { "./index.html": buildMain, "./index.css": buildDefault, "./404/index.css": buildDefault }; const errorPath = "./404/index.html"; // await for needed things in async let [shiftCont, classCont, foodsThisWeek, foodsNextWeek] = await Promise.all([ openFile("./shifts.txt"), openFile("./classes.txt"), scrape.food(scrape.link(1)), scrape.food(scrape.link(2)) ]); // 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", ""); let DB = parse.build(shiftCont); parse.classes(classCont, DB); // get the food "database" const foods = [foodsThisWeek, foodsNextWeek]; // server code async function server(req, res) { let q = url.parse(req.url, true); let path = "." + q.pathname; if (path == "./") path = "./index.html"; let data; const args = { "path": path, "query": q.query, "db": DB, "foods": foods }; if (typeof build[path] === "function") data = await build[path](args); else data = await build404(errorPath, q.pathname); res.write(data); res.end(); } // start server http.createServer(server).listen(8080); } function openFile(path) { return new Promise((resolve, reject) => { fs.readFile(path, (err, data) => { if (err) reject(err); resolve(data); }) }); } async function buildMain(args) { const path = args["path"]; const query = args["query"]; console.log(query); const foods = args["foods"]; let index; if (typeof query.index === "string") index = parse.cluttered(query.index); const DB = args["db"]; const data = await openFile(path); let data_string = data.toString("utf-8"); let res = {}; const d = new Date(); 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)) day = parseInt(query.day); data_string = data_string.replace(`