From 5f3ae55e51d2518019ec33ddd81b644c14fdf312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20K=C3=A1n=C4=9B?= Date: Tue, 4 Jun 2019 21:11:53 +0200 Subject: [PATCH] Initial commit --- display.html | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 29 ++++++++++++++++++++ main.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 display.html create mode 100644 index.html create mode 100644 main.go diff --git a/display.html b/display.html new file mode 100644 index 0000000..c780817 --- /dev/null +++ b/display.html @@ -0,0 +1,73 @@ + + + + + Makerfaire Pátek + + + + +
+
+
+
+
+
+
+
+
+ + diff --git a/index.html b/index.html new file mode 100644 index 0000000..7ff307c --- /dev/null +++ b/index.html @@ -0,0 +1,29 @@ + + + + + Makerfaire Pátek + + + +

Makerfaire

+
+ + +
+ + diff --git a/main.go b/main.go new file mode 100644 index 0000000..3c4e810 --- /dev/null +++ b/main.go @@ -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 + } + } +}