diff --git a/index.js b/index.js index 260ea82..7228dc8 100644 --- a/index.js +++ b/index.js @@ -2,21 +2,27 @@ 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) ]; 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 +32,7 @@ global.internals = { global.app = express(); global.modules = []; let dE = new Date(), +// Get the time at invocation startTime = dE.getTime(), endTime, reqhandler; @@ -33,12 +40,15 @@ reqhandler; // Clear console before printing anything 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++) { /* @@ -47,6 +57,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()}`); @@ -59,12 +70,17 @@ fs.readFile('./misc/ascii.txt', function(err, data) { // 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; } } } 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) { @@ -75,14 +91,18 @@ 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 + // 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. global.modules.consoleHelper.printInfo(emoji.wave, "Starting Revolution..."); frameworkServer(); } @@ -90,17 +110,23 @@ fs.readFile('./misc/ascii.txt', function(err, data) { }); function frameworkServer() { + // 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`); }); } 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)}`); diff --git a/modules/consoleHelper.js b/modules/consoleHelper.js index 1a8ddcf..451f9ba 100644 --- a/modules/consoleHelper.js +++ b/modules/consoleHelper.js @@ -13,42 +13,54 @@ 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`); } +// 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}`); }, 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())}`); } }, + // 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) { + // 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 +71,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; diff --git a/modules/example_request_handler.js b/modules/example_request_handler.js index b5d458d..f5a84de 100644 --- a/modules/example_request_handler.js +++ b/modules/example_request_handler.js @@ -20,20 +20,29 @@ 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 => { 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); } @@ -45,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. } }