From 07cf7cbe2d184c1c1d623d981d9a202d8223aeda Mon Sep 17 00:00:00 2001 From: tgpethan Date: Mon, 19 Aug 2019 03:37:02 +0100 Subject: [PATCH] General code sanity, Add some comments and rename ascii.lol to ascii.txt since it is not LOLCODE --- .gitignore | 1 + index.js | 81 +++++++++++++++++------------------ misc/{ascii.lol => ascii.txt} | 0 modules/request_handler.js | 7 ++- 4 files changed, 46 insertions(+), 43 deletions(-) rename misc/{ascii.lol => ascii.txt} (100%) diff --git a/.gitignore b/.gitignore index bf6937a..ae6b3c6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules/ package-lock.json config/config.json +utils/ \ No newline at end of file diff --git a/index.js b/index.js index 15bb92a..8e86eb9 100644 --- a/index.js +++ b/index.js @@ -1,52 +1,51 @@ -let dE = new Date(); -var startTime = dE.getTime(); -var endTime; - -const express = require("express"), -app = express(), -fs = require("fs"), -chalk = require("chalk"), -config = require("./config/config.json"); -let modules = {}; -console.clear(); - -fs.readFile('./misc/ascii.lol', function(err, data) { - if (err) { - throw err; - } - let asciiOut = data.toString() - .replace("|replaceVersion|", `${chalk.yellow("Version:")} ${chalk.cyan(internals.version)}`) - .replace("|titlecard|", chalk.yellow("The web server made for EUS")) - .replace("DEV", chalk.red("DEV")).replace("RELEASE", chalk.green("RELEASE")) - .replace("|replaceType|", `${chalk.yellow("Type: ")}${chalk.cyan(config.server.instance_type)}`); - console.log(asciiOut); - fs.readdir("./modules", (err, files) => { - for (var i = 0; i < files.length; i++) { - modules[files[i].toString().replace(".js", "")] = require(`./modules/${files[i].toString()}`); - console.log(`[Modules] Found module ${files[i].toString()}`) - } - modules.logger.log(internals.types.a, emoji.wave, "Starting Revolution..."); - server(); - }); -}); -const emoji = require("./misc/emoji_list.json"); -const internals = { +const express = require("express"), app = express(), fs = require("fs"), chalk = require("chalk"), config = require("./config/config.json"), emoji = require("./misc/emoji_list.json"), +internals = { version:"0.0.4 RELEASE", - instance:"Dev Instance", //Deprecated, does absolutely nothing. types: { a:"INFO", b:"REQUEST", c:"WARN" } -} +}; +let dE = new Date(), startTime = dE.getTime(), endTime, modules = []; + +// Clear console before printing anything +console.clear(); + +fs.readFile('./misc/ascii.txt', function(err, data) { + if (err) throw err; + // Generate the banner + let asciiOut = data.toString() + .replace("|replaceVersion|", `${chalk.yellow("Version:")} ${chalk.cyan(internals.version)}`) + .replace("|titlecard|", chalk.yellow("The web server made for EUS")) + .replace("DEV", chalk.red("DEV")).replace("RELEASE", chalk.green("RELEASE")) + .replace("|replaceType|", `${chalk.yellow("Type: ")}${chalk.cyan(config.server.instance_type)}`); + // Print the banner + console.log(asciiOut); + // Get the modules from the ./modules folder + fs.readdir("./modules", (err, files) => { + if (err) throw err; + for (var i = 0; i < files.length; i++) { + /* + For every file in the array, output that it was found + in the console and attempt to load it using require. + Oh, and check that it has .js in it's file name! + */ + if (files[i].includes(".js")) { + modules[files[i].toString().replace(".js", "")] = require(`./modules/${files[i].toString()}`); + console.log(`[Modules] Found module ${files[i].toString()}`); + } else { + console.log(`[Modules] Found file ${files[i]}. It is not a module.`) + } + } + modules.logger.log(internals.types.a, emoji.wave, "Starting Revolution..."); + server(); + }); +}); function server() { - app.get('*', (req, res) => { - modules.request_handler.handle(modules, internals, emoji, req, res); - }); - app.listen(config.server.port, () => { - dE = new Date(); - endTime = dE.getTime(); + app.get('*', (req, res) => { modules.request_handler.handle(modules, internals, emoji, req, res); }); + app.listen(config.server.port, () => { dE = new Date(), endTime = dE.getTime(); modules.logger.log(internals.types.a, emoji.thumb_up, `Started Revolution on port ${config.server.port}! Took ${endTime - startTime}ms`); }); } \ No newline at end of file diff --git a/misc/ascii.lol b/misc/ascii.txt similarity index 100% rename from misc/ascii.lol rename to misc/ascii.txt diff --git a/modules/request_handler.js b/modules/request_handler.js index 7d77993..645fe89 100644 --- a/modules/request_handler.js +++ b/modules/request_handler.js @@ -3,8 +3,11 @@ const fs = require("fs"); module.exports = { handle:function(modules, internals, emoji, req, res) { /* - req - Request from client - res - Response from server + modules - Modules that are loaded when Revolution starts + internals - Predefined variables, e.g the version and lables like "INFO" + emoji - Pretty self explanitory, the list of emojis used in Revolution. + req - Request from client + res - Response from server */ res.set("Server-Type", "Revolution"); if (req.url == "/") {