From a62e8d17a3c3b8dbd9b007c8af224a4aea72c4d2 Mon Sep 17 00:00:00 2001
From: Hornwitser
Date: Fri, 6 Sep 2019 01:57:19 +0200
Subject: [PATCH 1/3] Move ejs templates to views/
Move the ejs templates to the default Express.js views/ folder and
change their extension to .ejs. Reconfigure Express.js to look for
templates in the views folders as well as the plugins folder. This
enables plugins to render templates using paths of following format
"pluginName/path/to/page-or-view".
---
CHANGELOG.md | 5 +++++
master.js | 6 ++----
sharedPlugins/serverManager/masterPlugin.js | 10 +++-------
.../{static/index.html => views/index.ejs} | 6 +++---
static/sidebar.html | 1 -
static/head.html => views/head.ejs | 0
static/index.html => views/index.ejs | 6 +++---
static/nav.html => views/nav.ejs | 2 +-
static/nodeDetails.html => views/nodeDetails.ejs | 6 +++---
static/nodes.html => views/nodes.ejs | 6 +++---
static/settings.html => views/settings.ejs | 6 +++---
views/sidebar.ejs | 1 +
12 files changed, 27 insertions(+), 28 deletions(-)
rename sharedPlugins/serverManager/{static/index.html => views/index.ejs} (90%)
delete mode 100644 static/sidebar.html
rename static/head.html => views/head.ejs (100%)
rename static/index.html => views/index.ejs (96%)
rename static/nav.html => views/nav.ejs (99%)
rename static/nodeDetails.html => views/nodeDetails.ejs (92%)
rename static/nodes.html => views/nodes.ejs (97%)
rename static/settings.html => views/settings.ejs (81%)
create mode 100644 views/sidebar.ejs
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bccfa8b2..c366087c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,9 @@ Version 2.0.0
- Factorio game and rcon port now defaults to a random port above 49151.
- Removed unimplemented mods update command.
- Fixed rcon password being generated with Math.random().
+- Added plugins directory to the views path. This makes it possible for
+ plugins to render their own ejs views or pages in their own folders by
+ using paths of the format "pluginName/path/to/page-or-view".
### Breaking Changes
@@ -21,6 +24,8 @@ Version 2.0.0
clusterioModel.
- Instance names can no longer be invalid Windows file names.
- Removed config management from the command line and the server manager.
+- Moved ejs templates into views folder and changed their extension to
+ .ejs. Breaks playerManager.
Version 1.2.1
diff --git a/master.js b/master.js
index 6f407ed7..e0a1986c 100644
--- a/master.js
+++ b/master.js
@@ -42,7 +42,6 @@ const authenticate = require("lib/authenticate");
const express = require("express");
const compression = require('compression');
const cookieParser = require('cookie-parser');
-const ejs = require("ejs");
// Required for express post requests
const bodyParser = require("body-parser");
const fileUpload = require('express-fileupload');
@@ -59,9 +58,8 @@ app.use(fileUpload());
app.use(compression());
// dynamic HTML generations with EJS
-app.set('views', path.join(__dirname, 'static'));
-app.set('view engine', 'html');
-app.engine('html', ejs.renderFile);
+app.set('view engine', 'ejs');
+app.set('views', ['views', 'sharedPlugins']);
// give ejs access to some interesting information
app.use(function(req, res, next){
diff --git a/sharedPlugins/serverManager/masterPlugin.js b/sharedPlugins/serverManager/masterPlugin.js
index e972283d..981dbf00 100644
--- a/sharedPlugins/serverManager/masterPlugin.js
+++ b/sharedPlugins/serverManager/masterPlugin.js
@@ -69,8 +69,8 @@ class masterPlugin {
let pages = [
{
addr: "/serverManager",
- path: path.join(__dirname,"static/index.html"),
- render: ejs
+ path: "serverManager/views/index",
+ render: true
},
]
pages.forEach(page => {
@@ -81,14 +81,10 @@ class masterPlugin {
} else {
var permissions = {};
}
- page.render.renderFile(page.path, {
+ res.render(page.path, {
permissions,
serverManager: this,
req: req,
- }, async (err, prom) => {
- if(err) console.log(err);
- let str = await prom;
- res.send(str);
});
} else {
res.send(await fs.readFile(page.path));
diff --git a/sharedPlugins/serverManager/static/index.html b/sharedPlugins/serverManager/views/index.ejs
similarity index 90%
rename from sharedPlugins/serverManager/static/index.html
rename to sharedPlugins/serverManager/views/index.ejs
index 0dd92458..54fc72fc 100644
--- a/sharedPlugins/serverManager/static/index.html
+++ b/sharedPlugins/serverManager/views/index.ejs
@@ -1,12 +1,12 @@
- <%- include("./../../../static/head.html") %>
+ <%- include("head") %>
- <%- include("./../../../static/sidebar.html") %>
+ <%- include("sidebar") %>
diff --git a/static/sidebar.html b/static/sidebar.html
deleted file mode 100644
index ab694086..00000000
--- a/static/sidebar.html
+++ /dev/null
@@ -1 +0,0 @@
-<%- include("nav.html") %>
\ No newline at end of file
diff --git a/static/head.html b/views/head.ejs
similarity index 100%
rename from static/head.html
rename to views/head.ejs
diff --git a/static/index.html b/views/index.ejs
similarity index 96%
rename from static/index.html
rename to views/index.ejs
index b755490e..336848b8 100644
--- a/static/index.html
+++ b/views/index.ejs
@@ -1,11 +1,11 @@
- <%- include("head.html") %>
+ <%- include("head") %>
-<%- include("nav.html") %>
+<%- include("nav") %>
- <%- include("nav.html") %>
+ <%- include("nav") %>
- <%- include("nav.html") %>
+ <%- include("nav") %>
- <%- include("nav.html") %>
+ <%- include("nav") %>
-
diff --git a/static/nav.html b/views/nav.ejs
similarity index 99%
rename from static/nav.html
rename to views/nav.ejs
index 6c359198..536f47c3 100644
--- a/static/nav.html
+++ b/views/nav.ejs
@@ -34,4 +34,4 @@
-
\ No newline at end of file
+
diff --git a/static/nodeDetails.html b/views/nodeDetails.ejs
similarity index 92%
rename from static/nodeDetails.html
rename to views/nodeDetails.ejs
index 4851b9e3..6d6e2c00 100644
--- a/static/nodeDetails.html
+++ b/views/nodeDetails.ejs
@@ -1,13 +1,13 @@
- <%- include("head.html") %>
+ <%- include("head") %>
\ No newline at end of file
+
diff --git a/static/nodes.html b/views/nodes.ejs
similarity index 97%
rename from static/nodes.html
rename to views/nodes.ejs
index f45ec765..c8277c74 100644
--- a/static/nodes.html
+++ b/views/nodes.ejs
@@ -1,11 +1,11 @@
- <%- include("head.html") %>
+ <%- include("head") %>
\ No newline at end of file
+
diff --git a/static/settings.html b/views/settings.ejs
similarity index 81%
rename from static/settings.html
rename to views/settings.ejs
index 8ae0f096..18295ad5 100644
--- a/static/settings.html
+++ b/views/settings.ejs
@@ -1,11 +1,11 @@
- <%- include("head.html") %>
+ <%- include("head") %>
\ No newline at end of file
+
diff --git a/views/sidebar.ejs b/views/sidebar.ejs
new file mode 100644
index 00000000..8809c389
--- /dev/null
+++ b/views/sidebar.ejs
@@ -0,0 +1 @@
+<%- include("nav") %>
From 706a1dbd398a68a384b882c109cd651a5f62da3d Mon Sep 17 00:00:00 2001
From: Hornwitser
Date: Sun, 25 Aug 2019 14:04:49 +0200
Subject: [PATCH 2/3] Indent static/browserconfig.xml
Make static/browserconfig.xml readable by indenting it's content.
---
static/browserconfig.xml | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/static/browserconfig.xml b/static/browserconfig.xml
index c5541482..ae389fe5 100644
--- a/static/browserconfig.xml
+++ b/static/browserconfig.xml
@@ -1,2 +1,11 @@
-#ffffff
\ No newline at end of file
+
+
+
+
+
+
+ #ffffff
+
+
+
\ No newline at end of file
From 6726c2b16f4421b6c7558c38de95aa3759f82ce9 Mon Sep 17 00:00:00 2001
From: Hornwitser
Date: Mon, 26 Aug 2019 11:36:05 +0200
Subject: [PATCH 3/3] Refactor EJS string gluing code
Rewrite awful string gluing of HTML with JS to use the slightly less
awful EJS string gluing method.
---
views/nav.ejs | 35 +++++++++---------
views/nodes.ejs | 94 ++++++++++++++++++++++---------------------------
2 files changed, 59 insertions(+), 70 deletions(-)
diff --git a/views/nav.ejs b/views/nav.ejs
index 536f47c3..5a560649 100644
--- a/views/nav.ejs
+++ b/views/nav.ejs
@@ -10,27 +10,24 @@
- <%# Very ugly ejs code to hide the Players button if the playerManager plugin isn`t installed %>
- <%
- let sidebarPluginButtonsHtml = "";
- for(let o in locals.masterPlugins){
+ <%
+for(let o in locals.masterPlugins) {
let plug = locals.masterPlugins[o];
- if(plug.main
- && plug.main.ui
- && typeof plug.main.ui.sidebar == "object"
- && Array.isArray(plug.main.ui.sidebar)){
- let sidebarEntries = plug.main.ui.sidebar;
- for(let i in sidebarEntries){
- let button = sidebarEntries[i];
- if(button.getHtml && typeof button.getHtml == "function"){
- sidebarPluginButtonsHtml += button.getHtml();
+ if (
+ plug.main
+ && plug.main.ui
+ && typeof plug.main.ui.sidebar == "object"
+ && Array.isArray(plug.main.ui.sidebar)
+ ) {
+ let sidebarEntries = plug.main.ui.sidebar;
+ for (let i in sidebarEntries) {
+ let button = sidebarEntries[i];
+ if (button.getHtml && typeof button.getHtml == "function") { %>
+ <%- button.getHtml() %><%
+ }
+ }
}
- }
- }
- }
- %>
- <%- sidebarPluginButtonsHtml %>
+} %>
diff --git a/views/nodes.ejs b/views/nodes.ejs
index c8277c74..1e860c23 100644
--- a/views/nodes.ejs
+++ b/views/nodes.ejs
@@ -7,56 +7,48 @@
<%- include("nav") %>
-
-
- <%
- let html = ""
- for(let [id, slave] of locals.slaves){
- html += `
-
-
-
- ID: ${id}
- Last seen: ${moment(Number(slave.time)).format("DD.MM.YY, hh:mm")}
- Online players: ${slave.playerCount}
- IP: ${slave.publicIP}:${slave.serverPort}
- Host: ${slave.mac}
-
- `
- for(let o in locals.masterPlugins){
- let plug = locals.masterPlugins[o];
- if(plug.main
- && plug.main.ui
- && typeof plug.main.ui.slaveListing == "object"
- && Array.isArray(plug.main.ui.slaveListing)){
- let slaveListingEntries = plug.main.ui.slaveListing;
- for(let i in slaveListingEntries){
- let button = slaveListingEntries[i];
- if(button.getHtml && typeof button.getHtml == "function"){
- html += "
"
- html += button.getHtml({slave});
- html += "
"
- }
- }
- }
- }
- html += `
-
- `
- }
- %>
- <%- html %>
+
-
+
+
<%
+for (let [id, slave] of locals.slaves) { %>
+
+
+
+ ID: <%= id %>
+ Last seen: <%= moment(Number(slave.time)).format("DD.MM.YY, hh:mm") %>
+ Online players: <%= slave.playerCount %>
+ IP: <%= slave.publicIP %>:<%= slave.serverPort %>
+ Host: <%= slave.mac %>
+ <%
+ for (let o in locals.masterPlugins) {
+ let plug = locals.masterPlugins[o];
+ if (
+ plug.main
+ && plug.main.ui
+ && typeof plug.main.ui.slaveListing == "object"
+ && Array.isArray(plug.main.ui.slaveListing)
+ ) {
+ let slaveListingEntries = plug.main.ui.slaveListing;
+ for (let i in slaveListingEntries) {
+ let button = slaveListingEntries[i];
+ if (button.getHtml && typeof button.getHtml == "function") { %>
+
<%- button.getHtml({slave}) %>
<%
+ }
+ }
+ }
+ } %>
+
<%
+} %>
+
+
+