-
Notifications
You must be signed in to change notification settings - Fork 0
/
webgui.go
86 lines (75 loc) · 2.2 KB
/
webgui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
)
func webGui(dir string) {
mux := http.NewServeMux()
mux.HandleFunc("/events", streamEvents)
mux.HandleFunc("/command", postCommand)
mux.Handle("/", http.FileServer(http.Dir(dir)))
err := http.ListenAndServe(":8000", mux)
log.Fatal(err)
}
func streamEvents(w http.ResponseWriter, req *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "streaming not supported", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
status := make(map[string]json.RawMessage)
for {
status["initiate"], _ = json.Marshal(u.Initiate.Stat())
status["cycling"], _ = json.Marshal(cycle.Stat())
status["mp"] = u.Mp.State()
ftState := []json.RawMessage{u.Ft[0].State(), u.Ft[1].State(), u.Ft[2].State()}
status["ft"], _ = json.Marshal(ftState)
accState := [20]json.RawMessage{}
for i := range u.Accumulator {
accState[i] = u.Accumulator[i].State()
}
status["acc"], _ = json.Marshal(accState)
status["div"] = u.Divsr.State()
status["mult"] = u.Multiplier.State()
status["constant"], _ = json.Marshal(u.Constant.Stat())
message, _ := json.Marshal(status)
fmt.Fprintf(w, "data: %s\n\n", message)
time.Sleep(100 * time.Millisecond)
flusher.Flush()
}
}
type commandRequest struct {
Commands []string `json:"commands"`
}
type commandResponse struct {
Outputs []string `json:"outputs"`
}
func postCommand(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "Invalid HTTP method")
return
}
reqData := commandRequest{}
err := json.NewDecoder(req.Body).Decode(&reqData)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
respData := commandResponse{Outputs: make([]string, 0, len(reqData.Commands))}
for i := range reqData.Commands {
var buf bytes.Buffer
doCommand(&buf, reqData.Commands[i])
respData.Outputs = append(respData.Outputs, buf.String())
}
output, _ := json.Marshal(respData)
w.Write(output)
}