Serve both the controller and the display on the same endpoint
This commit is contained in:
@@ -4,8 +4,8 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>The Fancy N</title>
|
<title>The Fancy N</title>
|
||||||
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab&display=swap" rel="stylesheet">
|
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab&display=swap" rel="stylesheet">
|
||||||
<link rel="stylesheet" href="./main.css">
|
<link rel="stylesheet" href="/controller/main.css">
|
||||||
<script src="controller.js"></script>
|
<script src="/controller/controller.js"></script>
|
||||||
<meta name="viewport" content="width=300px, initial-scale=1">
|
<meta name="viewport" content="width=300px, initial-scale=1">
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ document.addEventListener("touchmove", (e) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function sendNudge(x, y) {
|
function sendNudge(x, y) {
|
||||||
fetch("/", {
|
fetch("", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {"Content-Type": "application/json"},
|
headers: {"Content-Type": "application/json"},
|
||||||
referrer: "no-referrer",
|
referrer: "no-referrer",
|
||||||
|
|||||||
@@ -33,70 +33,60 @@ func main() {
|
|||||||
var clients clientsStore
|
var clients clientsStore
|
||||||
clients.set = make(map[*client]struct{})
|
clients.set = make(map[*client]struct{})
|
||||||
|
|
||||||
go func() {
|
mux := http.NewServeMux()
|
||||||
mux := http.NewServeMux()
|
mux.HandleFunc("/controller", func(w http.ResponseWriter, r *http.Request) {
|
||||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
switch r.Method {
|
||||||
w.Header().Add("Access-Control-Allow-Origin", "*")
|
case "POST":
|
||||||
w.Header().Add("Access-Control-Allow-Headers", "*")
|
var nudge [2]int
|
||||||
w.Header().Add("Access-Control-Allow-Methods", "*")
|
switch r.Header.Get("Content-Type") {
|
||||||
switch r.Method {
|
case "application/json":
|
||||||
case "OPTIONS":
|
var decoder = json.NewDecoder(r.Body)
|
||||||
w.WriteHeader(http.StatusNoContent)
|
if err := decoder.Decode(&nudge); err != nil {
|
||||||
return
|
log.Println(err)
|
||||||
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:
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
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:
|
default:
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
})
|
(func() {
|
||||||
mux.HandleFunc("/main.css", func(w http.ResponseWriter, r *http.Request) {
|
clients.RLock()
|
||||||
w.Header().Add("Content-Type", "text/css")
|
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)
|
w.WriteHeader(http.StatusOK)
|
||||||
file, _ := os.Open("./display/main.css")
|
file, _ := os.Open("./controller/controller.html")
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
io.Copy(w, file)
|
io.Copy(w, file)
|
||||||
})
|
default:
|
||||||
mux.HandleFunc("/controller.js", func(w http.ResponseWriter, r *http.Request) {
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
w.Header().Add("Content-Type", "application/javascript")
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
})
|
||||||
file, _ := os.Open("./controller/controller.js")
|
mux.HandleFunc("/controller/main.css", func(w http.ResponseWriter, r *http.Request) {
|
||||||
defer file.Close()
|
w.Header().Add("Content-Type", "text/css")
|
||||||
io.Copy(w, file)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
file, _ := os.Open("./display/main.css")
|
||||||
log.Println(http.ListenAndServe(":8080", mux))
|
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.Handle("/", http.FileServer(http.Dir("./display/")))
|
||||||
mux.HandleFunc("/socket", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/socket", func(w http.ResponseWriter, r *http.Request) {
|
||||||
con, err := upgrader.Upgrade(w, r, nil)
|
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))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user