allow multiple pages too

This commit is contained in:
AJ ONeal 2015-12-02 12:42:23 +00:00
parent 760f00af35
commit 8cc7234d64
1 changed files with 14 additions and 19 deletions

View File

@ -62,18 +62,18 @@ function compileVhosts(vhostsMap) {
return results;
}
function loadPages(pkgConf, route, req, res, next) {
function loadPages(pkgConf, packagedPage, req, res, next) {
var PromiseA = require('bluebird');
var fs = require('fs');
var path = require('path');
var pkgpath = path.join(pkgConf.pagespath, (route.app.package || route.app.id), (route.app.version || ''));
var pkgpath = path.join(pkgConf.pagespath, (packagedPage.package || packagedPage.id), (packagedPage.version || ''));
// TODO special cases for /.well_known/ and similar (oauth3.html, oauth3.json, webfinger, etc)
function handlePromise(p) {
p.then(function (app) {
app(req, res, next);
route._app = app;
packagedPage._page = app;
}, function (err) {
console.error('[App Promise Error]');
next(err);
@ -81,13 +81,13 @@ function loadPages(pkgConf, route, req, res, next) {
}
if (staticHandlers[pkgpath]) {
route._app = staticHandlers[pkgpath];
route._app(req, res, next);
packagedPage._page = staticHandlers[pkgpath];
packagedPage._page(req, res, next);
return;
}
if (!route._promise_app) {
route._promise_app = new PromiseA(function (resolve, reject) {
if (!packagedPage._promise_page) {
packagedPage._promise_page = new PromiseA(function (resolve, reject) {
fs.exists(pkgpath, function (exists) {
if (!exists) {
reject(new Error("package '" + pkgpath + "' is registered but does not exist"));
@ -100,7 +100,7 @@ function loadPages(pkgConf, route, req, res, next) {
});
}
handlePromise(route._promise_app);
handlePromise(packagedPage._promise_page);
}
function getApi(pkgConf, pkgDeps, packagedApi) {
@ -194,7 +194,7 @@ function layerItUp(pkgConf, router, req, res, next) {
// i.e. oauth3.html isn't in *your* app but you may use it and want it mounted at /.well-known/oauth3.html
// or perhaps some dynamic content (like application cache)
function nextify(err) {
var route;
var packagedPage;
nexti += 1;
if (err) {
@ -205,24 +205,19 @@ function layerItUp(pkgConf, router, req, res, next) {
// shortest to longest
//route = packages.pop();
// longest to shortest
route = router.packages[nexti];
if (!route) {
packagedPage = router.packagedPages[nexti];
if (!packagedPage) {
next();
return;
}
if (!route.app) {
// new Error("no Static App is registered for the specified path")
nextify();
return;
}
if (route._app) {
route._app(req, res, nextify);
if (packagedPage._page) {
packagedPage._page(req, res, nextify);
return;
}
// could attach to req.{ pkgConf, pkgDeps, Services}
loadPages(pkgConf, route, req, res, next);
loadPages(pkgConf, packagedPage, req, res, next);
}
nextify();