//const http = require("http"); const https = require("https"); 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/*, httpsKey, httpsCert*/] = await Promise.all([ openFile("./shifts.txt"), openFile("./classes.txt"), scrape.food(scrape.link(1)), scrape.food(scrape.link(2)), //openFile("../Certificate/key.pem"), //openFile("../Certificate/cert.pem") ]); // https options, you need to get a certificate in the file ../Certificate for the server to work const httpsOpts = { key: fs.readFileSync("../Certificate/key.pem"),//httpsKey, cert: fs.readFileSync("../Certificate/cert.pem")//httpsCert }; // 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 https.createServer(httpsOpts, 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) { // get the passed arguments const path = args["path"]; const query = args["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"); // here are the things to replace in the html page let res = {}; // get valid day const d = new Date(); let day = d.getDay(); let actualDay = day; day = +((day === 0) || (day === 6)) + (+(!(day === 0) && !(day === 6)) * day); // clamp the day between monday (1) and friday (5) (inclusive) // make the day the passed day instead if the passed day is valid 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); // set the day selected (must be done manually with this replacement system) data_string = data_string.replace(`