Skip to content

Instantly share code, notes, and snippets.

@mohammed-ali-1
Last active March 14, 2019 12:57
Show Gist options
  • Save mohammed-ali-1/b372ba940d7f8816e327a55d9448efc5 to your computer and use it in GitHub Desktop.
Save mohammed-ali-1/b372ba940d7f8816e327a55d9448efc5 to your computer and use it in GitHub Desktop.
OpenWhisk web action that returns a HTML page
function main({reading}) {
var script = `
<script>
function latest_readings() {
const Http = new XMLHttpRequest();
//URL to getLatestTenReadings web action
const url='https://OPENWHISK_HOST:OPENWHISK_PORT/api/v1/web/guest/default/getLatestTenReadings';
Http.open("GET", url);
Http.send();
Http.onreadystatechange=(e)=>
{
var obj = JSON.parse(Http.responseText);
var i = 9;
document.getElementById("readings").innerHTML = "";
while (i > 4) {
document.getElementById("readings").innerHTML += obj[i].body;
document.getElementById("readings").innerHTML += "<br />";
i--;
}
}
}
latest_readings();
setInterval(latest_readings,10000);
</script>`;
return {
body: `
<html>
<style>
#readings {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border-style: solid;
border-width: 2px;
}
</style>
<body>
<h3 id="readings"></h3>
${script}
</body>
</html>`
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment