Merge pull request #16 from tgpethan/add-comments
Add comments to the framework & included modules
This commit is contained in:
commit
6042c87d14
3 changed files with 52 additions and 4 deletions
28
index.js
28
index.js
|
@ -2,21 +2,27 @@ const express = require("express"),
|
||||||
fs = require("fs"),
|
fs = require("fs"),
|
||||||
chalk = require("chalk"),
|
chalk = require("chalk"),
|
||||||
emoji = require("./misc/emoji_list.json");
|
emoji = require("./misc/emoji_list.json");
|
||||||
|
// This is a class that contains the name and status of required modules
|
||||||
class reqMod {
|
class reqMod {
|
||||||
constructor(name, status) {
|
constructor(name, status) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.status = status;
|
this.status = status;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Array containing a list of required modules and their flags
|
||||||
const requiredModules = [
|
const requiredModules = [
|
||||||
new reqMod("handle_console", false)
|
new reqMod("handle_console", false)
|
||||||
];
|
];
|
||||||
let config;
|
let config;
|
||||||
|
// Statup so sync stuff is fine
|
||||||
|
// Check if config exists
|
||||||
if (fs.existsSync("./config/config.json")) {
|
if (fs.existsSync("./config/config.json")) {
|
||||||
|
// It exists, load it.
|
||||||
config = JSON.parse(fs.readFileSync("./config/config.json"));
|
config = JSON.parse(fs.readFileSync("./config/config.json"));
|
||||||
} else {
|
} else {
|
||||||
console.log("[Config] Config file doesn't exist! You probably haven't copied the example config in the config directory.");
|
console.log("[Config] Config file doesn't exist! You probably haven't copied the example config in the config directory.");
|
||||||
console.log("[Config] Exiting...");
|
console.log("[Config] Exiting...");
|
||||||
|
// It doesn't exist, exit the framework and tell the user what to do
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
global.actualDir = __dirname;
|
global.actualDir = __dirname;
|
||||||
|
@ -26,6 +32,7 @@ global.internals = {
|
||||||
global.app = express();
|
global.app = express();
|
||||||
global.modules = [];
|
global.modules = [];
|
||||||
let dE = new Date(),
|
let dE = new Date(),
|
||||||
|
// Get the time at invocation
|
||||||
startTime = dE.getTime(),
|
startTime = dE.getTime(),
|
||||||
endTime,
|
endTime,
|
||||||
reqhandler;
|
reqhandler;
|
||||||
|
@ -33,12 +40,15 @@ reqhandler;
|
||||||
// Clear console before printing anything
|
// Clear console before printing anything
|
||||||
console.clear();
|
console.clear();
|
||||||
|
|
||||||
|
// Read the server header for output
|
||||||
fs.readFile('./misc/ascii.txt', function(err, data) {
|
fs.readFile('./misc/ascii.txt', function(err, data) {
|
||||||
|
// Make sure there are no errors
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
// Generate and Print the banner
|
// Generate and Print the banner
|
||||||
console.log(highlightHeader(data));
|
console.log(highlightHeader(data));
|
||||||
// Get the modules from the ./modules folder
|
// Get the modules from the ./modules folder
|
||||||
fs.readdir("./modules", (err, files) => {
|
fs.readdir("./modules", (err, files) => {
|
||||||
|
// Make sure there are no errors
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
for (var i = 0; i < files.length; i++) {
|
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!
|
Oh, and check that it has .js in it's file name!
|
||||||
*/
|
*/
|
||||||
try {
|
try {
|
||||||
|
// Make sure the file has the extention of .js
|
||||||
if (files[i].includes(".js")) {
|
if (files[i].includes(".js")) {
|
||||||
console.log(`[Modules] Found module ${files[i].toString()}`);
|
console.log(`[Modules] Found module ${files[i].toString()}`);
|
||||||
global.modules[files[i].toString().replace(".js", "")] = require(`./modules/${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
|
// Loop through and set the required modules flags
|
||||||
for (var i1 = 0; i1 < requiredModules.length; i1++) {
|
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) {
|
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;
|
requiredModules[i1].status = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// File is not a .js file (module)
|
||||||
|
// Make sure the file is not a directory
|
||||||
if (files[i].split(".").length < 2) continue;
|
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.`)
|
console.log(`[Modules] Found file ${files[i]}. It is not a module.`)
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -75,14 +91,18 @@ fs.readFile('./misc/ascii.txt', function(err, data) {
|
||||||
// Check if all the required modules flags are set
|
// Check if all the required modules flags are set
|
||||||
let allRequiredExist = [];
|
let allRequiredExist = [];
|
||||||
for (var i = 0; i < requiredModules.length; i++) {
|
for (var i = 0; i < requiredModules.length; i++) {
|
||||||
|
// Push the status of the required modules to an array
|
||||||
allRequiredExist.push(requiredModules[i].status);
|
allRequiredExist.push(requiredModules[i].status);
|
||||||
}
|
}
|
||||||
|
// Make sure all required modules are found
|
||||||
if (allRequiredExist.length !== requiredModules.length) {
|
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] All required modules could not be found.");
|
||||||
console.log("[Modules] Server will not start until all required modules are 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);
|
process.exit(1);
|
||||||
} else {
|
} else {
|
||||||
|
// All required modules are found, start the framework's server.
|
||||||
global.modules.consoleHelper.printInfo(emoji.wave, "Starting Revolution...");
|
global.modules.consoleHelper.printInfo(emoji.wave, "Starting Revolution...");
|
||||||
frameworkServer();
|
frameworkServer();
|
||||||
}
|
}
|
||||||
|
@ -90,17 +110,23 @@ fs.readFile('./misc/ascii.txt', function(err, data) {
|
||||||
});
|
});
|
||||||
|
|
||||||
function frameworkServer() {
|
function frameworkServer() {
|
||||||
|
// Load in the request handler's extra required items.
|
||||||
reqhandler.extras();
|
reqhandler.extras();
|
||||||
|
// Define where GET requests go to in the request handlers.
|
||||||
app.get('*', (req, res) => reqhandler.get(req, res));
|
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));
|
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, () => {
|
app.listen(config.server.port, () => {
|
||||||
dE = new Date(),
|
dE = new Date(),
|
||||||
|
// Get time after server has started listening or the "end time".
|
||||||
endTime = dE.getTime();
|
endTime = dE.getTime();
|
||||||
global.modules.consoleHelper.printInfo(emoji.thumb_up, `Started Revolution on port ${config.server.port}! Took ${endTime - startTime}ms`);
|
global.modules.consoleHelper.printInfo(emoji.thumb_up, `Started Revolution on port ${config.server.port}! Took ${endTime - startTime}ms`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function highlightHeader(s) {
|
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)}`)
|
const s1 = s.toString().replace("|replaceVersion|", `${chalk.yellow("Version:")} ${chalk.cyan(internals.version)}`)
|
||||||
.replace("|titlecard|", chalk.yellow("A modular and fexible server"))
|
.replace("|titlecard|", chalk.yellow("A modular and fexible server"))
|
||||||
.replace("|replaceType|", `${chalk.yellow("Instance: ")}${chalk.cyan(config.server.instance_type)}`);
|
.replace("|replaceType|", `${chalk.yellow("Instance: ")}${chalk.cyan(config.server.instance_type)}`);
|
||||||
|
|
|
@ -13,42 +13,54 @@ if (!fs.existsSync(__dirname + BASE_PATH)) {
|
||||||
fs.mkdirSync(__dirname + BASE_PATH);
|
fs.mkdirSync(__dirname + BASE_PATH);
|
||||||
console.log(`[consoleHelper] Made consoleHelper module folder`);
|
console.log(`[consoleHelper] Made consoleHelper module folder`);
|
||||||
}
|
}
|
||||||
|
// Creates the consoleHelper config file
|
||||||
if (!fs.existsSync(__dirname + BASE_PATH + "/config.json")) {
|
if (!fs.existsSync(__dirname + BASE_PATH + "/config.json")) {
|
||||||
fs.writeFileSync(__dirname + BASE_PATH + "/config.json", JSON.stringify({"24hour":true}));
|
fs.writeFileSync(__dirname + BASE_PATH + "/config.json", JSON.stringify({"24hour":true}));
|
||||||
console.log(`[consoleHelper] Made consoleHelper config file`);
|
console.log(`[consoleHelper] Made consoleHelper config file`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load in config
|
||||||
const config = require(__dirname + BASE_PATH + "/config.json");
|
const config = require(__dirname + BASE_PATH + "/config.json");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
// Prints a bit of information to the console
|
||||||
printInfo:function(emoji, s) {
|
printInfo:function(emoji, s) {
|
||||||
console.log(chalk.green(`[${this.getTime24()}] `)+chalk.bgGreen(chalk.black(" INFO "))+` ${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) {
|
printWarn:function(emoji, s) {
|
||||||
console.warn(chalk.green(`[${this.getTime24()}] `)+chalk.bgYellow(chalk.black(" WARN "))+` ${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) {
|
printError:function(emoji, s) {
|
||||||
console.error(chalk.green(`[${this.getTime24()}] `)+chalk.bgRed(chalk.black(" ERROR "))+` ${emoji} ${s}`);
|
console.error(chalk.green(`[${this.getTime24()}] `)+chalk.bgRed(chalk.black(" ERROR "))+` ${emoji} ${s}`);
|
||||||
},
|
},
|
||||||
|
|
||||||
getTime24:function() {
|
getTime24:function() {
|
||||||
const time = new Date();
|
const time = new Date();
|
||||||
|
// Check if the user wants 24 hour or 12 hour time
|
||||||
if (config["24hour"]) {
|
if (config["24hour"]) {
|
||||||
|
// User wants 24 hour time, leave it as it is.
|
||||||
return `${correctValue(time.getHours())}:${correctValue(time.getMinutes())}:${correctValue(time.getSeconds())}`;
|
return `${correctValue(time.getHours())}:${correctValue(time.getMinutes())}:${correctValue(time.getSeconds())}`;
|
||||||
} else {
|
} else {
|
||||||
|
// User wants 12 hour time, process it for that.
|
||||||
return this.t2412(`${correctValue(time.getHours())}:${correctValue(time.getMinutes())}:${correctValue(time.getSeconds())}`);
|
return this.t2412(`${correctValue(time.getHours())}:${correctValue(time.getMinutes())}:${correctValue(time.getSeconds())}`);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Function for converting 24 hour to 12 hour time
|
||||||
t2412:function(inp) {
|
t2412:function(inp) {
|
||||||
|
// Check what time it is, AM or PM.
|
||||||
if (parseInt(inp.split(":")[0]) > 11) {
|
if (parseInt(inp.split(":")[0]) > 11) {
|
||||||
|
// It's over 11 so it's PM
|
||||||
const i = inp.split(":");
|
const i = inp.split(":");
|
||||||
let i1 = parseInt(i[0]) - 12;
|
let i1 = parseInt(i[0]) - 12;
|
||||||
if (i1 == 0) i1 = 12;
|
if (i1 == 0) i1 = 12;
|
||||||
return i1 + ":" + i[1] + " PM";
|
return i1 + ":" + i[1] + " PM";
|
||||||
} else {
|
} else {
|
||||||
|
// It's lower than 12 so it's AM
|
||||||
const i = inp.split(":");
|
const i = inp.split(":");
|
||||||
let i1 = parseInt(i[0]);
|
let i1 = parseInt(i[0]);
|
||||||
if (i1 == 0) i1 = 12;
|
if (i1 == 0) i1 = 12;
|
||||||
|
@ -59,6 +71,7 @@ module.exports = {
|
||||||
|
|
||||||
module.exports.MOD_FUNC = MODULE_FUNCTION;
|
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) {
|
function correctValue(i) {
|
||||||
if (i < 10) {
|
if (i < 10) {
|
||||||
return "0"+i;
|
return "0"+i;
|
||||||
|
|
|
@ -20,8 +20,12 @@ if (!fs.existsSync(__dirname + BASE_PATH + "/files")) {
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
extras:async function() {
|
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) {
|
get:async function(req, res) {
|
||||||
/*
|
/*
|
||||||
|
@ -29,11 +33,16 @@ module.exports = {
|
||||||
res - Response from server
|
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 => {
|
fs.access(__dirname + BASE_PATH + "/files" + req.url, fs.F_OK, error => {
|
||||||
if (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`);
|
global.modules.consoleHelper.printWarn(emoji.page, `${req.method}: ${req.url} was requested - Returned 404`);
|
||||||
res.status(404).send("404!<hr>Revolution");
|
res.status(404).send("404!<hr>Revolution");
|
||||||
} else {
|
} else {
|
||||||
|
// File does exist, send the file back to the client.
|
||||||
global.modules.consoleHelper.printInfo(emoji.page, `${req.method}: ${req.url} was requested`);
|
global.modules.consoleHelper.printInfo(emoji.page, `${req.method}: ${req.url} was requested`);
|
||||||
res.sendFile(__dirname + BASE_PATH + "/files" + req.url);
|
res.sendFile(__dirname + BASE_PATH + "/files" + req.url);
|
||||||
}
|
}
|
||||||
|
@ -45,7 +54,7 @@ module.exports = {
|
||||||
res - Response from server
|
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.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue