From a62e8d17a3c3b8dbd9b007c8af224a4aea72c4d2 Mon Sep 17 00:00:00 2001
From: Hornwitser
Date: Fri, 6 Sep 2019 01:57:19 +0200
Subject: [PATCH] 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") %>