clean up server

This commit is contained in:
Felix Wiedenbach 2021-01-05 18:37:16 +01:00
parent d22064c6f4
commit ac141a4316

View file

@ -4,25 +4,22 @@
* By Michael Teeuw https://michaelteeuw.nl * By Michael Teeuw https://michaelteeuw.nl
* MIT Licensed. * MIT Licensed.
*/ */
var express = require("express"); const express = require("express");
var app = require("express")(); const app = require("express")();
var path = require("path"); const path = require("path");
var ipfilter = require("express-ipfilter").IpFilter; const ipfilter = require("express-ipfilter").IpFilter;
var fs = require("fs"); const fs = require("fs");
var helmet = require("helmet"); const helmet = require("helmet");
var Log = require("./logger.js"); const Log = require("./logger.js");
var Utils = require("./utils.js"); const Utils = require("./utils.js");
var Server = function (config, callback) { function Server(config, callback) {
var port = config.port; const port = process.env.MM_PORT || config.port;
if (process.env.MM_PORT) {
port = process.env.MM_PORT;
}
var server = null; let server = null;
if (config.useHttps) { if (config.useHttps) {
var options = { const options = {
key: fs.readFileSync(config.httpsPrivateKey), key: fs.readFileSync(config.httpsPrivateKey),
cert: fs.readFileSync(config.httpsCertificate) cert: fs.readFileSync(config.httpsCertificate)
}; };
@ -30,18 +27,18 @@ var Server = function (config, callback) {
} else { } else {
server = require("http").Server(app); server = require("http").Server(app);
} }
var io = require("socket.io")(server); const io = require("socket.io")(server);
Log.log("Starting server on port " + port + " ... "); Log.log(`Starting server on port ${port} ... `);
server.listen(port, config.address ? config.address : "localhost"); server.listen(port, config.address || "localhost");
if (config.ipWhitelist instanceof Array && config.ipWhitelist.length === 0) { if (config.ipWhitelist instanceof Array && config.ipWhitelist.length === 0) {
Log.warn(Utils.colors.warn("You're using a full whitelist configuration to allow for all IPs")); Log.warn(Utils.colors.warn("You're using a full whitelist configuration to allow for all IPs"));
} }
app.use(function (req, res, next) { app.use(function (req, res, next) {
var result = ipfilter(config.ipWhitelist, { mode: config.ipWhitelist.length === 0 ? "deny" : "allow", log: false })(req, res, function (err) { ipfilter(config.ipWhitelist, { mode: config.ipWhitelist.length === 0 ? "deny" : "allow", log: false })(req, res, function (err) {
if (err === undefined) { if (err === undefined) {
return next(); return next();
} }
@ -52,10 +49,9 @@ var Server = function (config, callback) {
app.use(helmet({ contentSecurityPolicy: false })); app.use(helmet({ contentSecurityPolicy: false }));
app.use("/js", express.static(__dirname)); app.use("/js", express.static(__dirname));
var directories = ["/config", "/css", "/fonts", "/modules", "/vendor", "/translations", "/tests/configs"];
var directory; const directories = ["/config", "/css", "/fonts", "/modules", "/vendor", "/translations", "/tests/configs"];
for (var i in directories) { for (const directory of directories) {
directory = directories[i];
app.use(directory, express.static(path.resolve(global.root_path + directory))); app.use(directory, express.static(path.resolve(global.root_path + directory)));
} }
@ -68,10 +64,10 @@ var Server = function (config, callback) {
}); });
app.get("/", function (req, res) { app.get("/", function (req, res) {
var html = fs.readFileSync(path.resolve(global.root_path + "/index.html"), { encoding: "utf8" }); let html = fs.readFileSync(path.resolve(`${global.root_path}/index.html`), { encoding: "utf8" });
html = html.replace("#VERSION#", global.version); html = html.replace("#VERSION#", global.version);
var configFile = "config/config.js"; let configFile = "config/config.js";
if (typeof global.configuration_file !== "undefined") { if (typeof global.configuration_file !== "undefined") {
configFile = global.configuration_file; configFile = global.configuration_file;
} }
@ -83,6 +79,6 @@ var Server = function (config, callback) {
if (typeof callback === "function") { if (typeof callback === "function") {
callback(app, io); callback(app, io);
} }
}; }
module.exports = Server; module.exports = Server;