-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtemplates.go
71 lines (57 loc) · 1.3 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
package main
import (
"embed"
"fmt"
"html/template"
"strings"
)
//go:embed templates/*
var templateFS embed.FS
var (
indexTpl, protectedTpl, errorTpl *template.Template
templateFuncMap = template.FuncMap{
"stringsJoin": strings.Join,
"stringsEqualFold": strings.EqualFold,
"isStringInSlice": isStringInSlice,
}
)
type indexTplData struct {
Title, Description, RawToken string
Error string
LoggedIn bool
Claims tplClaims
Groups []string
AuthorizeCodeURL string
}
type tplClaims struct {
IDToken Claims
UserInfo Claims
}
type protectedTplData struct {
Title, Description string
Vars struct {
Type, Value, ProtectedSecret string
}
Claims Claims
}
type errorTplData struct {
Title, Description string
Error, ErrorDescription, ErrorURI, State string
}
func init() {
indexTpl = templateMustLoadAndParse("index")
protectedTpl = templateMustLoadAndParse("protected")
errorTpl = templateMustLoadAndParse("error")
}
func templateMustLoadAndParse(name string) *template.Template {
if data, err := templateFS.ReadFile(fmt.Sprintf("templates/%s.tpl", name)); err != nil {
panic(err)
} else {
t, err := template.New(name).Funcs(templateFuncMap).Parse(string(data))
if err != nil {
panic(err)
}
return t
}
return nil
}