support text linked packages

This commit is contained in:
AJ ONeal 2017-05-26 23:09:49 +00:00
parent 69a18ad7d4
commit 064f4ca903
1 changed files with 121 additions and 130 deletions

View File

@ -108,155 +108,146 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
function loadRestHelper(myConf, clientUrih, pkgId) { function loadRestHelper(myConf, clientUrih, pkgId) {
var pkgPath = path.join(myConf.restPath, pkgId); var pkgPath = path.join(myConf.restPath, pkgId);
var pkgLinks = [];
pkgLinks.push(pkgId);
// TODO should not require package.json. Should work with files alone. // TODO allow recursion, but catch cycles
return fs.readFileAsync(path.join(pkgPath, 'package.json'), 'utf8').then(function (text) { return fs.lstatAsync(pkgPath).then(function (stat) {
var pkg = JSON.parse(text); if (!stat.isFile()) {
var pkgDeps = {}; return;
var myApp;
if (pkg.walnut) {
pkgPath = path.join(pkgPath, pkg.walnut);
} }
Object.keys(apiDeps).forEach(function (key) { return fs.readFileAsync(pkgPath, 'utf8').then(function (text) {
pkgDeps[key] = apiDeps[key]; pkgId = text.trim();
}); pkgPath = path.join(myConf.restPath, pkgId);
Object.keys(apiFactories).forEach(function (key) {
pkgDeps[key] = apiFactories[key];
}); });
}, function () {
// ignore error
return;
}).then(function () {
// TODO should not require package.json. Should work with files alone.
return fs.readFileAsync(path.join(pkgPath, 'package.json'), 'utf8').then(function (text) {
var pkg = JSON.parse(text);
var pkgDeps = {};
var myApp;
// TODO pull db stuff from package.json somehow and pass allowed data models as deps if (pkg.walnut) {
// pkgPath = path.join(pkgPath, pkg.walnut);
// how can we tell which of these would be correct? }
// deps.memstore = apiFactories.memstoreFactory.create(pkgId);
// deps.memstore = apiFactories.memstoreFactory.create(req.experienceId);
// deps.memstore = apiFactories.memstoreFactory.create(req.experienceId + pkgId);
// let's go with this one for now and the api can choose to scope or not to scope Object.keys(apiDeps).forEach(function (key) {
pkgDeps.memstore = apiFactories.memstoreFactory.create(pkgId); pkgDeps[key] = apiDeps[key];
});
Object.keys(apiFactories).forEach(function (key) {
pkgDeps[key] = apiFactories[key];
});
console.log('DEBUG pkgPath', pkgPath); // TODO pull db stuff from package.json somehow and pass allowed data models as deps
myApp = express(); //
// how can we tell which of these would be correct?
// deps.memstore = apiFactories.memstoreFactory.create(pkgId);
// deps.memstore = apiFactories.memstoreFactory.create(req.experienceId);
// deps.memstore = apiFactories.memstoreFactory.create(req.experienceId + pkgId);
/* // let's go with this one for now and the api can choose to scope or not to scope
var pkgConf = { pkgDeps.memstore = apiFactories.memstoreFactory.create(pkgId);
pagespath: path.join(__dirname, '..', '..', 'packages', 'pages') + path.sep
, apipath: path.join(__dirname, '..', '..', 'packages', 'apis') + path.sep
, servicespath: path.join(__dirname, '..', '..', 'packages', 'services')
, vhostsMap: vhostsMap
, vhostPatterns: null
, server: webserver
, externalPort: info.conf.externalPort
, primaryNameserver: info.conf.primaryNameserver
, nameservers: info.conf.nameservers
, privkey: info.conf.privkey
, pubkey: info.conf.pubkey
, redirects: info.conf.redirects
, apiPrefix: '/api'
, 'org.oauth3.consumer': info.conf['org.oauth3.consumer']
, 'org.oauth3.provider': info.conf['org.oauth3.provider']
, keys: info.conf.keys
};
*/
var _getOauth3Controllers = pkgDeps.getOauth3Controllers = require('oauthcommon/example-oauthmodels').create( console.log('DEBUG pkgPath', pkgPath);
{ sqlite3Sock: xconfx.sqlite3Sock, ipcKey: xconfx.ipcKey } myApp = express();
).getControllers;
//require('oauthcommon').inject(packagedApi._getOauth3Controllers, packagedApi._api, pkgConf, pkgDeps);
require('oauthcommon').inject(_getOauth3Controllers, myApp/*, pkgConf, pkgDeps*/);
myApp.use('/public', function preHandler(req, res, next) { var _getOauth3Controllers = pkgDeps.getOauth3Controllers = require('oauthcommon/example-oauthmodels').create(
// TODO authenticate or use guest user { sqlite3Sock: xconfx.sqlite3Sock, ipcKey: xconfx.ipcKey }
next(); ).getControllers;
}); //require('oauthcommon').inject(packagedApi._getOauth3Controllers, packagedApi._api, pkgConf, pkgDeps);
require('oauthcommon').inject(_getOauth3Controllers, myApp/*, pkgConf, pkgDeps*/);
myApp.use('/', function preHandler(req, res, next) { myApp.use('/public', function preHandler(req, res, next) {
return getSiteConfig(clientUrih).then(function (siteConfig) { // TODO authenticate or use guest user
/* next();
Object.defineProperty(req, 'siteConfig', { });
enumerable: true
, configurable: false
, writable: false
, value: siteConfig
});
*/
Object.defineProperty(req, 'getSiteMailer', {
enumerable: true
, configurable: false
, writable: false
, value: function getSiteMailerProp() {
var nodemailer = require('nodemailer');
var transport = require('nodemailer-mailgun-transport');
//var mailconf = require('../../../com.daplie.mailer/config.mailgun');
var mailconf = siteConfig['mailgun.org'];
var mailer = PromiseA.promisifyAll(nodemailer.createTransport(transport(mailconf)));
return mailer; myApp.use('/', function preHandler(req, res, next) {
} return getSiteConfig(clientUrih).then(function (siteConfig) {
}); Object.defineProperty(req, 'getSiteMailer', {
enumerable: true
, configurable: false
, writable: false
, value: function getSiteMailerProp() {
var nodemailer = require('nodemailer');
var transport = require('nodemailer-mailgun-transport');
//var mailconf = require('../../../com.daplie.mailer/config.mailgun');
var mailconf = siteConfig['mailgun.org'];
var mailer = PromiseA.promisifyAll(nodemailer.createTransport(transport(mailconf)));
Object.defineProperty(req, 'getSiteConfig', { return mailer;
enumerable: true
, configurable: false
, writable: false
, value: function getSiteMailerProp(section) {
return PromiseA.resolve((siteConfig || {})[section]);
}
});
Object.defineProperty(req, 'getSitePackageConfig', {
enumerable: true
, configurable: false
, writable: false
, value: function getSitePackageConfigProp() {
return getSitePackageConfig(clientUrih, pkgId);
}
});
Object.defineProperty(req, 'getSiteStore', {
enumerable: true
, configurable: false
, writable: false
, value: function getSiteStoreProp() {
var restPath = path.join(myConf.restPath, pkgId);
var apiPath = path.join(myConf.apiPath, pkgId);
var dir;
// TODO usage package.json as a falback if the standard location is not used
try {
dir = require(path.join(apiPath, 'models.js'));
} catch(e) {
dir = require(path.join(restPath, 'models.js'));
} }
});
return getSiteStore(clientUrih, pkgId, dir); Object.defineProperty(req, 'getSiteConfig', {
} enumerable: true
, configurable: false
, writable: false
, value: function getSiteMailerProp(section) {
return PromiseA.resolve((siteConfig || {})[section]);
}
});
Object.defineProperty(req, 'getSitePackageConfig', {
enumerable: true
, configurable: false
, writable: false
, value: function getSitePackageConfigProp() {
return getSitePackageConfig(clientUrih, pkgId);
}
});
Object.defineProperty(req, 'getSiteStore', {
enumerable: true
, configurable: false
, writable: false
, value: function getSiteStoreProp() {
var restPath = path.join(myConf.restPath, pkgId);
var apiPath = path.join(myConf.apiPath, pkgId);
var dir;
// TODO usage package.json as a falback if the standard location is not used
try {
dir = require(path.join(apiPath, 'models.js'));
} catch(e) {
dir = require(path.join(restPath, 'models.js'));
}
return getSiteStore(clientUrih, pkgId, dir);
}
});
req._walnutOriginalUrl = req.url;
// "/path/api/com.example/hello".replace(/.*\/api\//, '').replace(/([^\/]*\/+)/, '/') => '/hello'
req.url = req.url.replace(/\/api\//, '').replace(/.*\/api\//, '').replace(/([^\/]*\/+)/, '/');
console.log('[prehandler] req.url', req.url);
next();
});
});
//
// TODO handle /accounts/:accountId
//
return PromiseA.resolve(require(pkgPath).create({
etcpath: xconfx.etcpath
}/*pkgConf*/, pkgDeps/*pkgDeps*/, myApp/*myApp*/)).then(function (handler) {
myApp.use('/', function postHandler(req, res, next) {
req.url = req._walnutOriginalUrl;
console.log('[posthandler] req.url', req.url);
next();
}); });
req._walnutOriginalUrl = req.url; localCache.pkgs[pkgId] = { pkgId: pkgId, pkg: pkg, handler: handler || myApp, createdAt: Date.now() };
// "/path/api/com.example/hello".replace(/.*\/api\//, '').replace(/([^\/]*\/+)/, '/') => '/hello' pkgLinks.forEach(function (pkgLink) {
req.url = req.url.replace(/\/api\//, '').replace(/.*\/api\//, '').replace(/([^\/]*\/+)/, '/'); localCache.pkgs[pkgLink] = localCache.pkgs[pkgId];
console.log('[prehandler] req.url', req.url); });
next(); return localCache.pkgs[pkgId];
}); });
}); });
//
// TODO handle /accounts/:accountId
//
return PromiseA.resolve(require(pkgPath).create({
etcpath: xconfx.etcpath
}/*pkgConf*/, pkgDeps/*pkgDeps*/, myApp/*myApp*/)).then(function (handler) {
myApp.use('/', function postHandler(req, res, next) {
req.url = req._walnutOriginalUrl;
console.log('[posthandler] req.url', req.url);
next();
});
localCache.pkgs[pkgId] = { pkg: pkg, handler: handler || myApp, createdAt: Date.now() };
return localCache.pkgs[pkgId];
});
}); });
} }