This is an automated email from the ASF dual-hosted git repository. bdelacretaz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
commit e6e6478a197e16d46f1977ccbf63c6e416c968e8 Author: Bertrand Delacretaz <[email protected]> AuthorDate: Fri Jul 26 12:21:46 2019 +0200 Content-type provided by action annotation --- serverless-microsling/install | 2 +- serverless-microsling/lib/default-renderers.js | 9 ++---- serverless-microsling/lib/openwhisk-renderer.js | 43 +++++++++++++++++++------ serverless-microsling/lib/render.js | 17 ++++------ 4 files changed, 44 insertions(+), 27 deletions(-) diff --git a/serverless-microsling/install b/serverless-microsling/install index 8e5283e..eccbccc 100755 --- a/serverless-microsling/install +++ b/serverless-microsling/install @@ -15,7 +15,7 @@ echo Preparing $ZIP ... \ && zip -r $ZIP $ACTION_FILES > /dev/null \ && echo "$ZIP: $(du -h $ZIP | cut -f1)" \ && echo "Updating action '$ACTION...'" \ -&& wsk action update $ACTION $ZIP --web true --kind nodejs:10 --param debug true +&& wsk action update $ACTION $ZIP --web true --kind nodejs:10 --param debug true -a provide-api-key true echo "Installing rendering actions..." wsk action update somedoc-rendering rendering-actions/somedoc-html.js -a sling:resourceType microsling/somedoc -a sling:extensions html diff --git a/serverless-microsling/lib/default-renderers.js b/serverless-microsling/lib/default-renderers.js index dca7c27..0562249 100644 --- a/serverless-microsling/lib/default-renderers.js +++ b/serverless-microsling/lib/default-renderers.js @@ -19,27 +19,24 @@ module.exports.renderers = { text: { - contentType: 'text/plain', getRendererInfo : (resourceType, extension) => { - return extension == 'txt'; + return extension == 'txt' ? { contentType : 'text/plain' } : null; }, render : (resource) => { return { output: `${resource.title}\n${resource.body}\n` }; }, }, json: { - contentType: 'application/json', getRendererInfo : (resourceType, extension) => { - return extension == 'json'; + return extension == 'json' ? { contentType : 'application/json' } : null; }, render : (resource) => { return { output: JSON.stringify(resource, 2, null) }; }, }, html: { - contentType: 'text/html', getRendererInfo : (resourceType, extension) => { - return extension == 'html'; + return extension == 'html' ? { contentType : 'text/html' } : null; }, render : (resource) => { return { output: ` diff --git a/serverless-microsling/lib/openwhisk-renderer.js b/serverless-microsling/lib/openwhisk-renderer.js index 199e26d..a323f94 100644 --- a/serverless-microsling/lib/openwhisk-renderer.js +++ b/serverless-microsling/lib/openwhisk-renderer.js @@ -19,11 +19,13 @@ const openwhisk = require('openwhisk'); const getAnnotation = (act, key) => { - const annotation = act.annotations.find(ann => key == ann.key); - return annotation ? annotation.value : undefined; + if(act != null) { + const annotation = act.annotations.find(ann => key == ann.key); + return annotation ? annotation.value : undefined; + } } -const getAction = async (resourceType, extension) => { +const getActionInfo = async (resourceType, extension) => { return new Promise(resolve => { var ow = openwhisk(); ow.actions.list() @@ -31,7 +33,10 @@ const getAction = async (resourceType, extension) => { const act = actions.find(act => { return resourceType == getAnnotation(act, 'sling:resourceType') && extension == getAnnotation(act, 'sling:extensions') }) - resolve(act); + resolve({ + action: act, + contentType: getAnnotation(act, 'sling:contentType'), + }); }) .catch(e => { throw e; @@ -39,8 +44,11 @@ const getAction = async (resourceType, extension) => { }) }; -const renderWithAction = (resource, action) => { - const name = action.name; +const renderWithAction = (resource, actionInfo) => { + if(!actionInfo.action) { + throw Error("No Action provided, cannot render"); + } + const name = actionInfo.action.name; const blocking = true, result = true const params = { resource: resource @@ -50,9 +58,8 @@ const renderWithAction = (resource, action) => { }; const renderer = { - contentType: 'text/html', getRendererInfo : async (resourceType, extension) => { - return getAction(resourceType, extension) + return getActionInfo(resourceType, extension); }, render : (resource, action) => { return renderWithAction(resource, action); @@ -60,15 +67,31 @@ const renderWithAction = (resource, action) => { } // For testing as a standalone OpenWhisk action +// (requires installing the action with -a provide-api-key true) +// or from the command line function main () { - return new Promise(async resolve => { + return new Promise(async (resolve, reject) => { const resource = { title: 'cmdline title test', body: 'cmdline body test', } - resolve(renderWithAction(resource, getAction('microsling/somedoc', 'html'))); + try { + const actionInfo = await getActionInfo('microsling/somedoc', 'html'); + console.log(`actionInfo=${JSON.stringify(actionInfo, 2, null)}`); + const rendered = await renderWithAction(resource, actionInfo); + console.log(`rendered=${JSON.stringify(rendered, 2, null)}`); + resolve(rendered); + } catch(e) { + reject(e); + } }); } +// From the command line, __OW_API_HOST and __OW_API_KEY environment +// variables must be set +if (require.main === module) { + main(); +} + module.exports.openWhiskRenderer = renderer; module.exports.main = main; \ No newline at end of file diff --git a/serverless-microsling/lib/render.js b/serverless-microsling/lib/render.js index 5ceaa25..91fcaf9 100644 --- a/serverless-microsling/lib/render.js +++ b/serverless-microsling/lib/render.js @@ -31,21 +31,18 @@ const renderers = [ async function selectRendererInfo(resourceType, extension) { return new Promise(async resolve => { let i; - let resolved; + let result; for(i in renderers) { const rendererInfo = await renderers[i].getRendererInfo(resourceType, extension); if(rendererInfo) { - resolve({ + result = { 'renderer': renderers[i], - 'rendererInfo': rendererInfo, - }); - resolved = true; + 'info': rendererInfo, + }; break; } } - if(!resolved) { - resolve(); - } + resolve(result); }) } @@ -63,13 +60,13 @@ async function render(context) { if(!rendererInfo) { throw Error(`Renderer not found for ${resourceType} extension ${extension}`); } - const rendered = await rendererInfo.renderer.render(resource, rendererInfo.rendererInfo); + const rendered = await rendererInfo.renderer.render(resource, rendererInfo.info); if(!rendered.output) { throw Error('Renderer generated no output'); } context.response.body = rendered.output; context.response.headers = { - 'Content-Type': rendererInfo.renderer.contentType + 'Content-Type': rendererInfo.contentType }; return context;
