-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.go
274 lines (226 loc) · 6.89 KB
/
renderer.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package handler
import (
"bytes"
"errors"
"fmt"
"html/template"
"io/fs"
"net/http"
"time"
"github.com/polyscone/tofu/app"
"github.com/polyscone/tofu/app/account"
"github.com/polyscone/tofu/app/system"
"github.com/polyscone/tofu/errsx"
"github.com/polyscone/tofu/httpx"
"github.com/polyscone/tofu/web/guard"
"github.com/polyscone/tofu/web/sess"
)
type State struct {
data map[string]any
}
func (s *State) Get(key string) any {
return s.data[key]
}
func (s *State) Set(key string, value any) {
if s.data == nil {
s.data = make(map[string]any)
}
s.data[key] = value
}
func (s *State) Store(key string, value any) bool {
if s.data == nil {
s.data = make(map[string]any)
}
if _, ok := s.data[key]; ok {
return false
}
s.data[key] = value
return true
}
type ViewData struct {
View string
Status int
CSRF CSRF
ErrorMessage string
Errors errsx.Map
Now time.Time
Form Form
URL URL
App AppData
Session SessionData
Config *system.Config
User *account.User
Passport guard.Passport
Props map[string]any
State *State
Vars Vars
}
func (v ViewData) WithProps(pairs ...any) (ViewData, error) {
if len(pairs)%2 == 1 {
return v, errors.New("WithProps: want key value pairs")
}
v.Props = make(map[string]any, len(pairs)/2)
for i := 0; i < len(pairs); i += 2 {
key := fmt.Sprintf("%v", pairs[i])
value := pairs[i+1]
if key == "Props" {
props, ok := value.(map[string]any)
if ok {
for key, value := range props {
v.Props[key] = value
}
continue
}
}
v.Props[key] = value
}
return v, nil
}
type ViewDataFunc func(data *ViewData)
type ViewVarsFunc func(r *http.Request) (Vars, error)
type TemplatePathsFunc func(view string) []string
type TemplateProcessFunc func(w http.ResponseWriter, r *http.Request, template *bytes.Buffer) []byte
type Renderer struct {
h *Handler
templateFiles fs.FS
templatePaths TemplatePathsFunc
funcs template.FuncMap
viewVarsFuncs map[string]ViewVarsFunc
process TemplateProcessFunc
}
func NewRenderer(h *Handler, templateFiles fs.FS, templatePaths TemplatePathsFunc, funcs template.FuncMap, process TemplateProcessFunc) *Renderer {
return &Renderer{
h: h,
templateFiles: templateFiles,
templatePaths: templatePaths,
funcs: funcs,
viewVarsFuncs: make(map[string]ViewVarsFunc),
process: process,
}
}
func (rn *Renderer) ViewFunc(w http.ResponseWriter, r *http.Request, status int, view string, dataFunc ViewDataFunc) {
ctx := r.Context()
config := rn.h.Config(ctx)
user := rn.h.User(ctx)
passport := rn.h.Passport(ctx)
data := ViewData{
View: view,
Status: status,
CSRF: CSRF{Ctx: ctx},
Now: time.Now(),
Form: Form{Values: r.PostForm},
URL: URL{
Scheme: rn.h.Tenant.Scheme,
Host: rn.h.Tenant.Host,
Path: template.URL(r.URL.Path),
Query: Query{Values: r.URL.Query()},
},
App: AppData{
Name: app.Name,
ShortName: app.ShortName,
Description: app.Description,
ThemeColour: app.ThemeColour,
},
Session: SessionData{
// Global session keys
Flash: rn.h.Sessions.PopStrings(ctx, sess.Flash),
FlashImportant: rn.h.Sessions.PopStrings(ctx, sess.FlashImportant),
FlashError: rn.h.Sessions.PopStrings(ctx, sess.FlashError),
Redirect: rn.h.Sessions.GetString(ctx, sess.Redirect),
HighlightID: rn.h.Sessions.PopString(ctx, sess.HighlightID),
// Account session keys
UserID: rn.h.Sessions.GetString(ctx, sess.UserID),
Email: rn.h.Sessions.GetString(ctx, sess.Email),
TOTPMethod: rn.h.Sessions.GetString(ctx, sess.TOTPMethod),
HasActivatedTOTP: rn.h.Sessions.GetBool(ctx, sess.HasActivatedTOTP),
IsAwaitingTOTP: rn.h.Sessions.GetBool(ctx, sess.IsAwaitingTOTP),
IsSignedIn: rn.h.Sessions.GetBool(ctx, sess.IsSignedIn),
KnownPasswordBreachCount: rn.h.Sessions.GetInt(ctx, sess.KnownPasswordBreachCount),
},
Config: config,
User: user,
Passport: passport,
State: &State{},
}
if vars, ok := rn.viewVarsFuncs[view]; ok {
defaults, err := vars(r)
if err != nil {
rn.ErrorView(w, r, "vars", err, "site/error", nil)
return
}
data.Vars = data.Vars.Merge(defaults)
}
if dataFunc != nil {
dataFunc(&data)
}
// Make sure the current view name isn't overwritten by a user function
data.View = view
tmpl := rn.h.Template(rn.templateFiles, rn.templatePaths(view), rn.funcs, view)
var buf bytes.Buffer
if err := tmpl.ExecuteTemplate(&buf, "master", data); err != nil {
rn.h.Logger(ctx).Error("execute view template", "error", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if rn.process != nil {
b := rn.process(w, r, &buf)
if b != nil {
buf = *bytes.NewBuffer(b)
}
}
w.WriteHeader(status)
if _, err := buf.WriteTo(w); err != nil {
rn.h.Logger(ctx).Error("write view template response", "error", err)
}
}
func (rn *Renderer) View(w http.ResponseWriter, r *http.Request, status int, view string, vars Vars) {
rn.ViewFunc(w, r, status, view, func(data *ViewData) {
data.Vars = data.Vars.Merge(vars)
})
}
func (rn *Renderer) SetViewVars(name string, vars ViewVarsFunc) {
if _, ok := rn.viewVarsFuncs[name]; ok {
panic(fmt.Sprintf("default view vars already set for %q", name))
}
rn.viewVarsFuncs[name] = vars
}
func (rn *Renderer) HandlerFunc(view string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
rn.View(w, r, http.StatusOK, view, nil)
}
}
func (rn *Renderer) ErrorViewFunc(w http.ResponseWriter, r *http.Request, msg string, err error, view string, dataFunc ViewDataFunc) {
ctx := r.Context()
rn.h.Logger(ctx).Error(msg, "error", err)
status := httpx.ErrorStatus(err)
if status == http.StatusTooManyRequests {
// If a client is hitting a rate limit we set the connection header to
// close which will trigger the standard library's HTTP server to close
// the connection after the response is sent
//
// Doing this means the client needs to go through the handshake process
// again to make a new connection the next time, which should help to slow
// down additional requests for clients that keep on hitting the limit
w.Header().Set("connection", "close")
}
rn.ViewFunc(w, r, status, view, func(data *ViewData) {
data.ErrorMessage = httpx.ErrorMessage(err)
switch {
case errors.Is(err, app.ErrMalformedInput),
errors.Is(err, app.ErrInvalidInput),
errors.Is(err, app.ErrConflict):
var errs errsx.Map
if errors.As(err, &errs) {
data.Errors = errs
}
}
if dataFunc != nil {
dataFunc(data)
}
})
}
func (rn *Renderer) ErrorView(w http.ResponseWriter, r *http.Request, msg string, err error, view string, vars Vars) {
rn.ErrorViewFunc(w, r, msg, err, view, func(data *ViewData) {
data.Vars = data.Vars.Merge(vars)
})
}