2019-11-23 19:06:31 +00:00
|
|
|
const express = require("express"),
|
|
|
|
fs = require("fs"),
|
|
|
|
chalk = require("chalk"),
|
|
|
|
emoji = require("./misc/emoji_list.json");
|
2020-01-04 12:23:30 +00:00
|
|
|
// This is a class that contains the name and status of required modules
|
2020-01-02 20:44:05 +00:00
|
|
|
class reqMod {
|
|
|
|
constructor(name, status) {
|
|
|
|
this.name = name;
|
|
|
|
this.status = status;
|
|
|
|
}
|
|
|
|
}
|
2020-01-04 12:23:30 +00:00
|
|
|
// Array containing a list of required modules and their flags
|
2020-01-02 20:44:05 +00:00
|
|
|
const requiredModules = [
|
|
|
|
new reqMod("handle_console", false)
|
|
|
|
];
|
2020-01-03 04:03:04 +00:00
|
|
|
let config;
|
2020-01-04 12:08:03 +00:00
|
|
|
// Statup so sync stuff is fine
|
|
|
|
// Check if config exists
|
2020-01-03 04:03:04 +00:00
|
|
|
if (fs.existsSync("./config/config.json")) {
|
2020-01-04 12:08:03 +00:00
|
|
|
// It exists, load it.
|
2020-01-03 05:32:48 +00:00
|
|
|
config = JSON.parse(fs.readFileSync("./config/config.json"));
|
2020-01-03 04:03:04 +00:00
|
|
|
} 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...");
|
2020-01-04 12:08:03 +00:00
|
|
|
// It doesn't exist, exit the framework and tell the user what to do
|
2020-01-03 04:03:04 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2019-11-23 19:06:31 +00:00
|
|
|
global.actualDir = __dirname;
|
|
|
|
global.internals = {
|
|
|
|
version:"Open Source",
|
2019-08-19 03:37:02 +01:00
|
|
|
};
|
2019-11-23 20:33:53 +00:00
|
|
|
global.app = express();
|
2019-11-23 19:06:31 +00:00
|
|
|
global.modules = [];
|
|
|
|
let dE = new Date(),
|
2020-01-04 12:08:03 +00:00
|
|
|
// Get the time at invocation
|
2019-11-23 19:06:31 +00:00
|
|
|
startTime = dE.getTime(),
|
|
|
|
endTime,
|
|
|
|
reqhandler;
|
2019-08-17 23:33:00 +01:00
|
|
|
|
2019-08-19 03:37:02 +01:00
|
|
|
// Clear console before printing anything
|
2019-08-17 23:33:00 +01:00
|
|
|
console.clear();
|
|
|
|
|
2020-01-04 12:08:03 +00:00
|
|
|
// Read the server header for output
|
2019-08-19 03:37:02 +01:00
|
|
|
fs.readFile('./misc/ascii.txt', function(err, data) {
|
2020-01-04 12:23:30 +00:00
|
|
|
// Make sure there are no errors
|
2019-08-19 03:37:02 +01:00
|
|
|
if (err) throw err;
|
2019-11-23 19:06:31 +00:00
|
|
|
// Generate and Print the banner
|
|
|
|
console.log(highlightHeader(data));
|
2019-08-19 03:37:02 +01:00
|
|
|
// Get the modules from the ./modules folder
|
2019-08-17 23:33:00 +01:00
|
|
|
fs.readdir("./modules", (err, files) => {
|
2020-01-04 12:23:30 +00:00
|
|
|
// Make sure there are no errors
|
2019-08-19 03:37:02 +01:00
|
|
|
if (err) throw err;
|
2019-08-17 23:33:00 +01:00
|
|
|
for (var i = 0; i < files.length; i++) {
|
2019-08-19 03:37:02 +01:00
|
|
|
/*
|
|
|
|
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!
|
|
|
|
*/
|
2019-11-23 19:06:31 +00:00
|
|
|
try {
|
2020-01-04 12:08:03 +00:00
|
|
|
// Make sure the file has the extention of .js
|
2019-11-23 19:06:31 +00:00
|
|
|
if (files[i].includes(".js")) {
|
|
|
|
console.log(`[Modules] Found module ${files[i].toString()}`);
|
2019-11-23 21:11:47 +00:00
|
|
|
global.modules[files[i].toString().replace(".js", "")] = require(`./modules/${files[i].toString()}`);
|
2020-01-02 20:44:05 +00:00
|
|
|
// Loop through and set the required modules flags
|
|
|
|
for (var i1 = 0; i1 < requiredModules.length; i1++) {
|
2020-01-04 12:23:30 +00:00
|
|
|
// Check if this module is a required module
|
2020-01-02 20:44:05 +00:00
|
|
|
if (global.modules[files[i].toString().replace(".js", "")].MOD_FUNC == requiredModules[i1].name) {
|
2020-01-04 12:23:30 +00:00
|
|
|
// It is a required module, set the status flag of this one to true
|
2020-01-02 20:44:05 +00:00
|
|
|
requiredModules[i1].status = true;
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 19:06:31 +00:00
|
|
|
// We want to find out what the request handler module is
|
|
|
|
if (global.modules[files[i].toString().replace(".js", "")].MOD_FUNC == "handle_requests") {
|
2019-11-23 20:35:14 +00:00
|
|
|
// Set reqhandler to the request handler for easy getting
|
2019-11-23 19:06:31 +00:00
|
|
|
reqhandler = global.modules[files[i].toString().replace(".js", "")];
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-04 12:23:30 +00:00
|
|
|
// File is not a .js file (module)
|
|
|
|
// Make sure the file is not a directory
|
2019-11-23 19:06:31 +00:00
|
|
|
if (files[i].split(".").length < 2) continue;
|
2020-01-04 12:23:30 +00:00
|
|
|
// Inform user that a file that was found in the modules folder is not a module
|
2019-11-23 19:06:31 +00:00
|
|
|
console.log(`[Modules] Found file ${files[i]}. It is not a module.`)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.log(chalk.bgRed(` ! [Modules] There was an issue loading ${files[i]} ! `));
|
|
|
|
console.log(chalk.bgRed(` ! [Modules] ${err} ! `));
|
2019-08-19 03:37:02 +01:00
|
|
|
}
|
2019-08-17 23:33:00 +01:00
|
|
|
}
|
2020-01-02 20:44:05 +00:00
|
|
|
// Check if all the required modules flags are set
|
|
|
|
let allRequiredExist = [];
|
|
|
|
for (var i = 0; i < requiredModules.length; i++) {
|
2020-01-04 12:08:03 +00:00
|
|
|
// Push the status of the required modules to an array
|
2020-01-03 01:05:31 +00:00
|
|
|
allRequiredExist.push(requiredModules[i].status);
|
2020-01-02 20:44:05 +00:00
|
|
|
}
|
2020-01-04 12:08:03 +00:00
|
|
|
// Make sure all required modules are found
|
2020-01-02 20:44:05 +00:00
|
|
|
if (allRequiredExist.length !== requiredModules.length) {
|
2020-01-04 12:23:30 +00:00
|
|
|
// Inform the user that not all required modules are found.
|
2020-01-02 20:44:05 +00:00
|
|
|
console.log("[Modules] All required modules could not be found.");
|
|
|
|
console.log("[Modules] Server will not start until all required modules are found.");
|
2020-01-04 12:23:30 +00:00
|
|
|
// They are not all found, exit framework with code 1.
|
2020-01-02 20:44:05 +00:00
|
|
|
process.exit(1);
|
|
|
|
} else {
|
2020-01-04 12:08:03 +00:00
|
|
|
// All required modules are found, start the framework's server.
|
2020-01-02 20:44:05 +00:00
|
|
|
global.modules.consoleHelper.printInfo(emoji.wave, "Starting Revolution...");
|
|
|
|
server();
|
|
|
|
}
|
2019-08-17 23:33:00 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function server() {
|
2020-01-04 12:08:03 +00:00
|
|
|
// Load in the request handler's extra required items.
|
2019-11-23 20:33:53 +00:00
|
|
|
reqhandler.extras();
|
2020-01-04 12:08:03 +00:00
|
|
|
// Define where GET requests go to in the request handlers.
|
2019-11-23 19:06:31 +00:00
|
|
|
app.get('*', (req, res) => reqhandler.get(req, res));
|
2020-01-04 12:08:03 +00:00
|
|
|
// Define where POST requests go to in the request handlers.
|
2019-11-23 19:06:31 +00:00
|
|
|
app.post('*', (req, res) => reqhandler.post(req, res));
|
2020-01-04 12:08:03 +00:00
|
|
|
// Start the server listening at the port defined in the config file
|
2019-11-23 19:06:31 +00:00
|
|
|
app.listen(config.server.port, () => {
|
|
|
|
dE = new Date(),
|
2020-01-04 12:08:03 +00:00
|
|
|
// Get time after server has started listening or the "end time".
|
2019-11-23 19:06:31 +00:00
|
|
|
endTime = dE.getTime();
|
|
|
|
global.modules.consoleHelper.printInfo(emoji.thumb_up, `Started Revolution on port ${config.server.port}! Took ${endTime - startTime}ms`);
|
2019-08-17 23:33:00 +01:00
|
|
|
});
|
2019-11-23 19:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function highlightHeader(s) {
|
2020-01-04 12:10:23 +00:00
|
|
|
// Add the appropriate colours to the header and add information to it
|
2019-11-23 19:06:31 +00:00
|
|
|
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)}`);
|
|
|
|
return s1;
|
2019-08-17 23:33:00 +01:00
|
|
|
}
|