-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
49 lines (39 loc) · 1.42 KB
/
middleware.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
package inertia
import (
"github.com/gofiber/fiber/v2"
"github.com/theartefak/inertia-fiber/utils"
)
// Middleware returns a middleware function that sets the version header and context for the engine.
func (e *Engine) Middleware() fiber.Handler {
return func(c *fiber.Ctx) error {
if e.config.AssetsPath == "" {
panic("please provide an assets path")
}
// Compute the hash of the assets directory.
hash := utils.HashDir(e.config.AssetsPath)
// If the request is an XHR GET request and the version header does not match the hash, return a conflict error.
if c.Method() == "GET" && c.XHR() && c.Get(HeaderVersion, "1") != hash {
c.Set(HeaderLocation, c.OriginalURL())
return c.Status(fiber.StatusConflict).JSON(fiber.Map{})
}
// Check if the HTTP method used in the current request is one of "PUT", "PATCH", or "DELETE"
// and the HTTP response status code is "Found" (HTTP 302).
exist, _ := utils.InArray(c.Method(), []string{"PUT", "PATCH", "DELETE"})
if exist && c.Response().StatusCode() == fiber.StatusFound {
c.Status(fiber.StatusSeeOther)
}
// Set the version header and context for the engine.
c.Set(HeaderVersion, hash)
e.version = hash
return c.Next()
}
}
func (e *Engine) Share(name string, value any) {
e.props[name] = value
}
func (e *Engine) WithProp(name string, value any) {
e.next[name] = value
}
func (e *Engine) WithViewData(name string, value any) {
e.params[name] = value
}