/
public.go
44 lines (37 loc) · 1.08 KB
/
public.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
package web
import (
"bytes"
"html/template"
"io"
"net/http"
"github.com/Sirupsen/logrus"
)
type Public struct {
logger *logrus.Entry
templates *template.Template
}
// NewPublic returns a new web instance.
func NewPublic(logger *logrus.Entry) (*Public, error) {
templates, err := template.ParseGlob("web/templates/public-*.tmpl")
if err != nil {
return nil, err
}
return &Public{
logger: logger,
templates: templates,
}, nil
}
func (p *Public) render(w http.ResponseWriter, logger *logrus.Entry, template string, data interface{}) {
buf := &bytes.Buffer{}
if err := p.templates.ExecuteTemplate(buf, template, data); err != nil {
logger.WithField("template", template).WithError(err).Error("could not execute template")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
io.Copy(w, buf)
}
// Home is the handler to view the console page.
func (p *Public) Home(w http.ResponseWriter, r *http.Request) {
logger := p.logger.WithField("requestURI", r.RequestURI)
p.render(w, logger, "public-home.tmpl", nil)
}