/
route.go
77 lines (69 loc) · 1.99 KB
/
route.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
package live
import (
"fmt"
"net/http"
"reflect"
"runtime/debug"
)
// joinHandler wraps an http.ResponseWriter and swallows all HTTP activity—
// except for WriteHeader, which it steals the status code from but does not pass through.
// This is to prevent the live.Config.Mux routing from poisoning non-LiveView requests
// by writing, for examples, 404s to routes that very much exist elsewhere in the routing tree.
type joinHandler struct {
w http.ResponseWriter
code int
}
func (x *joinHandler) Header() http.Header {
return make(http.Header)
}
func (x *joinHandler) Write(b []byte) (int, error) {
return 0, fmt.Errorf("joinHandler.Write does not accept writes. Attempted to write %q from:\n%s", b, debug.Stack())
}
func (x *joinHandler) WriteHeader(statusCode int) {
if x.code == 0 {
x.code = statusCode
}
}
// SetView marks r as corresponding to v.
// Its handler will result in a rendered LiveView.
func SetView(r *http.Request, v View) {
container, ok := r.Context().Value(liveViewRequestContextKey{}).(*liveViewContainer)
if !ok {
return
}
container.lv = v
container.r = r
}
// GetView returns the View of type T corresponding to r.
// If no such view has been set, returns the zero value for T.
func GetView[T View](r *http.Request) T {
var zero T
container, ok := r.Context().Value(liveViewRequestContextKey{}).(*liveViewContainer)
if !ok {
return zero
}
t, ok := container.lv.(T)
if !ok {
return zero
}
return t
}
// MakeView will either get the existing View of type T associated with r
// or create a View of type T.
// If r is not a LiveView-enabled request, returns the zero value of T.
func MakeView[T View](r *http.Request) T {
var zero T
container, ok := r.Context().Value(liveViewRequestContextKey{}).(*liveViewContainer)
if !ok {
return zero
}
t, ok := container.lv.(T)
if !ok {
typ := reflect.TypeOf(zero)
if typ.Kind() != reflect.Pointer {
return reflect.New(typ).Elem().Interface().(T)
}
return reflect.New(typ.Elem()).Interface().(T)
}
return t
}