-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmiddleware.go
184 lines (157 loc) · 4.5 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
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
package inertia
import (
"net/http"
"os"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
const (
key = "__inertia__"
)
type MiddlewareConfig struct {
Skipper middleware.Skipper
// The root template that's loaded on the first page visit.
// see https://inertiajs.com/server-side-setup#root-template
RootView string
// Determines the current asset version.
// see https://inertiajs.com/asset-versioning
VersionFunc func() string
// Defines the props that are shared by default.
// see https://inertiajs.com/shared-data
Share SharedDataFunc
// Renderer is a renderer that is used for rendering the root view.
Renderer Renderer
// IsSsrDisabled is a flag that determines whether server-side rendering is disabled.
IsSsrDisabled bool
}
type SharedDataFunc func(c echo.Context) (map[string]interface{}, error)
var DefaultMiddlewareConfig = MiddlewareConfig{
Skipper: middleware.DefaultSkipper,
RootView: "app.html",
VersionFunc: defaultVersionFunc(),
Share: nil,
Renderer: nil,
IsSsrDisabled: false,
}
func defaultVersionFunc() VersionFunc {
var v string
// It is for Google App Engine.
// see https://cloud.google.com/appengine/docs/standard/go/runtime#environment_variables
if v = os.Getenv("GAE_VERSION"); v == "" {
// The fallback version value that imitates the default GAE version format.
// It assumes to be used for development.
v = time.Now().Format("20060102t150405")
}
return func() string {
return v
}
}
func Middleware(r Renderer) echo.MiddlewareFunc {
return MiddlewareWithConfig(MiddlewareConfig{
Renderer: r,
})
}
// MiddlewareWithConfig returns an echo middleware that adds the Inertia instance to the context.
func MiddlewareWithConfig(config MiddlewareConfig) echo.MiddlewareFunc {
// Defaults
if config.Skipper == nil {
config.Skipper = DefaultMiddlewareConfig.Skipper
}
if config.RootView == "" {
config.RootView = DefaultMiddlewareConfig.RootView
}
if config.VersionFunc == nil {
config.VersionFunc = DefaultMiddlewareConfig.VersionFunc
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
if config.Skipper(c) {
return next(c)
}
var sharedProps map[string]interface{}
if config.Share != nil {
ret, err := config.Share(c)
if err != nil {
return err
}
sharedProps = ret
} else {
sharedProps = map[string]interface{}{}
}
// Create an Inertia instance.
in := &Inertia{
c: c,
rootView: config.RootView,
sharedProps: sharedProps,
version: config.VersionFunc,
renderer: config.Renderer,
isSsrDisabled: config.IsSsrDisabled,
}
c.Set(key, in)
req := c.Request()
res := c.Response()
if req.Header.Get(HeaderXInertia) == "" {
// Not inertial request
return next(c)
}
// In the event that the assets change, initiate a
// client-side location visit to force an update.
// see https://inertiajs.com/the-protocol#asset-versioning
if checkVersion(req, in.Version()) {
return in.Location(req.URL.Path)
}
// Wrap the http response writer for modify the response headers after handler execution.
w := NewResponseWriterWrapper(res.Writer)
res.Writer = w
defer func(w *ResponseWriterWrapper) {
// send buffered header and restore the original response writer
w.FlushHeader()
res.Writer = w.ResponseWriter
}(w)
if err = next(c); err != nil {
return err
}
changeRedirectCode(req, res)
return nil
}
}
}
// checkVersion checks the assets version change.
func checkVersion(req *http.Request, version string) bool {
if req.Header.Get(HeaderXInertia) != "" &&
req.Method == "GET" &&
req.Header.Get(HeaderXInertiaVersion) != version {
return true
}
return false
}
// changeRedirectCode changes the status code during redirects, ensuring they are made as
// GET requests, preventing "MethodNotAllowedHttpException" errors.
// see https://inertiajs.com/redirects
func changeRedirectCode(req *http.Request, res *echo.Response) {
if req.Header.Get(HeaderXInertia) != "" &&
res.Status == 302 &&
inArray(req.Method, []string{"PUT", "PATCH", "DELETE"}) {
res.Status = 303
res.Writer.WriteHeader(303)
}
}
func Get(c echo.Context) (*Inertia, error) {
in, ok := c.Get(key).(*Inertia)
if !ok {
return nil, ErrNotFound
}
return in, nil
}
func MustGet(c echo.Context) *Inertia {
in, err := Get(c)
if err != nil {
panic(err)
}
return in
}
func Has(c echo.Context) bool {
_, ok := c.Get(key).(*Inertia)
return ok
}