fix app loader issue

This commit is contained in:
AJ ONeal 2015-11-19 07:42:20 +00:00
parent 78178eb43d
commit 17c18a15f3
3 changed files with 47 additions and 24 deletions

View File

@ -12,7 +12,7 @@ console.info('\n\n\n[MASTER] Welcome to WALNUT!');
var cluster = require('cluster'); var cluster = require('cluster');
var path = require('path'); var path = require('path');
//var minWorkers = 2; //var minWorkers = 2;
var numCores = 1; // Math.max(minWorkers, require('os').cpus().length); var numCores = 2; // Math.max(minWorkers, require('os').cpus().length);
var workers = []; var workers = [];
var caddypath = '/usr/local/bin/caddy'; var caddypath = '/usr/local/bin/caddy';
var useCaddy = require('fs').existsSync(caddypath); var useCaddy = require('fs').existsSync(caddypath);
@ -100,8 +100,8 @@ cluster.on('exit', function (worker, code, signal) {
return w; return w;
}); });
console.log('WARNING: worker spawning turned off for debugging '); //console.log('WARNING: worker spawning turned off for debugging ');
//fork(); fork();
}); });
fork(); fork();

View File

@ -2,29 +2,34 @@
// TODO handle static app urls? // TODO handle static app urls?
// NOTE rejecting non-api urls should happen before this // NOTE rejecting non-api urls should happen before this
module.exports.create = function (conf, deps, app) { module.exports.create = function (conf, deps/*, Services*/) {
var PromiseA = deps.Promise;
var app = deps.app;
var express = deps.express;
var escapeStringRegexp = require('escape-string-regexp'); var escapeStringRegexp = require('escape-string-regexp');
var vhostsMap = conf.vhostsMap; var vhostsMap = conf.vhostsMap;
if (!app) {
app = deps.app;
}
function getApi(route) { function getApi(route) {
// TODO don't modify route, modify some other variable instead // TODO don't modify route, modify some other variable instead
var PromiseA = require('bluebird');
var path = require('path'); var path = require('path');
// TODO needs some version stuff (which would also allow hot-loading of updates) // TODO needs some version stuff (which would also allow hot-loading of updates)
// TODO version could be tied to sha256sum // TODO version could be tied to sha256sum
var pkgpath = path.join(conf.apipath, (route.api.package || route.api.id), (route.api.version || '')); var pkgpath = path.join(conf.apipath, (route.api.package || route.api.id), (route.api.version || ''));
return new PromiseA(function (resolve, reject) { return new PromiseA(function (resolve, reject) {
var myApp;
try { try {
// TODO dynamic requires are a no-no // TODO dynamic requires are a no-no
// can we statically generate a require-er? on each install? // can we statically generate a require-er? on each install?
// module.exports = { {{pkgpath}}: function () { return require({{pkgpath}}) } } // module.exports = { {{pkgpath}}: function () { return require({{pkgpath}}) } }
// requirer[pkgpath]() // requirer[pkgpath]()
route.route = require(pkgpath).create(conf, deps, app); myApp = express();
if (app.get('trust proxy')) {
myApp.set('trust proxy', app.get('trust proxy'));
}
route.route = require(pkgpath).create(conf, deps, myApp);
} catch(e) { } catch(e) {
reject(e); reject(e);
return; return;
@ -50,8 +55,10 @@ module.exports.create = function (conf, deps, app) {
pathname = '/api'; pathname = '/api';
} }
if (-1 === pathname.indexOf('/api')) { if (-1 === pathname.indexOf('/api')) {
// TODO needs namespace for current api
pathname = '/api' + pathname; pathname = '/api' + pathname;
} }
// pathname += '.local';
if (!route.re) { if (!route.re) {
route.re = new RegExp(escapeStringRegexp(pathname) + '(#|\\/|\\?|$)'); route.re = new RegExp(escapeStringRegexp(pathname) + '(#|\\/|\\?|$)');
@ -62,7 +69,8 @@ module.exports.create = function (conf, deps, app) {
// re.test("/api/foo") // re.test("/api/foo")
// re.test("/apifoo") // false // re.test("/apifoo") // false
if (route.re.test(req.url)) { if (route.re.test(req.url)) {
apps = route.apps; // make a copy
apps = route.apps.slice(0);
return true; return true;
} }
}); });
@ -90,6 +98,12 @@ module.exports.create = function (conf, deps, app) {
} }
if (route.route) { if (route.route) {
if (route.route.then) {
route.route.then(function (expressApp) {
expressApp(req, res, nextify);
});
return;
}
route.route(req, res, nextify); route.route(req, res, nextify);
return; return;
} }
@ -104,16 +118,20 @@ module.exports.create = function (conf, deps, app) {
return; return;
} }
getApi(route).then(function (route) { return getApi(route).then(function (expressApp) {
try { try {
route(req, res, nextify); expressApp(req, res, nextify);
route.route = route; route.route = expressApp;
} catch(e) { } catch(e) {
route._errored = true; route._errored = true;
console.error('[App Load Error]'); console.error('[App Load Error]');
console.error(e.stack);
nextify(new Error("couldn't load api")); nextify(new Error("couldn't load api"));
} }
return expressApp;
}, function (err) {
console.error('[App Promise Error]');
nextify(err);
}); });
} }

View File

@ -173,8 +173,6 @@ module.exports.create = function (webserver, info, state) {
}); });
function handleApi(req, res, next) { function handleApi(req, res, next) {
var myApp;
if (!/^\/api/.test(req.url)) { if (!/^\/api/.test(req.url)) {
next(); next();
return; return;
@ -189,25 +187,21 @@ module.exports.create = function (webserver, info, state) {
} }
if (apiHandler) { if (apiHandler) {
/*
if (apiHandler.then) { if (apiHandler.then) {
apiHandler.then(function (myApp) { apiHandler.then(function (myApp) {
myApp(req, res, next); myApp(req, res, next);
}); });
return; return;
} }
*/
apiHandler(req, res, next); apiHandler(req, res, next);
return; return;
} }
// apiHandler = require('./vhost-server').create(info.conf.localPort, vhostsdir).create(webserver, app)
myApp = express();
if (app.get('trust proxy')) {
myApp.set('trust proxy', app.get('trust proxy'));
}
apiHandler = require('./api-server').create(apiConf, { apiHandler = require('./api-server').create(apiConf, {
app: myApp memstore: memstore
, memstore: memstore
, sqlstores: sqlstores , sqlstores: sqlstores
, clientSqlFactory: clientFactory , clientSqlFactory: clientFactory
, systemSqlFactory: systemFactory , systemSqlFactory: systemFactory
@ -215,6 +209,8 @@ module.exports.create = function (webserver, info, state) {
//, handleRejection: require('./lib/common').rejectableRequest; //, handleRejection: require('./lib/common').rejectableRequest;
//, localPort: info.conf.localPort //, localPort: info.conf.localPort
, Promise: PromiseA , Promise: PromiseA
, express: express
, app: app
}, Services).api; }, Services).api;
apiHandler(req, res, next); apiHandler(req, res, next);
@ -248,6 +244,15 @@ module.exports.create = function (webserver, info, state) {
.use(require('connect-send-error').error()) .use(require('connect-send-error').error())
; ;
app.use('/', handleApi); app.use('/', handleApi);
app.use('/', function (err, req, res, next) {
console.error('[Error Handler]');
console.error(err.stack);
if (req.xhr) {
res.send({ error: { message: "kinda unknownish error" } });
} else {
res.send('<html><head><title>ERROR</title></head><body>Error</body></html>');
}
});
return app; return app;
}); });