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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
package backend
import (
"fmt"
"html/template"
"net/http"
"regexp"
"strings"
)
var (
regexIsHttp = regexp.MustCompile(`^https?://`)
)
type dataUsable interface {
SetData(*data)
}
type data struct {
title string
Article bool
Domain string
URL string
Image string
Description string
Name string
Links []Link
Logo *Logo
}
func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string, custom dataUsable) {
cfg := r.Context().Value("config").(*Config)
if d.Domain == "" {
d.Domain = cfg.Domain
}
if d.Name == "" {
d.Name = cfg.Name
}
if d.Description == "" {
d.Description = cfg.Description
}
if d.Links == nil {
d.Links = cfg.Links
}
if d.Logo == nil {
d.Logo = &cfg.Logo
}
if d.URL == "" {
if !strings.HasPrefix(r.URL.Path, "/") {
r.URL.Path = "/" + r.URL.Path
}
d.URL = r.URL.Path
}
t, err := template.New("").Funcs(template.FuncMap{
"static": func(path string) string {
if regexIsHttp.MatchString(path) {
return path
}
return fmt.Sprintf("/static/%s", path)
},
"assets": func(path string) string {
if regexIsHttp.MatchString(path) {
return path
}
return fmt.Sprintf("/assets/%s", path)
},
}).ParseFS(templates, fmt.Sprintf("templates/%s.html", name), "templates/base.html")
if err != nil {
panic(err)
}
if custom == nil {
err = t.ExecuteTemplate(w, "base.html", d)
} else {
custom.SetData(d)
err = t.ExecuteTemplate(w, "base.html", custom)
}
if err != nil {
panic(err)
}
}
func (d *data) Title() string {
title := d.Name
if d.Article {
title += " - log entry"
}
if len(d.title) != 0 {
title += " - " + d.title
}
return title
}
|