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

[sling-whiteboard] 02/04: Stub content resolver

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

    Stub content resolver
---
 serverless-microsling/install                |  2 +-
 serverless-microsling/lib/resolve-content.js | 35 ++++++++++++++++++++++
 serverless-microsling/microsling.js          | 45 ++++++++++++++++++++++++----
 3 files changed, 76 insertions(+), 6 deletions(-)

diff --git a/serverless-microsling/install b/serverless-microsling/install
index cc7a6ae..2932f69 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 foo"
+export ACTION_FILES="package.json node_modules *.js lib/* foo"
 
 [[ -d node_modules ]] || fatal "node_modules folder not found, please run 'npm install'"
 
diff --git a/serverless-microsling/lib/resolve-content.js b/serverless-microsling/lib/resolve-content.js
new file mode 100644
index 0000000..2c9c855
--- /dev/null
+++ b/serverless-microsling/lib/resolve-content.js
@@ -0,0 +1,35 @@
+/* 
+ * Copyright 2019 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * 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"
+    }
+  }
+  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 c928a1f..f125378 100644
--- a/serverless-microsling/microsling.js
+++ b/serverless-microsling/microsling.js
@@ -1,19 +1,54 @@
+/* 
+ * Copyright 2019 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+const { resolveContent } = require('./lib/resolve-content.js');
+
 function main (params) {
-  const result = {
-    body: `${params.__ow_path}`,
+  const context = {
+    path: params.__ow_path,
+    content: {},
   };
 
   return new Promise(function (resolve, reject) {
-    return resolve(result);
+    resolveContent(context)
+    .then(context => {
+      return resolve({body: context});
+    })
+    .catch(e => {
+      if(e.httpStatus) {
+        return resolve({ status: e.httpStatus, body: e.message});
+      } else {
+        return resolve({ status: 500, body: e});
+      }
+    })
   })
 }
 
+const shellExec= async (input) => {
+  main(input)
+  .then(result => {
+    console.log(JSON.stringify(result, 2, null));
+  });
+};
+
 if (require.main === module) {
-  const result = main({
+  shellExec({
     __ow_path: process.argv[2],
     __ow_method: 'get',
   });
-  console.log(result);
 }
 
 module.exports.main = main
\ No newline at end of file