-
Notifications
You must be signed in to change notification settings - Fork 3
/
templates.go
46 lines (39 loc) · 973 Bytes
/
templates.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
package main
import (
"mime"
"net/http"
"path/filepath"
"text/template"
)
const (
borderSize = 30
spacing = 10
pixelSize = 256
)
func TemplateData() interface{} {
return map[string]interface{}{
"NumPixelsPerRow": options.NumPixelsPerRow,
"Spacing": spacing,
"PixelSize": pixelSize,
"BorderSize": borderSize,
"TotalWidth": (pixelSize + spacing + 2*borderSize) * options.NumPixelsPerRow,
}
}
type templateRenderer struct {
Dir string
Data interface{}
}
func (tr templateRenderer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
contentType := mime.TypeByExtension(filepath.Ext(r.URL.Path))
if contentType == "" {
contentType = "application/octet-stream"
}
path := filepath.Join(tr.Dir, r.URL.Path) + ".tpl"
tpl, err := template.ParseFiles(path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", contentType)
tpl.Execute(w, tr.Data)
}