Serve both the controller and the display on the same endpoint
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user