Repository navigation
Expand file tree
/
Copy pathheaders.go
More file actions
81 lines (68 loc) · 2.64 KB
/
Copy pathheaders.go
File metadata and controls
81 lines (68 loc) · 2.64 KB
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
package hx
import (
"net/http"
)
type (
// HxHeaders is a struct that contains the headers and status code to be returned to the client
//
// This is helpful for using HTMX with a framework that doesn't implement the stdlib http.ResponseWriter
HxHeaders struct {
headers map[string]string
}
// HeaderOption is the interface that can be used to set the headers and status code of the response
HeaderOption interface {
apply(*HxHeaders)
}
// HeaderOptionFunc is a function that can be used to set the headers and status code of the response
HeaderOptionFunc func(*HxHeaders)
)
// Headers modify the http.ResponseWriter to add HTMX headers.
//
// The following options are available:
// - Status(int) | StatusStopPolling: Sets the HTTP status code of the HTMX response.
// - Location(path): Enables client-side redirection without a full page reload.
// - PushUrl(string): Pushes a new URL into the history stack.
// - Redirect(string): Performs a client-side redirect with a full page reload.
// - Refresh(bool): If set to "true", triggers a full refresh of the client-side page.
// - ReplaceUrl(string): Replaces the current URL in the location bar.
// - Reswap(string) | {Swap constants}: Specifies an override for how the response will be swapped.
// - Retarget(string): A CSS selector to update the target of the content update to a different page element.
// - Reselect(string): A CSS selector to select a part of the response to be swapped in, overriding existing hx-select on the triggering element.
// - Trigger(...TriggerEvent): Triggers client-side events.
func Headers(w http.ResponseWriter, options ...HeaderOption) error {
o, err := BuildHeaders(options...)
if err != nil {
return err
}
if len(o.headers) > 0 {
for k, v := range o.headers {
w.Header().Set(k, v)
}
}
return nil
}
// BuildHeaders creates a new HxHeaders from the provided options.
//
// It can be used to create a response helper for your own HTTP library.
//
// Several libraries have already been implemented:
// - Echo: import github.com/stackus/hxgo/v4/hxecho
// - Fiber: import github.com/stackus/hxgo/v4/hxfiber
// - Gin: import github.com/stackus/hxgo/v4/hxgin
func BuildHeaders(options ...HeaderOption) (headers *HxHeaders, err error) {
// Recover from any panics that might happen during the processing of the options.
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
o := &HxHeaders{
headers: make(map[string]string),
}
for _, option := range options {
option.apply(o)
}
return o, nil
}
func (r HxHeaders) Headers() map[string]string { return r.headers }
func (f HeaderOptionFunc) apply(o *HxHeaders) { f(o) }