You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2019/07/24 13:44:23 UTC

[sling-whiteboard] 04/04: Read content from filesystem

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 14c7116f68d5bff05880d159d7adf05973efb0bc
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Wed Jul 24 15:44:04 2019 +0200

    Read content from filesystem
---
 .../content/demo/docs/somedoc.json                 |  5 ++++
 serverless-microsling/content/demo/index.json      |  5 ++++
 serverless-microsling/install                      |  2 +-
 serverless-microsling/lib/render.js                |  3 ++
 serverless-microsling/lib/resolve-content.js       | 35 +++++++++++++---------
 serverless-microsling/microsling.js                | 11 +++++--
 6 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/serverless-microsling/content/demo/docs/somedoc.json b/serverless-microsling/content/demo/docs/somedoc.json
new file mode 100644
index 0000000..997c1ce
--- /dev/null
+++ b/serverless-microsling/content/demo/docs/somedoc.json
@@ -0,0 +1,5 @@
+{
+    "resourceType": "microsling/somedoc",
+    "title": "Some Document",
+    "body": "This is some document"
+}
\ No newline at end of file
diff --git a/serverless-microsling/content/demo/index.json b/serverless-microsling/content/demo/index.json
new file mode 100644
index 0000000..8d4a19d
--- /dev/null
+++ b/serverless-microsling/content/demo/index.json
@@ -0,0 +1,5 @@
+{
+    "resourceType": "microsling/demo",
+    "title": "Demo root resource",
+    "body": "This is the content of the demo root resource"
+}
\ No newline at end of file
diff --git a/serverless-microsling/install b/serverless-microsling/install
index 2932f69..0933969 100755
--- a/serverless-microsling/install
+++ b/serverless-microsling/install
@@ -7,7 +7,7 @@ function fatal() {
 
 export ACTION=microsling
 export ZIP=openwhisk_action.zip
-export ACTION_FILES="package.json node_modules *.js lib/* foo"
+export ACTION_FILES="package.json node_modules *.js lib content"
 
 [[ -d node_modules ]] || fatal "node_modules folder not found, please run 'npm install'"
 
diff --git a/serverless-microsling/lib/render.js b/serverless-microsling/lib/render.js
index ff9842c..74f6c3f 100644
--- a/serverless-microsling/lib/render.js
+++ b/serverless-microsling/lib/render.js
@@ -14,8 +14,11 @@
  * limitations under the License.
  */
 
+ /* eslint-disable no-console */
+
 async function render(context) {
   const resource = context.content.resource.content;
+  console.log(resource);
   const markup = `
     <html>
     <head>
diff --git a/serverless-microsling/lib/resolve-content.js b/serverless-microsling/lib/resolve-content.js
index d9ba091..d40a894 100644
--- a/serverless-microsling/lib/resolve-content.js
+++ b/serverless-microsling/lib/resolve-content.js
@@ -14,22 +14,29 @@
  * limitations under the License.
  */
 
+/* eslint-disable no-console */
+
+const fs = require('fs');
+
 async function resolveContent(context) {
-  if(!context.request.path.startsWith('/demo/')) {
-    throw {
-      httpStatus: 404,
-      message: `path not found: ${context.request.path}`,
-    };
-  }
-  context.content["resource"] = {
-    path: context.request.path,
-    resourceType: 'microsling/demo',
-    content: {
-      title: `This is a demo resource at ${context.request.path}`,
-      body: "Here's the body of the demo resource"
+  const filePath = `../content${context.request.path}`;
+  return new Promise(resolve => {
+    try {
+      fs.readFile(require.resolve(filePath), (err, text) => {
+        context.content.resource = {
+          path: context.request.path,
+          content: JSON.parse(text)
+        };
+        return resolve(context);
+    
+      });
+    } catch(e) {
+      throw {
+        httpStatus: 404,
+        message: `path not found: ${context.request.path}`,
+      };
     }
-  }
-  return context;
+  });
 }
 
 module.exports.resolveContent = resolveContent;
\ No newline at end of file
diff --git a/serverless-microsling/microsling.js b/serverless-microsling/microsling.js
index 1e9a6db..46652b2 100644
--- a/serverless-microsling/microsling.js
+++ b/serverless-microsling/microsling.js
@@ -20,27 +20,31 @@ const { resolveContent } = require('./lib/resolve-content.js');
 const { render } = require('./lib/render.js');
 
 function main (params) {
+  const { debug } = params;
   const context = {
     request : {
       path: params.__ow_path,
     },
     response: {},
-    content: {},
+    content: {}
   };
 
   return new Promise(function (resolve) {
+    if(debug) console.log(`start: ${JSON.stringify(context, 2, null)}`);
     resolveContent(context)
     .then(context => {
+      if(debug) console.log(`pre-render: ${JSON.stringify(context, 2, null)}`);
       return render(context);
     })
     .then(context => {
-        return resolve(context.response);
+      if(debug) console.log(`pre-resolve: ${JSON.stringify(context, 2, null)}`);
+      return resolve(context.response);
     })
     .catch(e => {
       if(e.httpStatus) {
         return resolve({ status: e.httpStatus, body: e.message});
       } else {
-        return resolve({ status: 500, body: JSON.stringify(e, 2, null)});
+        throw e;
       }
     })
   })
@@ -58,6 +62,7 @@ if (require.main === module) {
     // eslint-disable-next-line no-undef
     __ow_path: process.argv[2],
     __ow_method: 'get',
+    debug: false
   });
 }