-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathresponse.go
572 lines (461 loc) · 13.5 KB
/
response.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package gonertia
import (
"bytes"
"context"
"fmt"
"html/template"
"net/http"
"strings"
)
// TemplateData are data that will be available in the root template.
type TemplateData map[string]any
// TemplateFuncs are functions that will be available in the root template.
type TemplateFuncs map[string]any
// Props are the data that will be transferred
// and will be available in the front-end component.
type Props map[string]any
// OptionalProp is a property that will evaluate when needed.
//
// https://inertiajs.com/partial-reloads
type OptionalProp struct {
ignoresFirstLoad
Value any
}
func (p OptionalProp) Prop() any {
return p.Value
}
func Optional(value any) OptionalProp {
return OptionalProp{Value: value}
}
var _ ignoreFirstLoad = OptionalProp{}
type ignoreFirstLoad interface {
shouldIgnoreFirstLoad() bool
}
type ignoresFirstLoad struct{}
func (i ignoresFirstLoad) shouldIgnoreFirstLoad() bool { return true }
// Deprecated: use OptionalProp.
type LazyProp = OptionalProp
// Deprecated: use Optional.
func Lazy(value any) LazyProp {
return LazyProp{Value: value}
}
// DeferProp is a property that will evaluate after page load.
//
// https://v2.inertiajs.com/deferred-props
type DeferProp struct {
ignoresFirstLoad
mergesProps
Value any
Group string
}
func (p DeferProp) Prop() any {
return p.Value
}
func (p DeferProp) Merge() DeferProp {
p.merge = true
return p
}
func Defer(value any, group ...string) DeferProp {
return DeferProp{
Value: value,
Group: firstOr[string](group, "default"),
}
}
var _ ignoreFirstLoad = DeferProp{}
var _ mergeable = DeferProp{}
// AlwaysProp is a property that will always evaluated.
//
// https://inertiajs.com/partial-reloads
type AlwaysProp struct {
Value any
}
func (p AlwaysProp) Prop() any {
return p.Value
}
func Always(value any) AlwaysProp {
return AlwaysProp{Value: value}
}
// MergeProps is a property, which items will be merged instead of overwrite.
//
// https://v2.inertiajs.com/merging-props
type MergeProps struct {
mergesProps
Value any
}
func (p MergeProps) Prop() any {
return p.Value
}
func (p MergeProps) Merge() MergeProps {
p.merge = true
return p
}
func Merge(value any) MergeProps {
return MergeProps{
Value: value,
mergesProps: mergesProps{merge: true},
}
}
var _ mergeable = MergeProps{}
type mergeable interface {
shouldMerge() bool
}
type mergesProps struct {
merge bool
}
func (p mergesProps) shouldMerge() bool {
return p.merge
}
// Proper is an interface for custom type, which provides property, that will be resolved.
type Proper interface {
Prop() any
}
// TryProper is an interface for custom type, which provides property and error, that will be resolved.
type TryProper interface {
TryProp() (any, error)
}
// ValidationErrors are messages, that will be stored in the "errors" prop.
type ValidationErrors map[string]any
// Location creates redirect response.
//
// If request was made by Inertia - sets status to 409 and url will be in "X-Inertia-Location" header.
// Otherwise, it will do an HTTP redirect with specified status (default is 302 for GET, 303 for POST/PUT/PATCH).
func (i *Inertia) Location(w http.ResponseWriter, r *http.Request, url string, status ...int) {
i.flashContext(r.Context())
if IsInertiaRequest(r) {
setInertiaLocationInResponse(w, url)
deleteInertiaInResponse(w)
deleteVaryInResponse(w)
setResponseStatus(w, http.StatusConflict)
return
}
redirectResponse(w, r, url, status...)
}
// Back creates plain redirect response to the previous url.
func (i *Inertia) Back(w http.ResponseWriter, r *http.Request, status ...int) {
i.Redirect(w, r, backURL(r), status...)
}
func backURL(r *http.Request) string {
// At the moment, it based only on the "Referer" HTTP header.
return refererFromRequest(r)
}
// Redirect creates plain redirect response.
func (i *Inertia) Redirect(w http.ResponseWriter, r *http.Request, url string, status ...int) {
i.flashContext(r.Context())
redirectResponse(w, r, url, status...)
}
func (i *Inertia) flashContext(ctx context.Context) {
i.flashValidationErrorsFromContext(ctx)
i.flashClearHistoryFromContext(ctx)
}
func (i *Inertia) flashValidationErrorsFromContext(ctx context.Context) {
if i.flash == nil {
return
}
validationErrors := ValidationErrorsFromContext(ctx)
if len(validationErrors) == 0 {
return
}
err := i.flash.FlashErrors(ctx, validationErrors)
if err != nil {
i.logger.Printf("cannot flash validation errors: %s", err)
}
}
func (i *Inertia) flashClearHistoryFromContext(ctx context.Context) {
if i.flash == nil {
return
}
clearHistory := ClearHistoryFromContext(ctx)
if !clearHistory {
return
}
err := i.flash.FlashClearHistory(ctx)
if err != nil {
i.logger.Printf("cannot flash clear history: %s", err)
}
}
// Render returns response with Inertia data.
//
// If request was made by Inertia - it will return data in JSON format.
// Otherwise, it will return HTML with root template.
//
// If SSR is enabled, pre-renders JavaScript and return HTML (https://inertiajs.com/server-side-rendering).
func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component string, props ...Props) (err error) {
p, err := i.buildPage(r, component, firstOr[Props](props, nil))
if err != nil {
return fmt.Errorf("build page: %w", err)
}
if IsInertiaRequest(r) {
if err = i.doInertiaResponse(w, p); err != nil {
return fmt.Errorf("inertia response: %w", err)
}
return
}
if err = i.doHTMLResponse(w, r, p); err != nil {
return fmt.Errorf("html response: %w", err)
}
return nil
}
type page struct {
Component string `json:"component"`
Props Props `json:"props"`
URL string `json:"url"`
Version string `json:"version"`
EncryptHistory bool `json:"encryptHistory"`
ClearHistory bool `json:"clearHistory"`
DeferredProps map[string][]string `json:"deferredProps,omitempty"`
MergeProps []string `json:"mergeProps,omitempty"`
}
func (i *Inertia) buildPage(r *http.Request, component string, props Props) (*page, error) {
deferredProps := resolveDeferredProps(r, component, props)
mergeProps := resolveMergeProps(r, props)
props, err := i.resolveProps(r, component, props)
if err != nil {
return nil, fmt.Errorf("resolve props: %w", err)
}
return &page{
Component: component,
Props: props,
URL: r.RequestURI,
Version: i.version,
EncryptHistory: i.resolveEncryptHistory(r.Context()),
ClearHistory: ClearHistoryFromContext(r.Context()),
DeferredProps: deferredProps,
MergeProps: mergeProps,
}, nil
}
func (i *Inertia) resolveProps(r *http.Request, component string, props Props) (Props, error) {
result := make(Props)
{
// Add validation errors from context to the result.
result["errors"] = AlwaysProp{ValidationErrorsFromContext(r.Context())}
}
{
// Add shared props to the result.
for key, val := range i.sharedProps {
result[key] = val
}
// Add props from context to the result.
for key, val := range PropsFromContext(r.Context()) {
result[key] = val
}
// Add passed props to the result.
for key, val := range props {
result[key] = val
}
}
{
// Partial reloads only work for visits made to the same page component.
//
// https://inertiajs.com/partial-reloads
if isPartial(r, component) {
// Only (include keys) and except (exclude keys) logic.
only, except := getOnlyAndExcept(r)
if len(only) > 0 {
for key, val := range result {
if _, ok := only[key]; ok {
continue
}
if _, ok := val.(AlwaysProp); ok {
continue
}
delete(result, key)
}
}
for key := range except {
if _, ok := result[key].(AlwaysProp); ok {
continue
}
delete(result, key)
}
} else {
// Props with ignoreFirstLoad should not be included.
for key, val := range result {
if ifl, ok := val.(ignoreFirstLoad); ok && ifl.shouldIgnoreFirstLoad() {
delete(result, key)
}
}
}
}
// Resolve props values.
for key, val := range result {
var err error
result[key], err = resolvePropVal(val)
if err != nil {
return nil, fmt.Errorf("resolve prop value: %w", err)
}
}
return result, nil
}
func isPartial(r *http.Request, component string) bool {
return partialComponentFromRequest(r) == component
}
func getOnlyAndExcept(r *http.Request) (only, except map[string]struct{}) {
return setOf[string](onlyFromRequest(r)), setOf[string](exceptFromRequest(r))
}
func resolvePropVal(val any) (_ any, err error) {
switch proper := val.(type) {
case Proper:
val = proper.Prop()
case TryProper:
val, err = proper.TryProp()
if err != nil {
return nil, err
}
}
switch typed := val.(type) {
case func() any:
return typed(), nil
case func() (any, error):
val, err = typed()
if err != nil {
return nil, fmt.Errorf("closure prop resolving: %w", err)
}
}
return val, nil
}
func resolveDeferredProps(r *http.Request, component string, props Props) map[string][]string {
if isPartial(r, component) {
return nil
}
keysByGroups := make(map[string][]string)
for key, val := range props {
if dp, ok := val.(DeferProp); ok {
keysByGroups[dp.Group] = append(keysByGroups[dp.Group], key)
}
}
return keysByGroups
}
func resolveMergeProps(r *http.Request, props Props) []string {
resetProps := setOf[string](resetFromRequest(r))
var mergeProps []string
for key, val := range props {
if _, ok := resetProps[key]; ok {
continue
}
if m, ok := val.(mergeable); ok && m.shouldMerge() {
mergeProps = append(mergeProps, key)
}
}
return mergeProps
}
func (i *Inertia) resolveEncryptHistory(ctx context.Context) bool {
encryptHistory, ok := EncryptHistoryFromContext(ctx)
if ok {
return encryptHistory
}
return i.encryptHistory
}
func (i *Inertia) doInertiaResponse(w http.ResponseWriter, page *page) error {
pageJSON, err := i.jsonMarshaller.Marshal(page)
if err != nil {
return fmt.Errorf("json marshal page into json: %w", err)
}
setInertiaInResponse(w)
setJSONResponse(w)
setResponseStatus(w, http.StatusOK)
if _, err = w.Write(pageJSON); err != nil {
return fmt.Errorf("write bytes to response: %w", err)
}
return nil
}
func (i *Inertia) doHTMLResponse(w http.ResponseWriter, r *http.Request, page *page) (err error) {
// If root template is already created - we'll use it to save some time.
if i.rootTemplate == nil {
i.rootTemplate, err = i.buildRootTemplate()
if err != nil {
return fmt.Errorf("build root template: %w", err)
}
}
templateData, err := i.buildTemplateData(r, page)
if err != nil {
return fmt.Errorf("build template data: %w", err)
}
setHTMLResponse(w)
if err = i.rootTemplate.Execute(w, templateData); err != nil {
return fmt.Errorf("execute root template: %w", err)
}
return nil
}
func (i *Inertia) buildRootTemplate() (*template.Template, error) {
tmpl := template.New("").Funcs(template.FuncMap(i.sharedTemplateFuncs))
return tmpl.Parse(i.rootTemplateHTML)
}
func (i *Inertia) buildTemplateData(r *http.Request, page *page) (TemplateData, error) {
// Defaults.
inertia, inertiaHead, err := i.buildInertiaHTML(page)
if err != nil {
return nil, fmt.Errorf("build inertia html: %w", err)
}
templateData := TemplateData{
"inertia": inertia,
"inertiaHead": inertiaHead,
}
// Add the shared template data to the result.
for key, val := range i.sharedTemplateData {
templateData[key] = val
}
// Add template data from context to the result.
for key, val := range TemplateDataFromContext(r.Context()) {
templateData[key] = val
}
return templateData, nil
}
func (i *Inertia) buildInertiaHTML(page *page) (inertia, inertiaHead template.HTML, _ error) {
pageJSON, err := i.jsonMarshaller.Marshal(page)
if err != nil {
return "", "", fmt.Errorf("json marshal page into json: %w", err)
}
if i.isSSREnabled() {
inertia, inertiaHead, err = i.htmlContainerSSR(pageJSON)
if err == nil {
return inertia, inertiaHead, nil
}
i.logger.Printf("ssr rendering error: %s", err)
}
return i.htmlContainer(pageJSON)
}
func (i *Inertia) isSSREnabled() bool {
return i.ssrURL != ""
}
// htmlContainerSSR will send request with json marshaled page payload to ssr render endpoint.
// That endpoint will return head and body html, which will be returned and then rendered.
func (i *Inertia) htmlContainerSSR(pageJSON []byte) (inertia, inertiaHead template.HTML, _ error) {
url := i.prepareSSRURL()
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(pageJSON))
if err != nil {
return "", "", fmt.Errorf("new http request: %w", err)
}
setJSONRequest(req)
resp, err := i.ssrHTTPClient.Do(req)
if err != nil {
return "", "", fmt.Errorf("execute http request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode >= http.StatusBadRequest {
return "", "", fmt.Errorf("invalid response status code: %d", resp.StatusCode)
}
var ssr struct {
Head []string `json:"head"`
Body string `json:"body"`
}
err = i.jsonMarshaller.Decode(resp.Body, &ssr)
if err != nil {
return "", "", fmt.Errorf("json decode ssr render response: %w", err)
}
inertia = template.HTML(ssr.Body)
inertiaHead = template.HTML(strings.Join(ssr.Head, "\n"))
return inertia, inertiaHead, nil
}
func (i *Inertia) prepareSSRURL() string {
return strings.ReplaceAll(i.ssrURL, "/render", "") + "/render"
}
func (i *Inertia) htmlContainer(pageJSON []byte) (inertia, _ template.HTML, _ error) {
var sb strings.Builder
// It doesn't look pretty, but fast!
sb.WriteString(`<div id="`)
sb.WriteString(i.containerID)
sb.WriteString(`" data-page="`)
template.HTMLEscape(&sb, pageJSON)
sb.WriteString(`"></div>`)
return template.HTML(sb.String()), "", nil
}