diff options
Diffstat (limited to 'server.js')
-rw-r--r-- | server.js | 270 |
1 files changed, 45 insertions, 225 deletions
@@ -1,18 +1,14 @@ const http = require("http"); const fs = require("fs"); const url = require("url"); +const parse = require("./parse.js"); -let maxshifts = Infinity; function init() { - const mainPage = "./index.html"; - const allowedPaths = [mainPage, "./index.css"]; - const path404 = "./404/index.html"; - const weekdays = [undefined, "MAANANTAI", "TIISTAI", "KESKIVIIKKO", "TORSTAI", "PERJANTAI", undefined]; - shifts = [ + const shifts = [ //1: "Ruokailuvuoro 1", //2: @@ -21,105 +17,42 @@ function init() "Ruokailuvuoro 3" ]; - /*openFile("shifts.txt").then(data => - { - let shifts = 0; - let i = 0; - do - { - i = getToLineStartingWith(data.toString("utf-8"), i + 1, "RUOKAILUVUORO"); - shifts++; - } while (i !== -1) - maxshifts = shifts / 5; // 5 = number of days - });*/ - function server(req, res) + const build = { + "./index.html": buildMain, + "./index.css": buildDefault, + }; + const errorPath = "./404/index.html"; + + async function server(req, res) { let q = url.parse(req.url, true); //true? let path = "." + q.pathname; if (path == "./") path = "./index.html"; - console.log(path); - - if (!allowedPaths.includes(path)) - path = path404; - - switch(path) + let data; + const args = { + "path": path, + "query": q.query, + "shifts": shifts + }; + try { - case mainPage: - buildMain(q.query, path, maxshifts).then( - (data) => - { - res.write(data); - res.end(); - } - ); - break; - case path404: - build404(path, q.pathname).then( - data => - { - res.write(data); - res.end(); - } - ); - break; - default: - buildDefault(path).then( - data => - { - res.write(data); - res.end(); - } - ); + data = await build[path](args); } + catch (error) + { + if (!(error instanceof TypeError)) + console.log("ERROR!!! " + error); + data = await build404(errorPath, q.pathname); + } + res.write(data); + res.end(); } http.createServer(server).listen(8080); } -function server(req, res) -{ - let q = url.parse(req.url, true); //true? - let path = "." + q.pathname; - if (path == "./") - path = "./index.html"; - - console.log(path); - - if (!allowedPaths.includes(path)) - path = path404; - - switch(path) - { - case mainPage: - buildMain(q.query, path, maxshifts).then( - (data) => - { - res.write(data); - res.end(); - } - ); - break; - case path404: - build404(path, q.pathname).then( - data => - { - res.write(data); - res.end(); - } - ); - break; - default: - buildDefault(path).then( - data => - { - res.write(data); - res.end(); - } - ); - } -} function openFile(path) { @@ -130,154 +63,41 @@ function openFile(path) if (err) reject(err); resolve(data); - return data; }) }); } -function buildMain(query, path, maxshifts) -{ - return new Promise((resolve, reject) => - { - openFile(path).then( - data => - { - data_string = data.toString("utf-8"); - if (query.index === undefined) - resolve(data_string.replace("\\(result\\)", "")); - resolve(data_string.replace("\\(result\\)", shifts[parseshift(query, maxshifts) - 1])); - } - ); - }); -} - - -function build404(path, attemptpath) -{ - return new Promise((resolve, reject) => - { - openFile(path).then( - data => - { - data_string = data.toString("utf-8"); - resolve(data_string.replace("\\(path\\)", attemptpath)); - } - ); - }); -} - -function buildDefault(path) -{ - return new Promise((resolve, reject) => - { - openFile(path).then( - data => - { - resolve(data.toString("utf-8")); - } - ); - }); -} - -function parseshift(index, maxshifts) +async function buildMain(args) { - /*//get index type - let is_teacher = isNaN(parseInt(index[index.length - 1])); - let is_course = !is_teacher; - - //get day of week - const d = new Date(); - let day = weekdays[d.getDay()]; - if (day === undefined) - day = weekdays[1]; - - //read shiftfile - openFile("shifts.txt").then( - data => - { - data = data.toString("utf-8"); - // get to the position of the day - let i = getToLineStartingWith(data, 0, day); - if (i === -1) - return -1; - - // iterate through shifts - let shift = ""; - let shifts = 0; - while (shifts < maxshifts) // infinite loop if maxshift is infinity, FIX! - { - i = getToLineStartingWith(data, i, "RUOKAILUVUORO"); - let nextLineStart = getNextChar(data, i, "\n"); - shift = data.substring(i, nextLineStart); - - // Get to the line with the teachers & courses - i = nextLineStart; - while (!((nextLineStart - i) > 2)) - { - i = nextLineStart; - nextLineStart = getNextChar(data, i + 1, "\n"); - } - i++; - // Find whether the course is in the line or not - let parsed_line = data.substring(i + 1, nextLineStart).replaceAll(",", ""); - if (findExpression(parsed_line, index, i) !== -1) - return shift; - shifts++; - } - return -1; - } - ); - //iterate over lines, search for day - //iterate shifts for course / teacher - //return the shift number*/ - return 1; + const path = args["path"]; + const query = args["query"]; + const shifts = args["shifts"]; + + const data = await openFile(path); + const data_string = data.toString("utf-8"); + if (query.index === undefined) + return data_string.replace("\\(result\\)", ""); + return data_string.replace("\\(result\\)", shifts[parseshift(query) - 1]); } -function getCharAmount(s, c) +async function build404(path, attemptpath) { - let n = 0; - for (let c_i = 0; c_i < s.length; c_i++) - { - n += +(s[c_i] === c); - } - return n; + const data = await openFile(path); + const data_string = data.toString("utf-8"); + return data_string.replace("\\(path\\)", attemptpath); } -function getNextChar(s, i, c) +async function buildDefault(args) { - for (; i < s.length; i++) - { - if (s[i] === c) - return i; - } - return -1; + const path = args["path"]; + const data = await openFile(path); + return data.toString("utf-8"); } -function getToLineStartingWith(s, start, ss) +function parseshift(index) { - let i = start; - do - { - if (s.substr(i, ss.length) === ss) - break; - i = getNextChar(s, i, "\n") + 1; - } while(i !== -1) - - if (i === -1) - return -1; - return i; - + return 1; // placeholder glue } -function findExpression(data, expr, start = 0) -{ - if (Number.isInteger(start) && (start < 0)) - throw new TypeError("Start must be a positive integer!"); - while ((data.substr(start, expr.length) !== expr) && (start + expr.length < data.length)) - start++; - if (data.substr(start, expr.length) !== expr) - return -1; - return start; -} init(); |