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:19 UTC
[sling-whiteboard] branch master updated (2262ef0 -> 14c7116)
This is an automated email from the ASF dual-hosted git repository.
bdelacretaz pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git.
from 2262ef0 skeleton
new 7a12dfe Improve install script
new 7e3fa1f Stub content resolver
new 03b2245 Stub rendering function
new 14c7116 Read content from filesystem
The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../{eslintrc.js => .eslintrc.json} | 10 ++--
.../content/demo/docs/somedoc.json | 5 ++
serverless-microsling/content/demo/index.json | 5 ++
serverless-microsling/install | 21 +++++--
serverless-microsling/lib/render.js | 42 ++++++++++++++
serverless-microsling/lib/resolve-content.js | 42 ++++++++++++++
serverless-microsling/microsling.js | 66 +++++++++++++++++++++-
7 files changed, 178 insertions(+), 13 deletions(-)
copy serverless-microsling/{eslintrc.js => .eslintrc.json} (70%)
create mode 100644 serverless-microsling/content/demo/docs/somedoc.json
create mode 100644 serverless-microsling/content/demo/index.json
create mode 100644 serverless-microsling/lib/render.js
create mode 100644 serverless-microsling/lib/resolve-content.js
[sling-whiteboard] 02/04: Stub content resolver
Posted by bd...@apache.org.
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
[sling-whiteboard] 01/04: Improve install script
Posted by bd...@apache.org.
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 7a12dfebbbe01a8aeedc3a32ff37bbb33f55a3af
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Wed Jul 24 14:05:34 2019 +0200
Improve install script
---
serverless-microsling/install | 21 +++++++++++++++------
serverless-microsling/microsling.js | 14 +++++++++++++-
2 files changed, 28 insertions(+), 7 deletions(-)
diff --git a/serverless-microsling/install b/serverless-microsling/install
index a9ec57a..cc7a6ae 100755
--- a/serverless-microsling/install
+++ b/serverless-microsling/install
@@ -1,11 +1,20 @@
#!/bin/bash
-echo installing the microsling code using 'wsk'...
+function fatal() {
+ echo $* >&2
+ exit 1
+}
-# TODO check for npm install
+export ACTION=microsling
+export ZIP=openwhisk_action.zip
+export ACTION_FILES="package.json node_modules *.js foo"
-zip -r openwhisk_action.zip package.json node_modules *.js \
-&& wsk action update microsling openwhisk_action.zip \
---web true --kind nodejs:10
+[[ -d node_modules ]] || fatal "node_modules folder not found, please run 'npm install'"
-echo "Microsling is available at at $(wsk -i action get microsling --url | grep http)"
\ No newline at end of file
+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
+
+echo "Microsling is available at at $(wsk -i action get $ACTION --url | grep http)"
\ No newline at end of file
diff --git a/serverless-microsling/microsling.js b/serverless-microsling/microsling.js
index 2a3df46..c928a1f 100644
--- a/serverless-microsling/microsling.js
+++ b/serverless-microsling/microsling.js
@@ -1,7 +1,19 @@
function main (params) {
+ const result = {
+ body: `${params.__ow_path}`,
+ };
+
return new Promise(function (resolve, reject) {
- return resolve({ body:"Ce texte-ci\n"});
+ return resolve(result);
})
}
+if (require.main === module) {
+ const result = main({
+ __ow_path: process.argv[2],
+ __ow_method: 'get',
+ });
+ console.log(result);
+}
+
module.exports.main = main
\ No newline at end of file
[sling-whiteboard] 03/04: Stub rendering function
Posted by bd...@apache.org.
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',
});
[sling-whiteboard] 04/04: Read content from filesystem
Posted by bd...@apache.org.
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
});
}