-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.go
62 lines (54 loc) · 1.23 KB
/
frontend.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
package main
import (
"bytes"
log "github.com/sirupsen/logrus"
"html/template"
"net/http"
"sync"
)
var frontend *Content
// Content encapsulates a RWMutex, a template and page bytes
type Content struct {
sync.RWMutex
tpl *template.Template
page []byte
}
// NewContent loads a given template file and returns a content container
func NewContent(path string) *Content {
tpl, err := template.ParseFiles(path)
if err != nil {
log.WithField("Template", path).WithError(err).Fatalf("Unable to process template")
}
return &Content{
tpl: tpl,
}
}
// Get returns the content container's page data
func (c *Content) Get() []byte {
c.RLock()
defer c.RUnlock()
return c.page
}
// Render updates the content container's page data by rendering its template
func (c *Content) Render(sr []SiteResult) {
c.Lock()
data := struct {
Sites []SiteResult
}{
Sites: sr,
}
var b bytes.Buffer
err := c.tpl.Execute(&b, data)
if err != nil {
log.WithError(err).Error("Template error")
}
c.page = b.Bytes()
c.Unlock()
}
// CoronaCountServer returns the frontend's page bytes
func CoronaCountServer(w http.ResponseWriter, r *http.Request) {
_, err := w.Write(frontend.Get())
if err != nil {
log.WithError(err).Warn("Writing response")
}
}