From af0387c030ea68019ba655ec80cf731aa70a07d0 Mon Sep 17 00:00:00 2001 From: Candid Dauth Date: Thu, 2 Mar 2017 00:41:16 +0100 Subject: [PATCH] Clean up webserver code, add method getFrontendFile() --- server/{ => export}/gpx.js | 2 +- server/{ => export}/table.js | 17 ++++++---- server/webserver.js | 65 ++++++++++++++++-------------------- 3 files changed, 40 insertions(+), 44 deletions(-) rename server/{ => export}/gpx.js (99%) rename server/{ => export}/table.js (79%) diff --git a/server/gpx.js b/server/export/gpx.js similarity index 99% rename from server/gpx.js rename to server/export/gpx.js index df6e7930..04fb7e0a 100644 --- a/server/gpx.js +++ b/server/export/gpx.js @@ -1,4 +1,4 @@ -var utils = require("./utils"); +var utils = require("../utils"); var _e = utils.escapeXml; diff --git a/server/table.js b/server/export/table.js similarity index 79% rename from server/table.js rename to server/export/table.js index 32c81b48..84d6d7af 100644 --- a/server/table.js +++ b/server/export/table.js @@ -5,10 +5,11 @@ const Promise = require("promise"); const commonUtils = require("facilmap-frontend/common/utils"); const commonFormat = require("facilmap-frontend/common/format"); -const utils = require("./utils"); +const utils = require("../utils"); +const webserver = require("../webserver"); -const table = module.exports = { - createTable(database, padId, template) { +const table = { + createTable(database, padId) { return utils.promiseAuto({ padData: database.getPadData(padId), @@ -31,9 +32,11 @@ const table = module.exports = { return utils.streamEachPromise(database.getPadLines(padId), function(line) { types[line.typeId].lines.push(line); }); - } + }, + + template: webserver.getFrontendFile("table.ejs") }).then((results) => { - return ejs.render(template, { + return ejs.render(results.template, { padData: results.padData, types: results.types, utils: commonUtils, @@ -41,4 +44,6 @@ const table = module.exports = { }) }) } -}; \ No newline at end of file +}; + +Object.assign(exports, table); \ No newline at end of file diff --git a/server/webserver.js b/server/webserver.js index 99db59f8..6f669c5b 100644 --- a/server/webserver.js +++ b/server/webserver.js @@ -7,8 +7,8 @@ const path = require("path"); const Promise = require("promise"); const database = require("./database/database"); -const gpx = require("./gpx"); -const table = require("./table"); +const gpx = require("./export/gpx"); +const table = require("./export/table"); const utils = require("./utils"); const frontendPath = path.dirname(require.resolve("facilmap-frontend/package.json")); // Do not resolve main property @@ -16,30 +16,18 @@ const frontendPath = path.dirname(require.resolve("facilmap-frontend/package.jso if(process.env.FM_DEV) process.chdir(frontendPath); // To make sure that webpack finds all the loaders -const webserver = module.exports = { - init(database, port, host) { - const staticMiddleware = process.env.FM_DEV - ? require("webpack-dev-middleware")(require("webpack")(require("facilmap-frontend/webpack.config")), { // require the stuff here so that it doesn't fail if devDependencies are not installed - publicPath: "/" - }) - : express.static(frontendPath + "/build/"); +const staticMiddleware = process.env.FM_DEV + ? require("webpack-dev-middleware")(require("webpack")(require("facilmap-frontend/webpack.config")), { // require the stuff here so that it doesn't fail if devDependencies are not installed + publicPath: "/" + }) + : express.static(frontendPath + "/build/"); +const webserver = { + init(database, port, host) { const padMiddleware = function(req, res, next) { utils.promiseAuto({ - template: () => { - if (process.env.FM_DEV) { - let intercept = utils.interceptWriteStream(res); - req.url = req.originalUrl = "/index.ejs"; - staticMiddleware(req, res, next); - return intercept; - } else { - // We don't want express.static's ETag handling, as it sometimes returns an empty template, - // so we have to read it directly from the file system - - return Promise.denodeify(fs.readFile)(`${frontendPath}/build/index.ejs`, "utf8"); - } - }, + template: () => webserver.getFrontendFile("index.ejs"), padData: () => { if(req.params && req.params.padId) { @@ -107,21 +95,7 @@ const webserver = module.exports = { }); app.get("/:padId/table", function(req, res, next) { - Promise.resolve().then(() => { - if (process.env.FM_DEV) { - let intercept = utils.interceptWriteStream(res); - req.url = req.originalUrl = "/table.ejs"; - staticMiddleware(req, res, next); - return intercept; - } else { - // We don't want express.static's ETag handling, as it sometimes returns an empty template, - // so we have to read it directly from the file system - - return Promise.denodeify(fs.readFile)(`${frontendPath}/build/table.ejs`, "utf8"); - } - }).then((template) => { - return table.createTable(database, req.params.padId, template); - }).then((renderedTable) => { + return table.createTable(database, req.params.padId).then((renderedTable) => { res.type("html"); res.send(renderedTable); }).catch(next); @@ -129,5 +103,22 @@ const webserver = module.exports = { let server = http.createServer(app); return Promise.denodeify(server.listen.bind(server))(port, host).then(() => server); + }, + + getFrontendFile(path) { + if (process.env.FM_DEV) { + return new Promise((resolve, reject) => { + staticMiddleware.waitUntilValid(resolve); + }).then(() => { + return staticMiddleware.fileSystem.readFileSync(staticMiddleware.getFilenameFromUrl(`/${path}`), "utf8"); + }); + } else { + // We don't want express.static's ETag handling, as it sometimes returns an empty template, + // so we have to read it directly from the file system + + return Promise.denodeify(fs.readFile)(`${frontendPath}/build/${path}`, "utf8"); + } } }; + +Object.assign(exports, webserver);