From 7807fccd8aa547bc9a5831660ada30a7562d2a38 Mon Sep 17 00:00:00 2001 From: tgpethan Date: Sat, 4 Jan 2020 12:08:03 +0000 Subject: [PATCH 1/8] Add comments to index.js --- index.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index e7c50c5..9219dd4 100644 --- a/index.js +++ b/index.js @@ -12,11 +12,15 @@ const requiredModules = [ new reqMod("handle_console", false) ]; let config; +// Statup so sync stuff is fine +// Check if config exists if (fs.existsSync("./config/config.json")) { + // It exists, load it. config = JSON.parse(fs.readFileSync("./config/config.json")); } else { console.log("[Config] Config file doesn't exist! You probably haven't copied the example config in the config directory."); console.log("[Config] Exiting..."); + // It doesn't exist, exit the framework and tell the user what to do process.exit(1); } global.actualDir = __dirname; @@ -26,6 +30,7 @@ global.internals = { global.app = express(); global.modules = []; let dE = new Date(), +// Get the time at invocation startTime = dE.getTime(), endTime, reqhandler; @@ -33,6 +38,7 @@ reqhandler; // Clear console before printing anything console.clear(); +// Read the server header for output fs.readFile('./misc/ascii.txt', function(err, data) { if (err) throw err; // Generate and Print the banner @@ -47,6 +53,7 @@ fs.readFile('./misc/ascii.txt', function(err, data) { Oh, and check that it has .js in it's file name! */ try { + // Make sure the file has the extention of .js if (files[i].includes(".js")) { console.log(`[Modules] Found module ${files[i].toString()}`); global.modules[files[i].toString().replace(".js", "")] = require(`./modules/${files[i].toString()}`); @@ -73,14 +80,17 @@ fs.readFile('./misc/ascii.txt', function(err, data) { // Check if all the required modules flags are set let allRequiredExist = []; for (var i = 0; i < requiredModules.length; i++) { + // Push the status of the required modules to an array allRequiredExist.push(requiredModules[i].status); } + // Make sure all required modules are found if (allRequiredExist.length !== requiredModules.length) { - // Exit if all required modules are not found + // They are not all found, exit framework with code 1. console.log("[Modules] All required modules could not be found."); console.log("[Modules] Server will not start until all required modules are found."); process.exit(1); } else { + // All required modules are found, start the framework's server. global.modules.consoleHelper.printInfo(emoji.wave, "Starting Revolution..."); server(); } @@ -88,11 +98,16 @@ fs.readFile('./misc/ascii.txt', function(err, data) { }); function server() { + // Load in the request handler's extra required items. reqhandler.extras(); + // Define where GET requests go to in the request handlers. app.get('*', (req, res) => reqhandler.get(req, res)); + // Define where POST requests go to in the request handlers. app.post('*', (req, res) => reqhandler.post(req, res)); + // Start the server listening at the port defined in the config file app.listen(config.server.port, () => { dE = new Date(), + // Get time after server has started listening or the "end time". endTime = dE.getTime(); global.modules.consoleHelper.printInfo(emoji.thumb_up, `Started Revolution on port ${config.server.port}! Took ${endTime - startTime}ms`); }); From c09a0b943b83c76f1ceb2cf4a280fe5da2e0cc0f Mon Sep 17 00:00:00 2001 From: tgpethan Date: Sat, 4 Jan 2020 12:10:23 +0000 Subject: [PATCH 2/8] Add a missed comment to index.js I forgot to save. --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 9219dd4..623d20e 100644 --- a/index.js +++ b/index.js @@ -114,6 +114,7 @@ function server() { } function highlightHeader(s) { + // Add the appropriate colours to the header and add information to it const s1 = s.toString().replace("|replaceVersion|", `${chalk.yellow("Version:")} ${chalk.cyan(internals.version)}`) .replace("|titlecard|", chalk.yellow("A modular and fexible server")) .replace("|replaceType|", `${chalk.yellow("Instance: ")}${chalk.cyan(config.server.instance_type)}`); From c25c8d75d8ce9b7ae3f77bc661c9dc74fa699e57 Mon Sep 17 00:00:00 2001 From: tgpethan Date: Sat, 4 Jan 2020 12:14:45 +0000 Subject: [PATCH 3/8] Add comments to consoleHelper --- modules/consoleHelper.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/modules/consoleHelper.js b/modules/consoleHelper.js index 1a8ddcf..c0f2c48 100644 --- a/modules/consoleHelper.js +++ b/modules/consoleHelper.js @@ -13,6 +13,7 @@ if (!fs.existsSync(__dirname + BASE_PATH)) { fs.mkdirSync(__dirname + BASE_PATH); console.log(`[consoleHelper] Made consoleHelper module folder`); } +// Creates the consoleHelper config file if (!fs.existsSync(__dirname + BASE_PATH + "/config.json")) { fs.writeFileSync(__dirname + BASE_PATH + "/config.json", JSON.stringify({"24hour":true})); console.log(`[consoleHelper] Made consoleHelper config file`); @@ -35,20 +36,26 @@ module.exports = { getTime24:function() { const time = new Date(); + // Check if the user wants 24 hour or 12 hour time if (config["24hour"]) { + // User wants 24 hour time, leave it as it is. return `${correctValue(time.getHours())}:${correctValue(time.getMinutes())}:${correctValue(time.getSeconds())}`; } else { + // User wants 12 hour time, process it for that. return this.t2412(`${correctValue(time.getHours())}:${correctValue(time.getMinutes())}:${correctValue(time.getSeconds())}`); } }, t2412:function(inp) { + // Check what time it is, AM or PM. if (parseInt(inp.split(":")[0]) > 11) { + // It's over 11 so it's PM const i = inp.split(":"); let i1 = parseInt(i[0]) - 12; if (i1 == 0) i1 = 12; return i1 + ":" + i[1] + " PM"; } else { + // It's lower than 12 so it's AM const i = inp.split(":"); let i1 = parseInt(i[0]); if (i1 == 0) i1 = 12; @@ -59,6 +66,7 @@ module.exports = { module.exports.MOD_FUNC = MODULE_FUNCTION; +// Date returns numbers without a 0 in front of them of course so this adds them. function correctValue(i) { if (i < 10) { return "0"+i; From a775a4c988a3a55c4b1d4436053c2997e76563e7 Mon Sep 17 00:00:00 2001 From: tgpethan Date: Sat, 4 Jan 2020 12:16:38 +0000 Subject: [PATCH 4/8] Add comments to example_request_handler --- modules/example_request_handler.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/example_request_handler.js b/modules/example_request_handler.js index b5d458d..b06f418 100644 --- a/modules/example_request_handler.js +++ b/modules/example_request_handler.js @@ -29,11 +29,14 @@ module.exports = { res - Response from server */ + // Make sure the file exists fs.access(__dirname + BASE_PATH + "/files" + req.url, fs.F_OK, error => { if (error) { + // File doesn't exist, return a 404 to the client. global.modules.consoleHelper.printWarn(emoji.page, `${req.method}: ${req.url} was requested - Returned 404`); res.status(404).send("404!
Revolution"); } else { + // File does exist, send the file back to the client. global.modules.consoleHelper.printInfo(emoji.page, `${req.method}: ${req.url} was requested`); res.sendFile(__dirname + BASE_PATH + "/files" + req.url); } From ea15b7dcb2b0874caf2128063f38d9f92aee6d70 Mon Sep 17 00:00:00 2001 From: tgpethan Date: Sat, 4 Jan 2020 12:23:30 +0000 Subject: [PATCH 5/8] Change and Add some comments to framework --- index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 623d20e..eaad2ee 100644 --- a/index.js +++ b/index.js @@ -2,12 +2,14 @@ const express = require("express"), fs = require("fs"), chalk = require("chalk"), emoji = require("./misc/emoji_list.json"); +// This is a class that contains the name and status of required modules class reqMod { constructor(name, status) { this.name = name; this.status = status; } } +// Array containing a list of required modules and their flags const requiredModules = [ new reqMod("handle_console", false) ]; @@ -40,11 +42,13 @@ console.clear(); // Read the server header for output fs.readFile('./misc/ascii.txt', function(err, data) { + // Make sure there are no errors if (err) throw err; // Generate and Print the banner console.log(highlightHeader(data)); // Get the modules from the ./modules folder fs.readdir("./modules", (err, files) => { + // Make sure there are no errors if (err) throw err; for (var i = 0; i < files.length; i++) { /* @@ -59,7 +63,9 @@ fs.readFile('./misc/ascii.txt', function(err, data) { global.modules[files[i].toString().replace(".js", "")] = require(`./modules/${files[i].toString()}`); // Loop through and set the required modules flags for (var i1 = 0; i1 < requiredModules.length; i1++) { + // Check if this module is a required module if (global.modules[files[i].toString().replace(".js", "")].MOD_FUNC == requiredModules[i1].name) { + // It is a required module, set the status flag of this one to true requiredModules[i1].status = true; } } @@ -69,7 +75,10 @@ fs.readFile('./misc/ascii.txt', function(err, data) { reqhandler = global.modules[files[i].toString().replace(".js", "")]; } } else { + // File is not a .js file (module) + // Make sure the file is not a directory if (files[i].split(".").length < 2) continue; + // Inform user that a file that was found in the modules folder is not a module console.log(`[Modules] Found file ${files[i]}. It is not a module.`) } } catch (err) { @@ -85,9 +94,10 @@ fs.readFile('./misc/ascii.txt', function(err, data) { } // Make sure all required modules are found if (allRequiredExist.length !== requiredModules.length) { - // They are not all found, exit framework with code 1. + // Inform the user that not all required modules are found. console.log("[Modules] All required modules could not be found."); console.log("[Modules] Server will not start until all required modules are found."); + // They are not all found, exit framework with code 1. process.exit(1); } else { // All required modules are found, start the framework's server. From 9bb52fcc8f603a12e1c35846cb74e6f5d7b4be62 Mon Sep 17 00:00:00 2001 From: tgpethan Date: Sat, 4 Jan 2020 12:27:12 +0000 Subject: [PATCH 6/8] Add some more comments to the consoleHelper --- modules/consoleHelper.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/consoleHelper.js b/modules/consoleHelper.js index c0f2c48..451f9ba 100644 --- a/modules/consoleHelper.js +++ b/modules/consoleHelper.js @@ -19,17 +19,21 @@ if (!fs.existsSync(__dirname + BASE_PATH + "/config.json")) { console.log(`[consoleHelper] Made consoleHelper config file`); } +// Load in config const config = require(__dirname + BASE_PATH + "/config.json"); module.exports = { + // Prints a bit of information to the console printInfo:function(emoji, s) { console.log(chalk.green(`[${this.getTime24()}] `)+chalk.bgGreen(chalk.black(" INFO "))+` ${emoji} ${s}`); }, + // Prints a warning to the console printWarn:function(emoji, s) { console.warn(chalk.green(`[${this.getTime24()}] `)+chalk.bgYellow(chalk.black(" WARN "))+` ${emoji} ${s}`); }, + // Prints an error to the console printError:function(emoji, s) { console.error(chalk.green(`[${this.getTime24()}] `)+chalk.bgRed(chalk.black(" ERROR "))+` ${emoji} ${s}`); }, @@ -46,6 +50,7 @@ module.exports = { } }, + // Function for converting 24 hour to 12 hour time t2412:function(inp) { // Check what time it is, AM or PM. if (parseInt(inp.split(":")[0]) > 11) { From 742f9a1ddef62b10c829fb7104b331358276a487 Mon Sep 17 00:00:00 2001 From: tgpethan Date: Sat, 4 Jan 2020 12:30:27 +0000 Subject: [PATCH 7/8] Rephrase and add comments to example_request_handler --- modules/example_request_handler.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/modules/example_request_handler.js b/modules/example_request_handler.js index b06f418..f5a84de 100644 --- a/modules/example_request_handler.js +++ b/modules/example_request_handler.js @@ -20,14 +20,20 @@ if (!fs.existsSync(__dirname + BASE_PATH + "/files")) { module.exports = { extras:async function() { - // Anything else that is needed like busboy - // Put them to global.app (the express app) + /* + Anything else that needs to be loaded into the framework + can be done here, this is used for things like busboy that + need to be put to the express server to work + The express server is accessable from global.app + */ }, get:async function(req, res) { /* req - Request from client res - Response from server */ + + // Anything that needs to be done in a GET request can be done here // Make sure the file exists fs.access(__dirname + BASE_PATH + "/files" + req.url, fs.F_OK, error => { @@ -48,7 +54,7 @@ module.exports = { res - Response from server */ - // Anything that needs to be done with a post can be done here. + // Anything that needs to be done with a POST can be done here. } } From c36d31863e73b2be9995cd0128722e0c9867458e Mon Sep 17 00:00:00 2001 From: tgpethan Date: Sat, 4 Jan 2020 15:16:08 +0000 Subject: [PATCH 8/8] Fix conflicts --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index eaad2ee..a321d93 100644 --- a/index.js +++ b/index.js @@ -107,7 +107,7 @@ fs.readFile('./misc/ascii.txt', function(err, data) { }); }); -function server() { +function frameworkServer() { // Load in the request handler's extra required items. reqhandler.extras(); // Define where GET requests go to in the request handlers.