aboutsummaryrefslogtreecommitdiff
path: root/server.js
diff options
context:
space:
mode:
authorJoelHMikael <joel.h.kronqvist@gmail.com>2021-11-22 18:14:19 +0200
committerJoelHMikael <joel.h.kronqvist@gmail.com>2021-11-22 18:14:19 +0200
commit0925b5b6407e6464c738ecf98eb5b9a612a9b810 (patch)
tree09f0e95f9bb2686352c61549b3e96abd6223c68f /server.js
parentb16b58a24bdd20c40ecbc53fc07d84547825fc16 (diff)
downloadLYLLRuoka-0925b5b6407e6464c738ecf98eb5b9a612a9b810.tar.gz
LYLLRuoka-0925b5b6407e6464c738ecf98eb5b9a612a9b810.zip
Made server
It seems to work. All other things are in progress.
Diffstat (limited to 'server.js')
-rw-r--r--server.js143
1 files changed, 143 insertions, 0 deletions
diff --git a/server.js b/server.js
new file mode 100644
index 0000000..9d641eb
--- /dev/null
+++ b/server.js
@@ -0,0 +1,143 @@
+const http = require("http");
+const fs = require("fs");
+const url = require("url");
+
+mainPage = "./index.html";
+allowedPaths = [mainPage, "./index.css"];
+path404 = "./404/index.html";
+
+shifts = [
+ //1:
+ "Ruokailuvuoro 1",
+ //2:
+ "Ruokailuvuoro 2",
+ //3:
+ "Ruokailuvuoro 3"
+];
+
+
+http.createServer((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).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();
+ }
+ );
+ }
+}).listen(8080)
+
+function openFile(path)
+{
+ return new Promise((resolve, reject) =>
+ {
+ fs.readFile(path, (err, data) =>
+ {
+ if (err)
+ reject(err);
+ resolve(data);
+ return data;
+ })
+ });
+}
+
+function buildMain(query, path)
+{
+ 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) - 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)
+{
+ //get index type
+ is_teacher = isNaN(parseInt(index[index.length - 1]));
+ is_course = !is_teacher;
+ //read shiftfile
+ openFile("shifts.txt").then(
+ data => {}
+ );
+ //iterate over lines, search for day
+ //iterate shifts for course / teacher
+ //return the shift number
+ return 1;
+}
+
+function getCharAmount(s, c)
+{
+ let n = 0;
+ for (let c_i = 0; c_i < s.length; c_i++)
+ {
+ n += +(s[c_i] === c);
+ }
+ return n;
+}