-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcontext.go
50 lines (40 loc) · 1.27 KB
/
context.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
package golte
import (
"net/http"
"github.com/nichady/golte/render"
)
type contextKey struct{}
// RenderContext is used for lower level control over rendering.
// It allows direct access to the renderer and component slice.
type RenderContext struct {
Renderer *render.Renderer
Components []render.Entry
ErrPage string
csr bool
scdata render.SvelteContextData
}
// GetRenderContext returns the render context from the request, or nil if it doesn't exist.
func GetRenderContext(r *http.Request) *RenderContext {
rctx, ok := r.Context().Value(contextKey{}).(*RenderContext)
if !ok {
return nil
}
return rctx
}
// MustGetRenderContext is like [GetRenderContext], but panics instead of returning nil.
func MustGetRenderContext(r *http.Request) *RenderContext {
rctx := GetRenderContext(r)
if rctx == nil {
panic("golte middleware not registered")
}
return rctx
}
// Render renders all the components in the render context to the writer,
// with each subsequent component being a child of the previous.
func (r *RenderContext) Render(w http.ResponseWriter) {
data := render.RenderData{Entries: r.Components, ErrPage: r.ErrPage, SCData: r.scdata}
err := r.Renderer.Render(w, data, r.csr)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}