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:22 UTC

[sling-whiteboard] 03/04: Stub rendering function

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 03b224533e38b6d2cd54e096f183da88f60f8eff
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Wed Jul 24 14:48:30 2019 +0200

    Stub rendering function
---
 serverless-microsling/.eslintrc.json               | 17 ++++++++++
 .../lib/{resolve-content.js => render.js}          | 36 ++++++++++++----------
 serverless-microsling/lib/resolve-content.js       |  8 ++---
 serverless-microsling/microsling.js                | 18 ++++++++---
 4 files changed, 55 insertions(+), 24 deletions(-)

diff --git a/serverless-microsling/.eslintrc.json b/serverless-microsling/.eslintrc.json
new file mode 100644
index 0000000..9a81a27
--- /dev/null
+++ b/serverless-microsling/.eslintrc.json
@@ -0,0 +1,17 @@
+{
+    "env": {
+        "browser": true,
+        "commonjs": true,
+        "es6": true
+    },
+    "extends": "eslint:recommended",
+    "globals": {
+        "Atomics": "readonly",
+        "SharedArrayBuffer": "readonly"
+    },
+    "parserOptions": {
+        "ecmaVersion": 2018
+    },
+    "rules": {
+    }
+}
\ No newline at end of file
diff --git a/serverless-microsling/lib/resolve-content.js b/serverless-microsling/lib/render.js
similarity index 59%
copy from serverless-microsling/lib/resolve-content.js
copy to serverless-microsling/lib/render.js
index 2c9c855..ff9842c 100644
--- a/serverless-microsling/lib/resolve-content.js
+++ b/serverless-microsling/lib/render.js
@@ -14,22 +14,26 @@
  * limitations under the License.
  */
 
-async function resolveContent(context) {
-  if(!context.path.startsWith('/demo/')) {
-    throw {
-      httpStatus: 404,
-      message: `path not found: ${context.path}`,
-    };
-  }
-  context.content["resource"] = {
-    path: context.path,
-    resourceType: 'microsling/demo',
-    content: {
-      title: "This is a demo resource",
-      body: "Here's the body of the demo resource"
-    }
-  }
+async function render(context) {
+  const resource = context.content.resource.content;
+  const markup = `
+    <html>
+    <head>
+    <title>${resource.title}</title>
+    </head>
+    <body>
+    <h1>
+        ${resource.title}
+    </h1>
+    <div>${resource.body}</div>
+    </body>
+    </html>
+  `;
+  context.response.body = markup;
+  context.response.headers = {
+    'Content-Type': 'text/html',
+  };
   return context;
 }
 
-module.exports.resolveContent = resolveContent;
\ No newline at end of file
+module.exports.render = render;
\ No newline at end of file
diff --git a/serverless-microsling/lib/resolve-content.js b/serverless-microsling/lib/resolve-content.js
index 2c9c855..d9ba091 100644
--- a/serverless-microsling/lib/resolve-content.js
+++ b/serverless-microsling/lib/resolve-content.js
@@ -15,17 +15,17 @@
  */
 
 async function resolveContent(context) {
-  if(!context.path.startsWith('/demo/')) {
+  if(!context.request.path.startsWith('/demo/')) {
     throw {
       httpStatus: 404,
-      message: `path not found: ${context.path}`,
+      message: `path not found: ${context.request.path}`,
     };
   }
   context.content["resource"] = {
-    path: context.path,
+    path: context.request.path,
     resourceType: 'microsling/demo',
     content: {
-      title: "This is a demo resource",
+      title: `This is a demo resource at ${context.request.path}`,
       body: "Here's the body of the demo resource"
     }
   }
diff --git a/serverless-microsling/microsling.js b/serverless-microsling/microsling.js
index f125378..1e9a6db 100644
--- a/serverless-microsling/microsling.js
+++ b/serverless-microsling/microsling.js
@@ -14,24 +14,33 @@
  * limitations under the License.
  */
 
+ /* eslint-disable no-console */
+
 const { resolveContent } = require('./lib/resolve-content.js');
+const { render } = require('./lib/render.js');
 
 function main (params) {
   const context = {
-    path: params.__ow_path,
+    request : {
+      path: params.__ow_path,
+    },
+    response: {},
     content: {},
   };
 
-  return new Promise(function (resolve, reject) {
+  return new Promise(function (resolve) {
     resolveContent(context)
     .then(context => {
-      return resolve({body: context});
+      return render(context);
+    })
+    .then(context => {
+        return resolve(context.response);
     })
     .catch(e => {
       if(e.httpStatus) {
         return resolve({ status: e.httpStatus, body: e.message});
       } else {
-        return resolve({ status: 500, body: e});
+        return resolve({ status: 500, body: JSON.stringify(e, 2, null)});
       }
     })
   })
@@ -46,6 +55,7 @@ const shellExec= async (input) => {
 
 if (require.main === module) {
   shellExec({
+    // eslint-disable-next-line no-undef
     __ow_path: process.argv[2],
     __ow_method: 'get',
   });