Serve both the controller and the display on the same endpoint

This commit is contained in:
Vojtěch Káně
2026-10-05 16:07:47 +02:00
parent e3602ac25e
commit 7f3f3e487a
3 changed files with 49 additions and 59 deletions
+2 -2
View File
@@ -4,8 +4,8 @@
<meta charset="UTF-8">
<title>The Fancy N</title>
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab&display=swap" rel="stylesheet">
<link rel="stylesheet" href="./main.css">
<script src="controller.js"></script>
<link rel="stylesheet" href="/controller/main.css">
<script src="/controller/controller.js"></script>
<meta name="viewport" content="width=300px, initial-scale=1">
<style>
body {
+1 -1
View File
@@ -52,7 +52,7 @@ document.addEventListener("touchmove", (e) => {
});
function sendNudge(x, y) {
fetch("/", {
fetch("", {
method: "POST",
headers: {"Content-Type": "application/json"},
referrer: "no-referrer",
+46 -56
View File
@@ -33,70 +33,60 @@ func main() {
var clients clientsStore
clients.set = make(map[*client]struct{})
go func() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "*")
w.Header().Add("Access-Control-Allow-Methods", "*")
switch r.Method {
case "OPTIONS":
w.WriteHeader(http.StatusNoContent)
return
case "POST":
var nudge [2]int
switch r.Header.Get("Content-Type") {
case "application/json":
var decoder = json.NewDecoder(r.Body)
if err := decoder.Decode(&nudge); err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
default:
mux := http.NewServeMux()
mux.HandleFunc("/controller", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
var nudge [2]int
switch r.Header.Get("Content-Type") {
case "application/json":
var decoder = json.NewDecoder(r.Body)
if err := decoder.Decode(&nudge); err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
(func() {
clients.RLock()
defer clients.RUnlock()
for c, _ := range clients.set {
select {
case c.nudges <- nudge:
default:
log.Println("Client too slow -> dropping nudge")
}
}
})()
w.WriteHeader(http.StatusNoContent)
case "GET":
w.Header().Add("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
file, _ := os.Open("./controller/controller.html")
defer file.Close()
io.Copy(w, file)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
w.WriteHeader(http.StatusBadRequest)
return
}
})
mux.HandleFunc("/main.css", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/css")
(func() {
clients.RLock()
defer clients.RUnlock()
for c, _ := range clients.set {
select {
case c.nudges <- nudge:
default:
log.Println("Client too slow -> dropping nudge")
}
}
})()
w.WriteHeader(http.StatusNoContent)
case "GET":
w.Header().Add("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
file, _ := os.Open("./display/main.css")
file, _ := os.Open("./controller/controller.html")
defer file.Close()
io.Copy(w, file)
})
mux.HandleFunc("/controller.js", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/javascript")
w.WriteHeader(http.StatusOK)
file, _ := os.Open("./controller/controller.js")
defer file.Close()
io.Copy(w, file)
})
log.Println(http.ListenAndServe(":8080", mux))
}()
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
})
mux.HandleFunc("/controller/main.css", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/css")
w.WriteHeader(http.StatusOK)
file, _ := os.Open("./display/main.css")
defer file.Close()
io.Copy(w, file)
})
mux.HandleFunc("/controller/controller.js", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/javascript")
w.WriteHeader(http.StatusOK)
file, _ := os.Open("./controller/controller.js")
defer file.Close()
io.Copy(w, file)
})
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir("./display/")))
mux.HandleFunc("/socket", func(w http.ResponseWriter, r *http.Request) {
con, err := upgrader.Upgrade(w, r, nil)
@@ -130,5 +120,5 @@ func main() {
}
}
})
log.Println(http.ListenAndServe(":8081", mux))
log.Println(http.ListenAndServe(":8080", mux))
}