Initial commit

This commit is contained in:
Vojtěch Káně 2019-06-04 21:11:53 +02:00
commit 5f3ae55e51
3 changed files with 176 additions and 0 deletions

73
display.html Normal file
View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="cs">
<head>
<meta charset="UTF-8">
<title>Makerfaire Pátek</title>
<style>
body {
background-image: linear-gradient(to left, #ff1546 90%, transparent 100%);
height: 100vh;
margin: 0;
padding: 1rem;
display: flex;
align-items: center;
justify-content: space-between;
}
#bar {
z-index: -1;
position: absolute;
left:0;
min-width: 50%;
top:0;
bottom: 0;
background-image: linear-gradient(to right, #1aff45 90%, transparent 100%);
transition: min-width 2s;
}
.perc {
font-size: 5rem;
}
.perc::after {
content: '%';
}
.val {
font-size: 2rem;
}
</style>
<script>
document.addEventListener("DOMContentLoaded", () => {
setInterval(() => {
fetch("/stats").then((response) => {
response.json().then((json) => {
let n1 = Number(json[0]);
let n2 = Number(json[1]);
let v1 = document.getElementById("1");
let v1f = n1 / (n1 + n2);
v1.getElementsByClassName("val")[0].innerText = n1.toString();
v1.getElementsByClassName("perc")[0].innerText = (n1 === 0 && n2 === 0) ? "50" : (n1 === 0 ? "0" : Number(v1f * 100).toFixed(0));
let v2 = document.getElementById("2");
let v2f = n2 / (n1 + n2);
v2.getElementsByClassName("val")[0].innerText = n2.toString();
v2.getElementsByClassName("perc")[0].innerText = (n1 === 0 && n2 === 0) ? "50" : (n2 === 0 ? "0" : Number(v2f * 100).toFixed(0));
document.getElementById("bar").style.minWidth = v1f*100 + "%";
});
});
}, 500)
})
</script>
</head>
<body>
<div id="1" class="votes">
<div class="perc"></div>
<div class="val"></div>
</div>
<div id="2" class="votes">
<div class="perc"></div>
<div class="val"></div>
</div>
<div id="bar"></div>
</body>
</html>

29
index.html Normal file
View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="cs">
<head>
<meta charset="UTF-8">
<title>Makerfaire Pátek</title>
<style>
body {
background-color: blueviolet;
}
#buttons {
display: flex;
justify-content: space-around;
}
#buttons button {
min-width: 25vw;
min-height: 35vh;
border: none;
}
</style>
</head>
<body>
<h1>Makerfaire</h1>
<div id="buttons">
<a href="/vote?key=0"><button style="background-color: green"></button></a>
<a href="/vote?key=1"><button style="background-color: red"></button></a>
</div>
</body>
</html>

74
main.go Normal file
View File

@ -0,0 +1,74 @@
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
)
type vote struct {
item byte
weight int
}
var homeHtml []byte
var displayHtml []byte
var stats = make([]int, 2)
func main() {
var err error
homeHtml, err = ioutil.ReadFile("index.html")
if err != nil {
panic(err)
}
displayHtml, err = ioutil.ReadFile("display.html")
if err != nil {
panic(err)
}
var manager = make(chan vote)
go manage(manager)
go func() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(displayHtml)
})
mux.HandleFunc("/stats", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(stats)
})
http.ListenAndServe("127.0.0.1:8081", mux)
}()
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(homeHtml)
})
mux.HandleFunc("/vote", func(w http.ResponseWriter, r *http.Request) {
var key = r.URL.Query().Get("key")
switch key {
case "0":
manager <- vote{item: 0, weight: 1}
case "1":
manager <- vote{item: 1, weight: 1}
default:
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusNoContent)
})
http.ListenAndServe(":8080", mux)
}
func manage(ch chan vote) {
for v := range ch {
if int(v.item) < len(stats) {
stats[v.item] += v.weight
}
}
}