From 0b8870f64000388f182c17b79974f19a67f617c9 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Thu, 13 Feb 2020 20:24:35 -0500 Subject: [PATCH] add ability to serve static roots as different prefixes --- js/moto-db.js | 4 +++- js/web-server.js | 28 +++++++++++++++++++++++----- web/kiri/filter/FDM/GridBot.Two | 1 + 3 files changed, 27 insertions(+), 6 deletions(-) create mode 120000 web/kiri/filter/FDM/GridBot.Two diff --git a/js/moto-db.js b/js/moto-db.js index 95feaa71..e5e2acfd 100644 --- a/js/moto-db.js +++ b/js/moto-db.js @@ -97,7 +97,9 @@ var gs_moto_db = exports; } storage.db = request.result; storage.store = storage.db.createObjectStore(name); - setTimeout(function() { storage.runQueue() }); + event.target.transaction.oncomplete = function(event) { + storage.runQueue(); + }; }; request.onsuccess = function(event) { diff --git a/js/web-server.js b/js/web-server.js index d56cdcd0..9f51d0c2 100644 --- a/js/web-server.js +++ b/js/web-server.js @@ -1018,7 +1018,7 @@ function initModule(file, dir) { pre: arg => { modPaths.push(prepath(arg)) }, map: arg => { modPaths.push(fixedmap(arg)) }, full: arg => { modPaths.push(fullpath(arg)) }, - static: arg => { modPaths.push(serveStatic(arg)) }, + static: (root, pre) => { modPaths.push(handleStatic(root, pre)) }, code: (endpoint, path) => { if (debug) { code[endpoint] = fs.readFileSync(path); @@ -1031,7 +1031,7 @@ function initModule(file, dir) { }, handler: { addCORS: addCorsHeaders, - static: serveStatic, + static: handleStatic, redirect: redirect, reply404: reply404, reply: quickReply @@ -1039,10 +1039,28 @@ function initModule(file, dir) { }) } +function handleStatic(root, pre) { + let statServe = serveStatic(root); + return function(req, res, next) { + if (pre) { + if (req.url.indexOf(pre) === 0) { + if (req.url === pre) { + req.url = "/"; + } else { + req.url = req.url.substring(pre.length); + } + return statServe(req, res, next); + } + return next(); + } + statServe(req, res, next); + }; +} + // add static assets to be served -function addStatic(dir) { +function addStatic(dir, pre) { helper.log({static:dir}); - modPaths.push(serveStatic(dir)); + modPaths.push(handleStatic(dir, pre)); } // either add module assets to path or require(init.js) @@ -1101,7 +1119,7 @@ handler.use(fullpath({ .use(fixedmap("/api/", api)) .use(rewriteHTML) .use(compression) - .use(serveStatic(currentDir + "/web/")) + .use(handleStatic(currentDir + "/web/")) .listen(port); helper.log("------------------------------------------"); diff --git a/web/kiri/filter/FDM/GridBot.Two b/web/kiri/filter/FDM/GridBot.Two new file mode 120000 index 00000000..b188c4a2 --- /dev/null +++ b/web/kiri/filter/FDM/GridBot.Two @@ -0,0 +1 @@ +GridBot.One \ No newline at end of file