From 064f4ca9035539bd2f621e8e9015df6d2a521516 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 26 May 2017 23:09:49 +0000 Subject: [PATCH] support text linked packages --- lib/apis.js | 251 +++++++++++++++++++++++++--------------------------- 1 file changed, 121 insertions(+), 130 deletions(-) diff --git a/lib/apis.js b/lib/apis.js index 7bce80f..249c051 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -108,155 +108,146 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { function loadRestHelper(myConf, clientUrih, pkgId) { var pkgPath = path.join(myConf.restPath, pkgId); + var pkgLinks = []; + pkgLinks.push(pkgId); - // 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; - - if (pkg.walnut) { - pkgPath = path.join(pkgPath, pkg.walnut); + // TODO allow recursion, but catch cycles + return fs.lstatAsync(pkgPath).then(function (stat) { + if (!stat.isFile()) { + return; } - Object.keys(apiDeps).forEach(function (key) { - pkgDeps[key] = apiDeps[key]; - }); - Object.keys(apiFactories).forEach(function (key) { - pkgDeps[key] = apiFactories[key]; + return fs.readFileAsync(pkgPath, 'utf8').then(function (text) { + pkgId = text.trim(); + pkgPath = path.join(myConf.restPath, pkgId); }); + }, 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 - // - // 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); + if (pkg.walnut) { + pkgPath = path.join(pkgPath, pkg.walnut); + } - // let's go with this one for now and the api can choose to scope or not to scope - pkgDeps.memstore = apiFactories.memstoreFactory.create(pkgId); + Object.keys(apiDeps).forEach(function (key) { + pkgDeps[key] = apiDeps[key]; + }); + Object.keys(apiFactories).forEach(function (key) { + pkgDeps[key] = apiFactories[key]; + }); - console.log('DEBUG pkgPath', pkgPath); - myApp = express(); + // TODO pull db stuff from package.json somehow and pass allowed data models as deps + // + // 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); -/* - var pkgConf = { - 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 - }; -*/ + // let's go with this one for now and the api can choose to scope or not to scope + pkgDeps.memstore = apiFactories.memstoreFactory.create(pkgId); - var _getOauth3Controllers = pkgDeps.getOauth3Controllers = require('oauthcommon/example-oauthmodels').create( - { sqlite3Sock: xconfx.sqlite3Sock, ipcKey: xconfx.ipcKey } - ).getControllers; - //require('oauthcommon').inject(packagedApi._getOauth3Controllers, packagedApi._api, pkgConf, pkgDeps); - require('oauthcommon').inject(_getOauth3Controllers, myApp/*, pkgConf, pkgDeps*/); + console.log('DEBUG pkgPath', pkgPath); + myApp = express(); - myApp.use('/public', function preHandler(req, res, next) { - // TODO authenticate or use guest user - next(); - }); + var _getOauth3Controllers = pkgDeps.getOauth3Controllers = require('oauthcommon/example-oauthmodels').create( + { sqlite3Sock: xconfx.sqlite3Sock, ipcKey: xconfx.ipcKey } + ).getControllers; + //require('oauthcommon').inject(packagedApi._getOauth3Controllers, packagedApi._api, pkgConf, pkgDeps); + require('oauthcommon').inject(_getOauth3Controllers, myApp/*, pkgConf, pkgDeps*/); - myApp.use('/', function preHandler(req, res, next) { - return getSiteConfig(clientUrih).then(function (siteConfig) { - /* - 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))); + myApp.use('/public', function preHandler(req, res, next) { + // TODO authenticate or use guest user + next(); + }); - 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', { - 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 mailer; } + }); - 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; - // "/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(); + localCache.pkgs[pkgId] = { pkgId: pkgId, pkg: pkg, handler: handler || myApp, createdAt: Date.now() }; + pkgLinks.forEach(function (pkgLink) { + localCache.pkgs[pkgLink] = localCache.pkgs[pkgId]; + }); + 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]; - }); }); }