add getSitePackageConfig
This commit is contained in:
parent
db32e76fc2
commit
8dcd1461bd
86
lib/apis.js
86
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) {
|
function getSiteConfig(clientUrih) {
|
||||||
|
// TODO test if the requesting package has permission to the root-level site config
|
||||||
var siteConfigPath = path.join(xconfx.appConfigPath, clientUrih);
|
var siteConfigPath = path.join(xconfx.appConfigPath, clientUrih);
|
||||||
return mkdirpAsync(siteConfigPath).then(function () {
|
return mkdirpAsync(siteConfigPath).then(function () {
|
||||||
return fs.readFileAsync(path.join(siteConfigPath, 'config.json'), 'utf8').then(function (text) {
|
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);
|
var pkgPath = path.join(myConf.restPath, pkgId);
|
||||||
|
|
||||||
// TODO should not require package.json. Should work with files alone.
|
// TODO should not require package.json. Should work with files alone.
|
||||||
|
@ -125,13 +135,52 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
|
||||||
//require('oauthcommon').inject(packagedApi._getOauth3Controllers, packagedApi._api, pkgConf, pkgDeps);
|
//require('oauthcommon').inject(packagedApi._getOauth3Controllers, packagedApi._api, pkgConf, pkgDeps);
|
||||||
require('oauthcommon').inject(_getOauth3Controllers, myApp/*, pkgConf, pkgDeps*/);
|
require('oauthcommon').inject(_getOauth3Controllers, myApp/*, pkgConf, pkgDeps*/);
|
||||||
|
|
||||||
|
myApp.use('/public', function preHandler(req, res, next) {
|
||||||
|
// TODO authenticate or use guest user
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
myApp.use('/', function preHandler(req, res, next) {
|
myApp.use('/', function preHandler(req, res, next) {
|
||||||
req.walnutOriginalUrl = req.url;
|
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'
|
// "/path/api/com.example/hello".replace(/.*\/api\//, '').replace(/([^\/]*\/+)/, '/') => '/hello'
|
||||||
req.url = req.url.replace(/\/api\//, '').replace(/.*\/api\//, '').replace(/([^\/]*\/+)/, '/');
|
req.url = req.url.replace(/\/api\//, '').replace(/.*\/api\//, '').replace(/([^\/]*\/+)/, '/');
|
||||||
console.log('[prehandler] req.url', req.url);
|
console.log('[prehandler] req.url', req.url);
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
//
|
//
|
||||||
// TODO handle /accounts/:accountId
|
// TODO handle /accounts/:accountId
|
||||||
//
|
//
|
||||||
|
@ -140,7 +189,7 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
|
||||||
}/*pkgConf*/, pkgDeps/*pkgDeps*/, myApp/*myApp*/)).then(function (handler) {
|
}/*pkgConf*/, pkgDeps/*pkgDeps*/, myApp/*myApp*/)).then(function (handler) {
|
||||||
|
|
||||||
myApp.use('/', function postHandler(req, res, next) {
|
myApp.use('/', function postHandler(req, res, next) {
|
||||||
req.url = req.walnutOriginalUrl;
|
req.url = req._walnutOriginalUrl;
|
||||||
console.log('[posthandler] req.url', req.url);
|
console.log('[posthandler] req.url', req.url);
|
||||||
next();
|
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)
|
// 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 packages/allowed_apis/sub.sld.tld (?)
|
||||||
// TODO auto-register org.oauth3.consumer for primaryDomain (and all sites?)
|
// 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 () {
|
return PromiseA.resolve().then(function () {
|
||||||
if (!localCache.pkgs[pkgId]) {
|
if (!localCache.pkgs[pkgId]) {
|
||||||
return loadRestHelper(myConf, pkgId);
|
return loadRestHelper(myConf, clientUrih, pkgId);
|
||||||
}
|
}
|
||||||
|
|
||||||
return localCache.pkgs[pkgId];
|
return localCache.pkgs[pkgId];
|
||||||
|
@ -240,30 +289,6 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
|
||||||
return null;
|
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)));
|
|
||||||
|
|
||||||
return mailer;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (localCache.rests[pkgId]) {
|
if (localCache.rests[pkgId]) {
|
||||||
localCache.rests[pkgId].handler(req, res, next);
|
localCache.rests[pkgId].handler(req, res, next);
|
||||||
hasBeenHandled = true;
|
hasBeenHandled = true;
|
||||||
|
@ -276,7 +301,7 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
|
||||||
if (!localCache.rests[pkgId]) {
|
if (!localCache.rests[pkgId]) {
|
||||||
//return doesThisPkgExist
|
//return doesThisPkgExist
|
||||||
|
|
||||||
return loadRestHandler(xconfx, pkgId).then(function (myHandler) {
|
return loadRestHandler(xconfx, clientUrih, pkgId).then(function (myHandler) {
|
||||||
if (!myHandler) {
|
if (!myHandler) {
|
||||||
notConfigured(req, res);
|
notConfigured(req, res);
|
||||||
return;
|
return;
|
||||||
|
@ -290,6 +315,5 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue