-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreemux.go
194 lines (156 loc) · 4.64 KB
/
treemux.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
185
186
187
188
189
190
191
192
193
194
package mixmux
import (
"net/http"
"strings"
"github.com/dimfeld/httptreemux/v5"
)
// TreeMux wraps HTTPTreeMux.
type TreeMux struct {
t *httptreemux.TreeMux
path string
reg map[string][]string
}
// NewTreeMux returns a wrapped HTTPTreeMux.
func NewTreeMux(opts *Options) *TreeMux {
t := &TreeMux{
t: httptreemux.New(),
path: "",
reg: make(map[string][]string),
}
if opts == nil {
opts = &Options{}
}
if opts.NotFound != nil {
t.t.NotFoundHandler = opts.NotFound.ServeHTTP
}
if opts.MethodNotAllowed != nil {
t.t.MethodNotAllowedHandler = func(w http.ResponseWriter, r *http.Request, m map[string]httptreemux.HandlerFunc) {
opts.MethodNotAllowed.ServeHTTP(w, r)
}
}
t.t.RedirectTrailingSlash = opts.RedirectTrailingSlash
t.t.RedirectCleanPath = opts.RedirectFixedPath
t.t.RedirectTrailingSlash = true
return t
}
func (m *TreeMux) addToReg(method, path string) {
_, ok := m.reg[path]
if !ok {
m.reg[path] = []string{}
}
m.reg[path] = append(m.reg[path], method)
}
func (m *TreeMux) handle(method, path string, handler http.Handler) {
m.t.Handle(method, path, treeMuxWrapper(handler))
m.addToReg(method, path)
}
func (m *TreeMux) lookup(method, path string) (bool, bool) {
ckSlsh := false
if m.t.RedirectTrailingSlash && path[len(path)-1] != '/' {
ckSlsh = true
}
meths, ok := m.reg[path]
if !ok {
return false, ckSlsh
}
for _, m := range meths {
if m == method {
return true, false
}
}
return false, ckSlsh
}
// Group takes a path and returns a new TreeMux wrapping the original TreeMux.
func (m *TreeMux) Group(path string) *TreeMux {
return &TreeMux{m.t, m.path + path, m.reg}
}
// GroupMux takes a path and returns a new TreeMux wrapping the original TreeMux.
func (m *TreeMux) GroupMux(path string) Mux {
return &TreeMux{m.t, m.path + path, m.reg}
}
// Any takes a path and http.Handler and adds them to the mux.
func (m *TreeMux) Any(path string, h http.Handler) {
p := m.path + path
m.handle(http.MethodOptions, p, h)
m.handle(http.MethodGet, p, h)
m.handle(http.MethodPost, p, h)
m.handle(http.MethodPut, p, h)
m.handle(http.MethodPatch, p, h)
m.handle(http.MethodDelete, p, h)
m.handle(http.MethodHead, p, h)
m.handle(http.MethodTrace, p, h)
m.handle(http.MethodConnect, p, h)
}
// Options takes a path and http.Handler and adds them to the mux.
func (m *TreeMux) Options(path string, h http.Handler) {
m.handle(http.MethodOptions, m.path+path, h)
}
// Get takes a path and http.Handler and adds them to the mux.
func (m *TreeMux) Get(path string, h http.Handler) {
m.handle(http.MethodGet, m.path+path, h)
}
// Post takes a path and http.Handler and adds them to the mux.
func (m *TreeMux) Post(path string, h http.Handler) {
m.handle(http.MethodPost, m.path+path, h)
}
// Put takes a path and http.Handler and adds them to the mux.
func (m *TreeMux) Put(path string, h http.Handler) {
m.handle(http.MethodPut, m.path+path, h)
}
// Patch takes a path and http.Handler and adds them to the mux.
func (m *TreeMux) Patch(path string, h http.Handler) {
m.handle(http.MethodPatch, m.path+path, h)
}
// Delete takes a path and http.Handler and adds them to the mux.
func (m *TreeMux) Delete(path string, h http.Handler) {
m.handle(http.MethodDelete, m.path+path, h)
}
// Head takes a path and http.Handler and adds them to the mux.
func (m *TreeMux) Head(path string, h http.Handler) {
m.handle(http.MethodHead, m.path+path, h)
}
// Trace takes a path and http.Handler and adds them to the mux.
func (m *TreeMux) Trace(path string, h http.Handler) {
m.handle(http.MethodTrace, m.path+path, h)
}
// Connect takes a path and http.Handler and adds them to the mux.
func (m *TreeMux) Connect(path string, h http.Handler) {
m.handle(http.MethodConnect, m.path+path, h)
}
// ServeHTTP satisfies the http.Handler interface.
func (m *TreeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.t.ServeHTTP(w, r)
}
// CORSMethods ... TODO:
func (m *TreeMux) CORSMethods(path string, handlerWrappers ...func(http.Handler) http.Handler) {
ms := []string{http.MethodOptions}
for _, v := range methods {
if v == http.MethodOptions {
continue
}
x, s := m.lookup(v, path)
if s {
x, _ = m.lookup(v, path+"/")
}
if !x {
continue
}
ms = append(ms, v)
}
opts := strings.Join(ms, ", ")
var fn http.Handler
fn = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Methods", opts)
})
for _, wrap := range handlerWrappers {
if wrap != nil {
fn = wrap(fn)
}
}
m.Options(path, fn)
}
func treeMuxWrapper(next http.Handler) httptreemux.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, _ map[string]string) {
next.ServeHTTP(w, r)
}
}