-
Notifications
You must be signed in to change notification settings - Fork 4
/
inertia.go
executable file
·106 lines (80 loc) · 2.11 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
package inertia
import (
"encoding/json"
"github.com/gofiber/fiber"
"log"
"strconv"
"strings"
)
type Config struct {
// AssetsPath defines the path of assets, if something is changed in the target path,
// inertiaJS will be notified
AssetsPath string
// Filter defines a function to skip middleware.
// Optional. Default: nil
Filter func(*fiber.Ctx) bool
}
func New (config ...Config) func(*fiber.Ctx) {
if len(config) == 0 {
panic("No config found")
}
return func(c *fiber.Ctx) {
if len(config[0].AssetsPath) == 0 {
panic("AssetsPath is required")
}
hash := hashDir(config[0].AssetsPath)
if c.Method() == "GET" && c.XHR() && c.Get("X-Inertia-Version", "1") != hash {
c.Set("X-Inertia-Location", c.Path())
_ = c.Status(fiber.StatusConflict).JSON(Map{})
return
}
c.Set("X-Inertia-Version", hash)
c.Next()
}
}
type Map map[string]interface{}
func Render(c *fiber.Ctx, component string, props Map) {
props = PartialReload(c, component, props)
Display(c, component, props)
}
func Display(c *fiber.Ctx, component string, props Map) {
data := map[string]interface{}{
"component": component,
"props": props,
"url": c.OriginalURL(),
"version": c.Get("X-Inertia-Version", ""),
}
renderJSON, err := strconv.ParseBool(c.Get("X-Inertia", "false"))
if err != nil {
log.Fatal("X-Inertia not parsable")
}
if renderJSON && c.XHR() {
JsonResponse(c, data)
return
}
HtmlResponse(c, data)
}
func HtmlResponse(c *fiber.Ctx, data Map) {
componentDataByte, _ := json.Marshal(data)
_ = c.Render("index", fiber.Map{
"Page": string(componentDataByte),
})
}
func JsonResponse(c *fiber.Ctx, page Map) {
jsonByte, _ := json.Marshal(page)
_ = c.Status(fiber.StatusOK).JSON(string(jsonByte))
}
func PartialReload(c *fiber.Ctx, component string, props Map) Map {
if c.Get("X-Inertia-Partial-Component", "/") == component {
var newProps = make(Map)
partials := strings.Split(c.Get("X-Inertia-Partial-Data", ""), ",")
for key, _ := range props {
for _, partial := range partials {
if key == partial {
newProps[partial] = props[key]
}
}
}
}
return props
}