add static/index.html

This commit is contained in:
Luna Mendes 2018-06-11 18:36:16 -03:00
parent 19253502c8
commit 30da8f31b5
2 changed files with 73 additions and 1 deletions

View file

@ -30,7 +30,7 @@ defmodule Elstat.Cowboy do
def build_dispatch_config do def build_dispatch_config do
:cowboy_router.compile([ :cowboy_router.compile([
{:_, [ {:_, [
{"/", :cowboy_static, {:priv_file, :my_app, "static/index.html"}}, {"/", :cowboy_static, {:file, "static/index.html"}},
{"/hewwo", Elstat.Cowboy.DefaultHandler, []}, {"/hewwo", Elstat.Cowboy.DefaultHandler, []},
{"/api/current_status", Elstat.API.Status, []}, {"/api/current_status", Elstat.API.Status, []},

72
static/index.html Normal file
View file

@ -0,0 +1,72 @@
<html>
<head>
<style>
p {
font-size: 15pt;
}
</style>
<script>
// https://stackoverflow.com/a/4033310
function httpGetAsync(theUrl, callback)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
function genText(data) {
base = `${data.description} - status: ${data.status}`;
if('latency' in data) {
base += ` - latency: ${data.latency} ms`;
}
return base;
}
function curStatHandler(text) {
let payload = JSON.parse(text);
for(const service in payload) {
let data = payload[service];
let elementId = `${service}-description`;
let existingElement = document.getElementById(elementId);
if (existingElement === null) {
let element = document.createElement('p');
element.id = elementId;
element.innerText = genText(data);
document.body.appendChild(element);
} else {
existingElement.innerText = genText(data);
}
}
}
function fetchStatus () {
httpGetAsync("http://127.0.0.1:8069/api/current_status", curStatHandler);
}
window.onload = function () {
fetchStatus();
setInterval(() => {
fetchStatus();
}, 3000)
}
</script>
</head>
<body>
</body>
</html>