Add comments to the framework & included modules #16

Merged
tgpholly merged 9 commits from add-comments into master 2020-01-04 15:18:13 +00:00
Showing only changes of commit 7807fccd8a - Show all commits

View file

@ -12,11 +12,15 @@ 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 +30,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,6 +38,7 @@ 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) {
if (err) throw err; if (err) throw err;
// Generate and Print the banner // 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! 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()}`);
@ -73,14 +80,17 @@ 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 // They are not all found, exit framework with code 1.
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.");
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...");
server(); server();
} }
@ -88,11 +98,16 @@ fs.readFile('./misc/ascii.txt', function(err, data) {
}); });
function server() { function server() {
// 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`);
}); });