/
page.go
98 lines (86 loc) · 2.25 KB
/
page.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package pages
import (
"html/template"
"net/http"
"path/filepath"
sprig "github.com/Masterminds/sprig/v3"
"github.com/go-logr/logr"
"github.com/gardener/test-infra/pkg/tm-bot/tests"
"github.com/gardener/test-infra/pkg/tm-bot/ui/auth"
"github.com/gardener/test-infra/pkg/version"
)
type Page struct {
basePath string
log logr.Logger
auth auth.Provider
runs *tests.Runs
}
type globalSettings struct {
DisplayLogin bool
Authenticated bool
URL string
User user
}
type baseTemplateSettings struct {
globalSettings
PageName string
Arguments interface{}
}
type user struct {
Name string
}
func New(logger logr.Logger, runs *tests.Runs, auth auth.Provider, basePath string) *Page {
return &Page{
basePath: basePath,
log: logger,
auth: auth,
runs: runs,
}
}
func (p *Page) handleSimplePage(templateName string, param interface{}) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
isAuthenticated := true
aCtx, err := p.auth.GetAuthContext(r)
if err != nil {
p.log.V(3).Info(err.Error())
isAuthenticated = false
}
global := globalSettings{
DisplayLogin: p.auth.DisplayLogin(),
Authenticated: isAuthenticated,
User: user{
Name: aCtx.User,
},
URL: r.URL.String(),
}
base := filepath.Join(p.basePath, "templates", "base.html")
components := filepath.Join(p.basePath, "templates", "components/*")
fp := filepath.Join(p.basePath, "templates", templateName)
tmpl := template.New(templateName)
tmpl.Funcs(map[string]interface{}{
"settings": makeBaseTemplateSettings(global),
"urlAddParams": addURLParams,
"version": func() string { return version.Get().String() },
})
tmpl.Funcs(sprig.FuncMap())
tmpl, err = tmpl.ParseGlob(components)
if err != nil {
p.log.Error(err, "unable to parse components")
return
}
tmpl, err = tmpl.ParseFiles(base, fp)
if err != nil {
p.log.Error(err, "unable to parse files")
return
}
if err := tmpl.Execute(w, map[string]interface{}{
"global": global,
"page": param,
}); err != nil {
p.log.Error(err, "unable to execute template")
}
}
}