-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
54 lines (44 loc) · 1.04 KB
/
web.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
package main
import (
_ "embed"
"html/template"
"net/http"
"github.com/sirupsen/logrus"
)
var (
//go:embed index.html.tmpl
indexData string
indexTmpl = template.Must(template.New("index.html").Parse(indexData))
)
type index struct {
l logrus.FieldLogger
streams []tmplStream
}
func newIndex(l logrus.FieldLogger, streams []configStream) *index {
var ts []tmplStream
for _, s := range streams {
ts = append(ts, tmplStream{
JSID: template.JS(s.ID),
HLSURL: template.JSStr("/m3u8?stream=" + s.ID),
ICYURL: template.JSStr("/icecast?stream=" + s.ID),
Name: s.Name,
})
}
return &index{l: l, streams: ts}
}
type tmplStream struct {
JSID template.JS
HLSURL template.JSStr
ICYURL template.JSStr
Name string
}
func (i *index) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
data := map[string]interface{}{
"Streams": i.streams,
}
if err := indexTmpl.Execute(w, data); err != nil {
i.l.WithError(err).Error("executing template")
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}
}