-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
47 lines (41 loc) · 879 Bytes
/
api.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
package main
import (
"image"
"image/jpeg"
"io"
"io/ioutil"
"log"
"net/http"
"strconv"
"time"
"github.com/nsmith5/mjpeg"
)
func (m *Model) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
handler := mjpeg.Handler{
Next: func() (image.Image, error) {
time.Sleep(100 * time.Millisecond)
return m.Image(), nil
},
Options: &jpeg.Options{Quality: 100},
}
handler.ServeHTTP(w, r)
case "POST":
b, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(400)
io.WriteString(w, "Failed to read body")
return
}
beta, err := strconv.ParseFloat(string(b), 64)
if err != nil {
w.WriteHeader(400)
io.WriteString(w, "Failed to parse float")
return
}
m.Beta = beta
log.Println("Beta modified to", m.Beta)
}
w.Header().Set("Access-Control-Allow-Origin", "https://www.nfsmith.ca")
}