This repository has been archived by the owner on May 19, 2020. It is now read-only.
/
templates.go
82 lines (73 loc) · 2.53 KB
/
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
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
package helpers
import (
"fmt"
"html/template"
"io"
"path/filepath"
)
const (
// InviteEmailTemplate is the template key for the invite email.
InviteEmailTemplate = "INVITE_EMAIL_TEMPLATE"
// IndexTemplate is the template key for the index.html.
IndexTemplate = "INDEX_HTML_TEMPLATE"
)
// findTemplates will try to construct to final path of where to find templates
// given the basePath of where to look.
func findTemplates(basePath string) map[string][]string {
return map[string][]string{
IndexTemplate: {filepath.Join(basePath, "web", "index.html")},
InviteEmailTemplate: {filepath.Join(basePath, "mail", "invite.html")},
}
}
// Templates serve as a mapping to various templates.
// Each entry can be a compilation of multiple files mapped to a string entry.
// This works if we ever want to use the .define blocks which are good for
// creating a main template with swappable content.
// Similar to https://hackernoon.com/golang-template-2-template-composition-and-how-to-organize-template-files-4cb40bcdf8f6
type Templates struct {
templates map[string]*template.Template
}
// InitTemplates will try to parse the templates.
func InitTemplates(basePath string) (*Templates, error) {
templates := make(map[string]*template.Template)
for templateName, templatePath := range findTemplates(basePath) {
tpl, err := template.ParseFiles(templatePath...)
if err != nil {
return nil, err
}
templates[templateName] = tpl
}
return &Templates{templates}, nil
}
func (t *Templates) getTemplate(templateKey string) (*template.Template, error) {
if template, ok := t.templates[templateKey]; ok {
return template, nil
}
return nil, fmt.Errorf("unable to find template with key %s", templateKey)
}
// inviteEmail provides struct for the templates/mail/invite.tmpl
type inviteEmail struct {
URL string
}
// GetInviteEmail gets the filled in invite email template.
func (t *Templates) GetInviteEmail(rw io.Writer, url string) error {
tpl, err := t.getTemplate(InviteEmailTemplate)
if err != nil {
return err
}
return tpl.Execute(rw, inviteEmail{url})
}
// GetIndex gets the filled in index.html
func (t *Templates) GetIndex(rw io.Writer, csrfToken, gaTrackingID, newRelicID,
newRelicBrowserLicenseKey string) error {
tpl, err := t.getTemplate(IndexTemplate)
if err != nil {
return err
}
return tpl.Execute(rw, map[string]interface{}{
"csrfToken": csrfToken,
"GA_TRACKING_ID": gaTrackingID,
"NEW_RELIC_ID": newRelicID,
"NEW_RELIC_BROWSER_LICENSE_KEY": newRelicBrowserLicenseKey,
})
}