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") %>
<% @@ -33,6 +33,6 @@
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") %>