From 8edc6724ea246ed32b9c0bd7cfd97e9b70245777 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Fri, 13 May 2022 12:57:58 +0300 Subject: Changed ports to 443 AND 80. Added improper error handling when there are stats already for the day when adding them at exit. --- server.js | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/server.js b/server.js index 79fc50c..d112d76 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,6 @@ //const http = require("http"); const https = require("https"); +const http = require("http"); const url = require("url"); const food = require("./food.js"); const SQL_DBS = require("./database.js"); @@ -96,34 +97,40 @@ async function init() res.end(); } - // start server - const runningServer = https.createServer(httpsOpts, server).listen(8080); + // start servers + const httpsServer = https.createServer(httpsOpts, server).listen(443); + const httpServer = http.createServer(server).listen(80); // stop server - async function closeServer() { + async function closeServers() { console.log("Updating stats to DB...") const uptime = Math.ceil((((new Date()).getTime() - startDate.getTime()) / 1000) / (24 * 60 * 60)); const monthOfStart = `${startDate.getMonth() + 1}`.padStart(2, "0"); const monthDayOfStart = `${startDate.getDate()}`.padStart(2, "0"); - await SQLDB.query("INSERT INTO stats VALUES (?, ?, ?, ?)", [ - `${startDate.getFullYear()}-${monthOfStart}-${monthDayOfStart}`, - uptime, - visitorCount, - Math.round(visitorCount / uptime) - ]); + try { + await SQLDB.query("INSERT INTO stats VALUES (?, ?, ?, ?)", [ + `${startDate.getFullYear()}-${monthOfStart}-${monthDayOfStart}`, + uptime, + visitorCount, + Math.round(visitorCount / uptime) + ]); + } catch(e) { + console.log(`\nERROR! Probably because updating the statistics several times a day is not supported, at least yet. Here's the error:\n${e}\n`); + } console.log("Done. Shutting down..."); await SQLDB.close(); - console.log("MySQL closed"); - runningServer.close(); - console.log("Server shut down"); + console.log("MySQL connection closed"); + httpsServer.close(); + httpServer.close(); + console.log("Servers shut down"); console.log("Process exiting..."); process.exit(0); } - process.on("SIGINT", closeServer); - process.on("SIGQUIT", closeServer); - process.on("SIGTERM", closeServer); + process.on("SIGINT", closeServers); + process.on("SIGQUIT", closeServers); + process.on("SIGTERM", closeServers); } -- cgit v1.2.3 From 94d4ec42b52252a4956e633074e92ebfdd49f596 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Fri, 13 May 2022 14:37:25 +0300 Subject: Made init work as sudo so that the server gets permissions to lower ports --- README.md | 18 +++++++----------- init.sh | 8 +++----- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 53cc75e..dff9ac0 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Follow these steps to install everything required for the project to run. ## Install the FoodJS repository -Just clone it from github. If you want the server to be able to update itself automatically without modifying `init.sh`, you will have to do this in your home folder. +Just clone it from github. ``` git clone "https://github.com/JoelHMikael/FoodJS.git" ``` @@ -134,18 +134,14 @@ INSERT INTO devs (name, description, contact) VALUES ('[name]', '[description]', > Insert the values in the quotation marks, don't change the text before the `VALUES` keyword. # Automatic server code updates -You can make the server update itself from github. To make this work, move `~/FoodJS/init.sh` to `~/init.sh`: -``` -mv ~/FoodJS/init.sh ~/init.sh -``` -This script should run at reboot, so next up you will have to set up some cronjobs: +You can make the server update itself from github. The code that updates everything is in the FoodJS folder (top-level of repository) in the file `init.sh`. It should run at reboot and it should have the environment variable PATH_TO_LYLLRUOKA set pointing to the FoodJS folder. -To your crontab: -``` -@reboot ~/init.sh -``` -To the crontab of **root** (you may have to update the shutdown path): +Add to **roots** crontab (run `sudo crontab -e`): ``` +@reboot PATH_TO_LYLLRUOKA=[the path] [the path]/init.sh +# For example: +# @reboot PATH_TO_LYLLRUOKA='/home/foobar/FoodJS' /home/foobar/FoodJS/init.sh + 0 0 * * 7 /sbin/shutdown -r ``` If you need to troubleshoot the initialization, you can find both normal and error logs in /tmp/slogs (text file) \ No newline at end of file diff --git a/init.sh b/init.sh index 8ff0aae..dfca76c 100755 --- a/init.sh +++ b/init.sh @@ -4,10 +4,8 @@ exec 2>>/tmp/slogs echo | date echo "Init running" -cd ~ - echo "Removing old packages..." -rm -rf ./FoodJS +rm -rf $PATH_TO_LYLLRUOKA echo "Waiting for connection..." while [ ! "$(ping 'www.github.com' -c 1)" ] @@ -19,5 +17,5 @@ echo "Cloning new packages..." git clone "https://github.com/JoelHMikael/FoodJS.git" echo "Starting server..." -cd ~/FoodJS -node ./server.js +cd $PATH_TO_LYLLRUOKA +node server.js -- cgit v1.2.3 From 95719c45251edab1af535d963065384ed20cadf5 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Fri, 13 May 2022 15:47:50 +0300 Subject: Lol I just need to upload these changes to not loose them while testing --- README.md | 4 ++-- init.sh | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index dff9ac0..1110518 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ INSERT INTO devs (name, description, contact) VALUES ('[name]', '[description]', > Insert the values in the quotation marks, don't change the text before the `VALUES` keyword. # Automatic server code updates -You can make the server update itself from github. The code that updates everything is in the FoodJS folder (top-level of repository) in the file `init.sh`. It should run at reboot and it should have the environment variable PATH_TO_LYLLRUOKA set pointing to the FoodJS folder. +You can make the server update itself from github. The code that updates everything is in the FoodJS folder (top-level of repository) in the file `init.sh`. It should run at reboot and it should have the environment variable PATH_TO_LYLLRUOKA set pointing to the FoodJS folder **without** a trailing backslash. Add to **roots** crontab (run `sudo crontab -e`): ``` @@ -144,4 +144,4 @@ Add to **roots** crontab (run `sudo crontab -e`): 0 0 * * 7 /sbin/shutdown -r ``` -If you need to troubleshoot the initialization, you can find both normal and error logs in /tmp/slogs (text file) \ No newline at end of file +If you need to troubleshoot the initialization, you can find both normal and error logs in /tmp/slogs (text file) diff --git a/init.sh b/init.sh index dfca76c..fce90af 100755 --- a/init.sh +++ b/init.sh @@ -5,7 +5,9 @@ echo | date echo "Init running" echo "Removing old packages..." -rm -rf $PATH_TO_LYLLRUOKA +rm -rf "${PATH_TO_LYLLRUOKA%%FoodJS}" + +cd "${PATH_TO_LYLLRUOKA%%FoodJS}" echo "Waiting for connection..." while [ ! "$(ping 'www.github.com' -c 1)" ] @@ -16,6 +18,6 @@ done echo "Cloning new packages..." git clone "https://github.com/JoelHMikael/FoodJS.git" -echo "Starting server..." cd $PATH_TO_LYLLRUOKA +echo "Starting server..." node server.js -- cgit v1.2.3 From b05d15e4e2771b31881fe8f0cf4c1deb8a2f4b58 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Fri, 13 May 2022 16:39:10 +0300 Subject: Fixed big bad bug. --- init.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/init.sh b/init.sh index fce90af..51791d6 100755 --- a/init.sh +++ b/init.sh @@ -5,9 +5,7 @@ echo | date echo "Init running" echo "Removing old packages..." -rm -rf "${PATH_TO_LYLLRUOKA%%FoodJS}" - -cd "${PATH_TO_LYLLRUOKA%%FoodJS}" +rm -rf $PATH_TO_LYLLRUOKA echo "Waiting for connection..." while [ ! "$(ping 'www.github.com' -c 1)" ] @@ -15,6 +13,8 @@ do sleep 5 done +cd "${PATH_TO_LYLLRUOKA%%FoodJS}" + echo "Cloning new packages..." git clone "https://github.com/JoelHMikael/FoodJS.git" -- cgit v1.2.3