/
http.go
65 lines (55 loc) · 1.4 KB
/
http.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
package imgcanvas
import (
// embed
_ "embed"
"net/http"
"go.uber.org/zap"
"github.com/robbydyer/sports/internal/board"
)
//go:embed assets/loading.gif
var loading []byte
// GetHTTPHandlers ...
func (i *ImgCanvas) GetHTTPHandlers() ([]*board.HTTPHandler, error) {
enable := &board.HTTPHandler{
Path: "/api/imgcanvas/enable",
Handler: func(w http.ResponseWriter, req *http.Request) {
i.log.Info("enabling ImgCanvas")
i.Enable()
},
}
disable := &board.HTTPHandler{
Path: "/api/imgcanvas/disable",
Handler: func(w http.ResponseWriter, req *http.Request) {
i.log.Info("disabling ImgCanvas")
i.Disable()
},
}
render := &board.HTTPHandler{
Path: "/api/imgcanvas/board",
Handler: func(w http.ResponseWriter, req *http.Request) {
i.Enable()
i.log.Debug("getting image for web board")
if len(i.lastPng) == 0 {
i.log.Warn("web board is not ready yet, loading")
w.Header().Set("Content-Type", "image/gif")
if _, err := w.Write(loading); err != nil {
i.log.Error("failed to copy loading.gif", zap.Error(err))
}
return
}
w.Header().Set("Content-Type", "image/png")
i.Lock()
defer i.Unlock()
if _, err := w.Write(i.lastPng); err != nil {
i.log.Error("failed to copy png for /api/imgcanvas/board", zap.Error(err))
return
}
i.log.Debug("web board image sent")
},
}
return []*board.HTTPHandler{
enable,
disable,
render,
}, nil
}