From da008ea6583d519b82247761c1be4f84e1ecac56 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 20 May 2017 04:51:44 +0000 Subject: [PATCH] fix api naming convention --- lib/apis.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/apis.js b/lib/apis.js index b88419c..7058167 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -12,7 +12,11 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { xconfx.appApiGrantsPath = path.join(__dirname, '..', '..', 'packages', 'client-api-grants'); function notConfigured(req, res) { - res.send({ error: { message: "api package '" + req.pkgId + "' not configured for client uri '" + req.experienceId + "'" } }); + var msg = "api package '" + req.pkgId + "' not configured for client uri '" + req.experienceId + "'" + + ". To configure it place a new line '" + req.pkgId + "' in the file '/srv/walnut/packages/client-api-grants/" + req.experienceId + "'" + ; + + res.send({ error: { message: msg } }); } /* @@ -23,12 +27,14 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { function isThisClientAllowedToUseThisPkg(myConf, clientUrih, pkgId) { var appApiGrantsPath = path.join(myConf.appApiGrantsPath, clientUrih); - console.log('sanity exists?', appApiGrantsPath);; + console.log('sanity exists?', appApiGrantsPath); return fs.readFileAsync(appApiGrantsPath, 'utf8').then(function (text) { console.log('sanity', text); return text.trim().split(/\n/); - }, function (rer) { -console.error(rer); + }, function (err) { + if ('ENOENT' !== err.code) { + console.error(err); + } return []; }).then(function (apis) { if (apis.some(function (api) { @@ -128,8 +134,17 @@ console.error(rer); return function (req, res, next) { cors(req, res, function () { console.log('[sanity check]', req.url); + // Canonical client names + // example.com should use api.example.com/api for all requests + // sub.example.com/api should resolve to sub.example.com + // example.com/subpath/api should resolve to example.com#subapp + // sub.example.com/subpath/api should resolve to sub.example.com#subapp var clientUrih = req.hostname.replace(/^api\./, '') + req.url.replace(/\/api\/.*/, '/').replace(/\/+/g, '#').replace(/#$/, ''); - var pkgId = req.url.replace(/.*\/api\//, '').replace(/^\//, '').replace(/\/$/, ''); + // Canonical package names + // '/api/com.daplie.hello/hello' should resolve to 'com.daplie.hello' + // '/subapp/api/com.daplie.hello/hello' should also 'com.daplie.hello' + // '/subapp/api/com.daplie.hello/' may exist... must be a small api + var pkgId = req.url.replace(/.*\/api\//, '').replace(/^\//, '').replace(/\/.*/, ''); var now = Date.now(); var hasBeenHandled = false;