-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinertia.go
236 lines (188 loc) · 5.33 KB
/
inertia.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
package inertia
import (
"bytes"
"net/http"
"sync"
"github.com/labstack/echo/v4"
)
const (
HeaderXInertia = "X-Inertia"
HeaderXInertiaVersion = "X-Inertia-Version"
HeaderXInertiaLocation = "X-Inertia-Location"
HeaderXInertiaPartialData = "X-Inertia-Partial-Data"
HeaderXInertiaPartialComponent = "X-Inertia-Partial-Component"
)
type Inertia struct {
c echo.Context
rootView string
sharedProps map[string]interface{}
version VersionFunc
renderer Renderer
isSsrDisabled bool
mu sync.RWMutex
}
func (i *Inertia) SetRenderer(r Renderer) {
i.renderer = r
}
func (i *Inertia) Renderer() Renderer {
return i.renderer
}
func (i *Inertia) IsSsrDisabled() bool {
return i.isSsrDisabled
}
func (i *Inertia) IsSsrEnabled() bool {
return !i.isSsrDisabled
}
func (i *Inertia) EnableSsr() {
i.isSsrDisabled = false
}
func (i *Inertia) DisableSsr() {
i.isSsrDisabled = true
}
func (i *Inertia) SetRootView(name string) {
i.rootView = name
}
func (i *Inertia) RootView() string {
return i.rootView
}
func (i *Inertia) Share(props map[string]interface{}) {
i.mu.Lock()
defer i.mu.Unlock()
// merge shared props
for k, v := range props {
i.sharedProps[k] = v
}
}
func (i *Inertia) Shared() map[string]interface{} {
i.mu.RLock()
defer i.mu.RUnlock()
return i.sharedProps
}
func (i *Inertia) FlushShared() {
i.mu.Lock()
defer i.mu.Unlock()
i.sharedProps = map[string]interface{}{}
}
type VersionFunc func() string
func (i *Inertia) SetVersion(version VersionFunc) {
i.version = version
}
func (i *Inertia) Version() string {
return i.version()
}
// Location generates 409 response for external redirects
// see https://inertiajs.com/redirects#external-redirects
func (i *Inertia) Location(url string) error {
if i.c.Request().Header.Get(HeaderXInertia) != "" {
res := i.c.Response()
res.Header().Set(HeaderXInertiaLocation, url)
res.WriteHeader(409)
return nil
} else {
return i.c.Redirect(http.StatusFound, url)
}
}
func (i *Inertia) Render(code int, component string, props map[string]interface{}) error {
return i.render(code, component, props, map[string]interface{}{})
}
func (i *Inertia) RenderWithViewData(code int, component string, props, viewData map[string]interface{}) error {
return i.render(code, component, props, viewData)
}
type Page struct {
Component string `json:"component"`
Props map[string]interface{} `json:"props"`
URL string `json:"url"`
Version string `json:"version"`
}
func (i *Inertia) render(code int, component string, props, viewData map[string]interface{}) error {
c := i.c
req := c.Request()
res := c.Response()
props = mergeProps(i.sharedProps, props)
only := splitAndRemoveEmpty(req.Header.Get(HeaderXInertiaPartialData), ",")
if len(only) > 0 && req.Header.Get(HeaderXInertiaPartialComponent) == component {
filteredProps := map[string]interface{}{}
for _, key := range only {
filteredProps[key] = props[key]
}
props = filteredProps
} else {
filteredProps := map[string]interface{}{}
for key, prop := range props {
// LazyProp is only used in partial reloads
// see https://inertiajs.com/partial-reloads#lazy-data-evaluation
if _, ok := prop.(*LazyProp); !ok {
filteredProps[key] = prop
}
}
props = filteredProps
}
if err := evaluateProps(props); err != nil {
return err
}
page := &Page{
Component: component,
Props: props,
URL: req.URL.String(),
Version: i.Version(),
}
res.Header().Set("Vary", HeaderXInertia)
if req.Header.Get(HeaderXInertia) != "" {
res.Header().Set(HeaderXInertia, "true")
return c.JSON(code, page)
}
viewData["page"] = page
return i.renderHTML(code, i.rootView, viewData)
}
// renderHTML renders HTML template with given code, name and data.
func (i *Inertia) renderHTML(code int, name string, data map[string]interface{}) error {
if i.renderer == nil {
return ErrRendererNotRegistered
}
buf := new(bytes.Buffer)
if err := i.renderer.Render(buf, name, data, i); err != nil {
return err
}
return i.c.HTMLBlob(code, buf.Bytes())
}
type LazyPropFunc func() (interface{}, error)
type LazyProp struct {
callback LazyPropFunc
}
// Lazy defines a lazy evaluated data.
// see https://inertiajs.com/partial-reloads#lazy-data-evaluation
func Lazy(callback LazyPropFunc) *LazyProp {
return &LazyProp{
callback: callback,
}
}
func SetRootView(c echo.Context, name string) {
MustGet(c).SetRootView(name)
}
func RootView(c echo.Context) string {
return MustGet(c).RootView()
}
func Share(c echo.Context, props map[string]interface{}) {
MustGet(c).Share(props)
}
func Shared(c echo.Context) map[string]interface{} {
return MustGet(c).Shared()
}
func FlushShared(c echo.Context) {
MustGet(c).FlushShared()
}
func SetVersion(c echo.Context, version VersionFunc) {
MustGet(c).SetVersion(version)
}
func Version(c echo.Context) string {
return MustGet(c).Version()
}
func Location(c echo.Context, url string) error {
return MustGet(c).Location(url)
}
func Render(c echo.Context, code int, component string, props map[string]interface{}) error {
return MustGet(c).Render(code, component, props)
}
func RenderWithViewData(c echo.Context, code int, component string, props, viewData map[string]interface{}) error {
return MustGet(c).RenderWithViewData(code, component, props, viewData)
}