@@ -28,6 +28,9 @@ import (
28
28
//go:embed out/bin/*
29
29
var site embed.FS
30
30
31
+ // HTMLTemplateHandler is a function that defines how `htmlState` is populated
32
+ type HTMLTemplateHandler func (* http.Request ) HtmlState
33
+
31
34
// DefaultHandler returns an HTTP handler for serving the static site,
32
35
// based on the `embed.FS` compiled into the binary.
33
36
func DefaultHandler (logger slog.Logger ) http.Handler {
@@ -37,23 +40,21 @@ func DefaultHandler(logger slog.Logger) http.Handler {
37
40
panic (err )
38
41
}
39
42
40
- return Handler (filesystem , logger )
43
+ templateFunc := func (r * http.Request ) HtmlState {
44
+ return HtmlState {
45
+ // CSP nonce for the given request (if there is one present)
46
+ CSPNonce : secure .CSPNonce (r .Context ()),
47
+ // CSRF token for the given request
48
+ CSRFToken : nosurf .Token (r ),
49
+ }
50
+ }
51
+
52
+ return Handler (filesystem , logger , templateFunc )
41
53
}
42
54
43
55
// Handler returns an HTTP handler for serving the static site.
44
56
// This takes a filesystem as a parameter.
45
- func Handler (filesystem fs.FS , logger slog.Logger ) http.Handler {
46
- // Render CSP and CSRF in the served pages
47
- // TODO: Bring back templates
48
- _ = func (r * http.Request ) interface {} {
49
- return htmlState {
50
- // Nonce is the CSP nonce for the given request (if there is one present)
51
- CSP : cspState {Nonce : secure .CSPNonce (r .Context ())},
52
- // Token is the CSRF token for the given request
53
- CSRF : csrfState {Token : nosurf .Token (r )},
54
- }
55
- }
56
-
57
+ func Handler (filesystem fs.FS , logger slog.Logger , templateFunc HTMLTemplateHandler ) http.Handler {
57
58
router := chi .NewRouter ()
58
59
59
60
staticFileHandler , err := serveFiles (filesystem , logger )
@@ -137,17 +138,9 @@ func serveFiles(fileSystem fs.FS, logger slog.Logger) (http.HandlerFunc, error)
137
138
return serveFunc , nil
138
139
}
139
140
140
- type htmlState struct {
141
- CSP cspState
142
- CSRF csrfState
143
- }
144
-
145
- type cspState struct {
146
- Nonce string
147
- }
148
-
149
- type csrfState struct {
150
- Token string
141
+ type HtmlState struct {
142
+ CSPNonce string
143
+ CSRFToken string
151
144
}
152
145
153
146
// cspDirectives is a map of all csp fetch directives to their values.
0 commit comments