updates to track app server changes

This commit is contained in:
Stewart Allen 2020-05-30 18:16:40 -04:00
commit 90fdd9d453
2 changed files with 35 additions and 65 deletions

94
app.js
View file

@ -8,7 +8,6 @@ const fs = require('fs');
const uglify = require('uglify-es'); const uglify = require('uglify-es');
const moment = require('moment'); const moment = require('moment');
const agent = require('express-useragent'); const agent = require('express-useragent');
const level = require('level')('./persist', {valueEncoding:"json"});
const ver = require_fresh('./js/license.js'); const ver = require_fresh('./js/license.js');
const fileCache = {}; const fileCache = {};
@ -23,24 +22,38 @@ const device = {
laser: [] laser: []
}; };
let level;
let cacheDir;
let startTime; let startTime;
let lastmod; let lastmod;
let logger; let logger;
let debug; let debug;
let http;
let util; let util;
let dir; let dir;
let log;
function init(mod) { function init(mod) {
startTime = time(); startTime = time();
lastmod = mod.util.lastmod; lastmod = mod.util.lastmod;
logger = mod.log; logger = mod.log;
debug = mod.env.debug || mod.info.debug; debug = mod.env.debug || mod.meta.debug;
http = mod.http;
util = mod.util; util = mod.util;
dir = mod.dir; dir = mod.dir;
log = mod.log;
level = require('level')(mod.util.datadir("kvstore"), {valueEncoding:"json"});
cacheDir = mod.util.datadir("cache");
mod.on.reload(() => level.close()); mod.on.reload(() => level.close());
mod.on.test((req) => {
if (mod.meta.version && mod.meta.version !== "*") {
return (cookieValue(req.headers.cookie, "version") === mod.meta.version);
} else {
return true;
}
});
mod.add(handleBetaKey);
mod.add(handleOptions); mod.add(handleOptions);
mod.add(fullpath({ mod.add(fullpath({
"/kiri/index.html" : redir("/kiri/"), "/kiri/index.html" : redir("/kiri/"),
@ -52,10 +65,13 @@ function init(mod) {
[ "/wasm/", handleWasm ] [ "/wasm/", handleWasm ]
])); ]));
mod.add(fixedmap("/api/", api)); mod.add(fixedmap("/api/", api));
mod.sync("/reload", (info) => { mod.reload(); return "reload" });
if (debug) { if (debug) {
mod.static("/js/", "js"); mod.static("/js/", "js");
mod.static("/mod/", "mod"); mod.static("/mod/", "mod");
mod.sync("/reload", () => {
mod.reload();
return "reload";
});
} }
mod.static("/obj/", "web/obj"); mod.static("/obj/", "web/obj");
mod.static("/moto/", "web/moto"); mod.static("/moto/", "web/moto");
@ -122,12 +138,13 @@ function initModule(mod, file, dir) {
log: logger.log, log: logger.log,
time: time, time: time,
guid: guid, guid: guid,
mkdirs: mkdirs, mkdirs: util.mkdir,
datadir: util.datadir,
lastmod: lastmod, lastmod: lastmod,
obj2string: obj2string, obj2string: obj2string,
string2obj: string2obj, string2obj: string2obj,
getCookieValue: cookieValue, getCookieValue: cookieValue,
logger: util.logger logger: log.new
}, },
db: { db: {
api: db, api: db,
@ -170,9 +187,8 @@ function initModule(mod, file, dir) {
}, },
handler: { handler: {
addCORS: addCorsHeaders, addCORS: addCorsHeaders,
static: util.handleStatic, redirect: http.redirect,
redirect: util.redirect, reply404: http.reply404,
reply404: util.reply404,
reply: quickReply reply: quickReply
}, },
ws: { ws: {
@ -351,18 +367,6 @@ function string2obj(s) {
return JSON.parse(s); return JSON.parse(s);
} }
function mkdirs(path) {
let root = "";
path.forEach(seg => {
if (root) {
root = root + "/" + seg;
} else {
root = seg;
}
lastmod(root) || fs.mkdirSync(root);
});
}
function handleOptions(req, res, next) { function handleOptions(req, res, next) {
if (req.method === 'OPTIONS') { if (req.method === 'OPTIONS') {
addCorsHeaders(req, res); addCorsHeaders(req, res);
@ -401,7 +405,7 @@ function handleCode(req, res, next) {
js = code[key]; js = code[key];
if (!js) { if (!js) {
return reply404(req, res); return http.reply404(req, res);
} }
if (ck) { if (ck) {
@ -425,7 +429,7 @@ function handleCode(req, res, next) {
function serveCode(req, res, code) { function serveCode(req, res, code) {
if (code.deny) { if (code.deny) {
return reply404(req, res); return http.reply404(req, res);
} }
let imd = ifModifiedDate(req); let imd = ifModifiedDate(req);
@ -495,8 +499,8 @@ function concatCode(array) {
} }
array.forEach(file => { array.forEach(file => {
let cached = getCachedFile(dir, file, function(path) { let cached = getCachedFile(file, function(path) {
return minify(dir + "/" + file); return minify(`${dir}/${file}`);
}); });
code.push(cached); code.push(cached);
}); });
@ -504,12 +508,11 @@ function concatCode(array) {
return code.join(''); return code.join('');
} }
function getCachedFile(dir, file, fn) { function getCachedFile(file, fn) {
let filePath = dir + "/" + file; let filePath = `${dir}/${file}`;
let cachePath = dir + "/.cache/" + file let cachePath = cacheDir + "/" + file
.replace(/\//g,'_') .replace(/\//g,'_')
.replace(/\\/g,'_') .replace(/\\/g,'_')
// .replace(/-/g,'_')
.replace(/:/g,'_'), .replace(/:/g,'_'),
cached = fileCache[filePath], cached = fileCache[filePath],
now = time(); now = time();
@ -566,17 +569,6 @@ function quickReply(res, code, msg) {
res.end(msg+"\n"); res.end(msg+"\n");
} }
function reply404(req, res) {
// logger.emit([
// '404',
// req.url,
// req.socket.remoteAddress,
// req.headers
// ]);
res.writeHead(404);
res.end("[404]");
}
function ifModifiedDate(req) { function ifModifiedDate(req) {
let ims = req.headers['if-modified-since']; let ims = req.headers['if-modified-since'];
if (ims) { if (ims) {
@ -644,7 +636,7 @@ function fixedmap(prefix, map) {
// HTTP 307 redirect // HTTP 307 redirect
function redir(path) { function redir(path) {
return (req, res, next) => util.redirect(res, path); return (req, res, next) => http.redirect(res, path);
} }
// mangle request path // mangle request path
@ -666,26 +658,6 @@ function cookieValue(cookie,key) {
} }
} }
function handleBetaKey(req, res, next) {
let host = req.headers['host'] || '',
beta = cookieValue(req.headers['cookie'],'beta');
if (beta === 'true' && req.url.indexOf('?prod') > 0) {
res.setHeader('Set-Cookie', 'beta=false; path=/;');
beta = 'false';
} else if (beta !== 'true' && req.url.indexOf('?beta') > 0) {
res.setHeader('Set-Cookie', 'beta=true; path=/;');
beta = 'true';
}
if (beta === 'true' && host.indexOf('beta.') !== 0) {
res.writeHead(307, { "Location": `//beta.${host}${req.url}` });
res.end();
} else {
next();
}
}
function rewriteHtmlVersion(req, res, next) { function rewriteHtmlVersion(req, res, next) {
if (req.url.indexOf(".html") > 0) { if (req.url.indexOf(".html") > 0) {
let real_write = res.write; let real_write = res.write;

View file

@ -1,6 +1,4 @@
{ {
"name": "kiri:moto", "name": "kiri",
"main": "app.js", "main": "app.js"
"host": ["localhost:8080", "grid.space"],
"debug": true
} }