add getSitePackageConfig
This commit is contained in:
parent
db32e76fc2
commit
8dcd1461bd
170
lib/apis.js
170
lib/apis.js
|
@ -56,7 +56,17 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
|
|||
});
|
||||
}
|
||||
|
||||
function getSitePackageConfig(clientUrih, pkgId) {
|
||||
var siteConfigPath = path.join(xconfx.appConfigPath, clientUrih);
|
||||
return mkdirpAsync(siteConfigPath).then(function () {
|
||||
return fs.readFileAsync(path.join(siteConfigPath, pkgId + '.json'), 'utf8').then(function (text) {
|
||||
return JSON.parse(text);
|
||||
}).then(function (data) { return data; }, function (/*err*/) { return {}; });
|
||||
});
|
||||
}
|
||||
|
||||
function getSiteConfig(clientUrih) {
|
||||
// TODO test if the requesting package has permission to the root-level site config
|
||||
var siteConfigPath = path.join(xconfx.appConfigPath, clientUrih);
|
||||
return mkdirpAsync(siteConfigPath).then(function () {
|
||||
return fs.readFileAsync(path.join(siteConfigPath, 'config.json'), 'utf8').then(function (text) {
|
||||
|
@ -65,7 +75,7 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
|
|||
});
|
||||
}
|
||||
|
||||
function loadRestHelper(myConf, pkgId) {
|
||||
function loadRestHelper(myConf, clientUrih, pkgId) {
|
||||
var pkgPath = path.join(myConf.restPath, pkgId);
|
||||
|
||||
// TODO should not require package.json. Should work with files alone.
|
||||
|
@ -99,24 +109,24 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
|
|||
myApp = express();
|
||||
|
||||
/*
|
||||
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
|
||||
};
|
||||
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
|
||||
};
|
||||
*/
|
||||
|
||||
var _getOauth3Controllers = pkgDeps.getOauth3Controllers = require('oauthcommon/example-oauthmodels').create(
|
||||
|
@ -125,13 +135,52 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
|
|||
//require('oauthcommon').inject(packagedApi._getOauth3Controllers, packagedApi._api, pkgConf, pkgDeps);
|
||||
require('oauthcommon').inject(_getOauth3Controllers, myApp/*, pkgConf, pkgDeps*/);
|
||||
|
||||
myApp.use('/', function preHandler(req, res, 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);
|
||||
myApp.use('/public', function preHandler(req, res, next) {
|
||||
// TODO authenticate or use guest user
|
||||
next();
|
||||
});
|
||||
|
||||
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)));
|
||||
|
||||
return mailer;
|
||||
}
|
||||
});
|
||||
|
||||
Object.defineProperty(req, 'getSitePackageConfig', {
|
||||
enumerable: true
|
||||
, configurable: false
|
||||
, writable: false
|
||||
, value: function getSitePackageConfigProp() {
|
||||
return getSitePackageConfig(clientUrih, pkgId);
|
||||
}
|
||||
});
|
||||
|
||||
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
|
||||
//
|
||||
|
@ -140,7 +189,7 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
|
|||
}/*pkgConf*/, pkgDeps/*pkgDeps*/, myApp/*myApp*/)).then(function (handler) {
|
||||
|
||||
myApp.use('/', function postHandler(req, res, next) {
|
||||
req.url = req.walnutOriginalUrl;
|
||||
req.url = req._walnutOriginalUrl;
|
||||
console.log('[posthandler] req.url', req.url);
|
||||
next();
|
||||
});
|
||||
|
@ -154,10 +203,10 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
|
|||
// Read packages/apis/sub.sld.tld (forward dns) to find list of apis as tld.sld.sub (reverse dns)
|
||||
// TODO packages/allowed_apis/sub.sld.tld (?)
|
||||
// TODO auto-register org.oauth3.consumer for primaryDomain (and all sites?)
|
||||
function loadRestHandler(myConf, pkgId) {
|
||||
function loadRestHandler(myConf, clientUrih, pkgId) {
|
||||
return PromiseA.resolve().then(function () {
|
||||
if (!localCache.pkgs[pkgId]) {
|
||||
return loadRestHelper(myConf, pkgId);
|
||||
return loadRestHelper(myConf, clientUrih, pkgId);
|
||||
}
|
||||
|
||||
return localCache.pkgs[pkgId];
|
||||
|
@ -240,55 +289,30 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
|
|||
return null;
|
||||
}
|
||||
|
||||
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 getSiteMailer() {
|
||||
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)));
|
||||
if (localCache.rests[pkgId]) {
|
||||
localCache.rests[pkgId].handler(req, res, next);
|
||||
hasBeenHandled = true;
|
||||
|
||||
return mailer;
|
||||
if (now - localCache.rests[pkgId].createdAt > staleAfter) {
|
||||
localCache.rests[pkgId] = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (!localCache.rests[pkgId]) {
|
||||
//return doesThisPkgExist
|
||||
|
||||
return loadRestHandler(xconfx, clientUrih, pkgId).then(function (myHandler) {
|
||||
if (!myHandler) {
|
||||
notConfigured(req, res);
|
||||
return;
|
||||
}
|
||||
|
||||
localCache.rests[pkgId] = { handler: myHandler.handler, createdAt: now };
|
||||
if (!hasBeenHandled) {
|
||||
myHandler.handler(req, res, next);
|
||||
}
|
||||
});
|
||||
|
||||
if (localCache.rests[pkgId]) {
|
||||
localCache.rests[pkgId].handler(req, res, next);
|
||||
hasBeenHandled = true;
|
||||
|
||||
if (now - localCache.rests[pkgId].createdAt > staleAfter) {
|
||||
localCache.rests[pkgId] = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (!localCache.rests[pkgId]) {
|
||||
//return doesThisPkgExist
|
||||
|
||||
return loadRestHandler(xconfx, pkgId).then(function (myHandler) {
|
||||
if (!myHandler) {
|
||||
notConfigured(req, res);
|
||||
return;
|
||||
}
|
||||
|
||||
localCache.rests[pkgId] = { handler: myHandler.handler, createdAt: now };
|
||||
if (!hasBeenHandled) {
|
||||
myHandler.handler(req, res, next);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue