load sites separately from packages

This commit is contained in:
AJ ONeal 2017-05-20 04:34:17 +00:00
parent 032ebe0302
commit 3cf54f2b9a
2 changed files with 12 additions and 11 deletions

View File

@ -218,7 +218,7 @@ install_my_app()
ln -sf ../node_modules /srv/walnut/core/node_modules
sudo mkdir -p /srv/walnut/etc/org.oauth3.consumer
sudo mkdir -p /srv/walnut/etc/org.oauth3.provider
sudo mkdir -p /srv/walnut/packages/{client-api-grants,rest,api,pages,services}
sudo mkdir -p /srv/walnut/packages/{client-api-grants,rest,api,pages,services,sites}
#sudo chown -R $(whoami):$(whoami) /srv/walnut
sudo chown -R www-data:www-data /srv/walnut
sudo chmod -R ug+Xrw /srv/walnut

View File

@ -25,7 +25,7 @@ module.exports.create = function (app, xconfx, apiFactories, apiDeps, errorIfApi
res.statusCode = 302;
res.setHeader('Location', url);
res.end();
res.end("The static pages for '" + reason + "' are not listed in '" + path.join(xconfx.sitespath, reason) + "'");
}
function disallowSymLinks(req, res) {
@ -67,19 +67,19 @@ module.exports.create = function (app, xconfx, apiFactories, apiDeps, errorIfApi
});
}
function loadHandler(name) {
function loadSiteHandler(name) {
return function handler(req, res, next) {
// path.join('packages/pages', 'com.daplie.hello') // package name (used as file-link)
// path.join('packages/pages', 'domain.tld#hello') // dynamic exact url match
var packagepath = path.join(xconfx.staticpath, name);
var sitepath = path.join(xconfx.sitespath, name);
return fs.lstatAsync(packagepath).then(function (stat) {
return fs.lstatAsync(sitepath).then(function (stat) {
if (stat.isSymbolicLink()) {
return disallowSymLinks;
}
if (stat.isDirectory()) {
return express.static(packagepath);
return express.static(sitepath);
}
if (!stat.isFile()) {
@ -96,13 +96,13 @@ module.exports.create = function (app, xconfx, apiFactories, apiDeps, errorIfApi
// and if no file matches that 'tld.domain.app' may be tried next, and so on
//
// this may well become a .htaccess type of situation allowing for redirects and such
return fs.readFileAsync(packagepath, 'utf8').then(function (text) {
return fs.readFileAsync(sitepath, 'utf8').then(function (text) {
// TODO allow cascading multiple lines
text = text.trim().split(/\n/)[0];
// TODO rerun the above, disallowing link-style (or count or memoize to prevent infinite loop)
// TODO make safe
packagepath = path.resolve(xconfx.staticpath, text);
var packagepath = path.resolve(xconfx.staticpath, text);
if (0 !== packagepath.indexOf(xconfx.staticpath)) {
return securityError;
}
@ -127,9 +127,10 @@ module.exports.create = function (app, xconfx, apiFactories, apiDeps, errorIfApi
// TODO inter-process cache expirey
// TODO add to xconfx.staticpath
xconfx.staticpath = path.join(__dirname, '..', '..', 'packages', 'pages');
xconfx.sitespath = path.join(__dirname, '..', '..', 'packages', 'sites');
// Reads in each of the static apps as 'nodes'
return fs.readdirAsync(xconfx.staticpath).then(function (nodes) {
// Reads in each of the sites directives as 'nodes'
return fs.readdirAsync(xconfx.sitespath).then(function (nodes) {
if (opts && opts.clear) {
localCache.statics = {};
}
@ -144,7 +145,7 @@ module.exports.create = function (app, xconfx, apiFactories, apiDeps, errorIfApi
console.log('[all apps]', name);
if (!localCache.statics[name]) {
console.log('[load this app]', name);
localCache.statics[name] = { handler: loadHandler(name), createdAt: Date.now() };
localCache.statics[name] = { handler: loadSiteHandler(name), createdAt: Date.now() };
}
});