Serve everything from the binary instead of Nginx. Missing TLS, but that may not be a requirement at all
This commit is contained in:
parent
7eb75541d3
commit
81a7e5cb22
|
@ -21,7 +21,12 @@ func coils(w http.ResponseWriter, r *http.Request) {
|
||||||
func listen() {
|
func listen() {
|
||||||
log.Println("Starting pingvinAPI...")
|
log.Println("Starting pingvinAPI...")
|
||||||
http.HandleFunc("/api/v1/coils/", coils)
|
http.HandleFunc("/api/v1/coils/", coils)
|
||||||
log.Fatal(http.ListenAndServe(":8888", nil))
|
static := http.FileServer(http.Dir("./static/html"))
|
||||||
|
http.Handle("/", static)
|
||||||
|
err := http.ListenAndServe(":8888", nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
../index.html
|
|
@ -0,0 +1,17 @@
|
||||||
|
.addr {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
.val {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
#data {
|
||||||
|
padding: 2pt;
|
||||||
|
border-collapse: collapse;
|
||||||
|
}
|
||||||
|
thead {
|
||||||
|
border-bottom: 1px solid;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
td {
|
||||||
|
padding: 2pt;
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<link rel="stylesheet" href="/css/tabledata.css">
|
||||||
|
<script src="/js/tabledata.js"></script>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title id="title">Enervent Pingvin Kotilämpö</title>
|
||||||
|
</head>
|
||||||
|
<body onload="getData()">
|
||||||
|
<table id="data">
|
||||||
|
<caption>Coil values at <span id="time"></span></caption>
|
||||||
|
<thead><th>Address</th><th>Value</th><th>Symbol</th><th>Description</th></thead>
|
||||||
|
<tbody id="datatable"></tbody>
|
||||||
|
</table>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -0,0 +1,54 @@
|
||||||
|
function zeroPad(number) {
|
||||||
|
return ("0" + number).slice(-2)
|
||||||
|
}
|
||||||
|
function getData() {
|
||||||
|
now = new Date()
|
||||||
|
Y = now.getFullYear()
|
||||||
|
m = now.getMonth()
|
||||||
|
d = now.getDate()
|
||||||
|
H = zeroPad(now.getHours())
|
||||||
|
M = zeroPad(now.getMinutes())
|
||||||
|
S = zeroPad(now.getSeconds())
|
||||||
|
document.getElementById('time').innerHTML = `${Y}-${m}-${d} ${H}:${M}:${S}`
|
||||||
|
|
||||||
|
error = false
|
||||||
|
// The same index.html is used for both coil and register data,
|
||||||
|
// change api url based on which we're looking at
|
||||||
|
if (document.location.pathname == "/coils/") {
|
||||||
|
url = "/api/v1/coils"
|
||||||
|
document.getElementById("title").innerHTML = "Coils | Enervent Pingvin Kotilämpö"
|
||||||
|
}
|
||||||
|
else if (document.location.pathname == "/registers/") {
|
||||||
|
url = "/api/v1/registers"
|
||||||
|
document.getElementById("title").innerHTML = "Registers | Enervent Pingvin Kotilämpö"
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
document.getElementById("data").innerHTML = 'Page not found'
|
||||||
|
error = true
|
||||||
|
}
|
||||||
|
if (!error) {
|
||||||
|
// Fetch data from API
|
||||||
|
fetch(url)
|
||||||
|
.then((response) => {
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Error fetching data: ${response.status}`)
|
||||||
|
}
|
||||||
|
return response.json()
|
||||||
|
})
|
||||||
|
.then((data) => {
|
||||||
|
// Populate table
|
||||||
|
document.getElementById('datatable').innerHTML = "";
|
||||||
|
for (n=0; n<data.length; n++) {
|
||||||
|
tablerow = `<tr><td class="addr" id="addr_${data[n].address}">${data[n].address}</td>\
|
||||||
|
<td class ="val" id="value_${data[n].address}">${Number(data[n].value)}</td>\
|
||||||
|
<td class="symbol" id="symbol_${data[n].address}">${data[n].symbol}</td>\
|
||||||
|
<td class="desc" id="description_${data[n].address}">${data[n].description}</td></tr>`
|
||||||
|
document.getElementById('datatable').innerHTML += tablerow
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Using setTimeout instead of setInterval to avoid possible connection issues
|
||||||
|
// There's no need to update exactly every 5 seconds, the skew is fine
|
||||||
|
setTimeout(getData, 1*1000);
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
../index.html
|
Loading…
Reference in New Issue