-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
51 lines (46 loc) · 1.66 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
package htmxtools
import (
"net/http"
"strings"
)
// WrapFunc is middleware for inspecting http requests for htmx metadata
func WrapFunc(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if res := ParseRequest(r); res != nil {
ctx := res.ToContext(r.Context())
next.ServeHTTP(w, r.WithContext(ctx))
return
}
next.ServeHTTP(w, r)
}
}
// Wrap is middleware for inspecting http requests for htmx metadata
func Wrap(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if res := ParseRequest(r); res != nil {
ctx := res.ToContext(r.Context())
next.ServeHTTP(w, r.WithContext(ctx))
return
}
next.ServeHTTP(w, r)
})
}
// ParseRequest parses an [http.Request] for any htmx request headers and returns an [HTMXRequest]
// fields will still have to be checked for empty string at call sites
func ParseRequest(r *http.Request) *HTMXRequest {
tru := "true"
isHTMXRequest := strings.TrimSpace(r.Header.Get(HXRequestHeader.String())) == tru
if !isHTMXRequest {
return nil
}
res := &HTMXRequest{
Boosted: strings.TrimSpace(r.Header.Get(BoostedRequest.String())) == tru,
CurrentURL: strings.TrimSpace(r.Header.Get(CurrentURLRequest.String())),
HistoryRestore: strings.TrimSpace(r.Header.Get(HistoryRestoreRequest.String())) == tru,
Prompt: strings.TrimSpace(r.Header.Get(PromptRequest.String())),
Target: strings.TrimSpace(r.Header.Get(TargetRequest.String())),
TriggerName: strings.TrimSpace(r.Header.Get(TriggerNameRequest.String())),
Trigger: strings.TrimSpace(r.Header.Get(TriggerRequest.String())),
}
return res
}