-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
146 lines (118 loc) · 3.7 KB
/
router.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
package mixmux
import (
"net/http"
"strings"
"github.com/julienschmidt/httprouter"
)
// Router wraps HTTPRouter.
type Router struct {
r *httprouter.Router
path string
}
// NewRouter returns a wrapped HTTPRouter.
func NewRouter(opts *Options) *Router {
// TODO:
r := &Router{
path: "",
}
if opts == nil {
opts = &Options{}
}
r.r = &httprouter.Router{
RedirectTrailingSlash: opts.RedirectTrailingSlash,
RedirectFixedPath: opts.RedirectFixedPath,
HandleMethodNotAllowed: opts.HandleMethodNotAllowed,
NotFound: opts.NotFound,
MethodNotAllowed: opts.MethodNotAllowed,
}
return r
}
// Group takes a path and returns a new Router wrapping the original Router.
func (m *Router) Group(path string) *Router {
return &Router{m.r, m.path + path}
}
// GroupMux takes a path and returns a new Router wrapping the original Router.
func (m *Router) GroupMux(path string) Mux {
return &Router{m.r, m.path + path}
}
// Any takes a path and http.Handler and adds them to the mux.
func (m *Router) Any(path string, h http.Handler) {
p := m.path + path
m.r.Handler(http.MethodOptions, p, h)
m.r.Handler(http.MethodGet, p, h)
m.r.Handler(http.MethodPost, p, h)
m.r.Handler(http.MethodPut, p, h)
m.r.Handler(http.MethodPatch, p, h)
m.r.Handler(http.MethodDelete, p, h)
m.r.Handler(http.MethodHead, p, h)
m.r.Handler(http.MethodTrace, p, h)
m.r.Handler(http.MethodConnect, p, h)
}
// Options takes a path and http.Handler and adds them to the mux.
func (m *Router) Options(path string, h http.Handler) {
m.r.Handler(http.MethodOptions, m.path+path, h)
}
// Get takes a path and http.Handler and adds them to the mux.
func (m *Router) Get(path string, h http.Handler) {
m.r.Handler(http.MethodGet, m.path+path, h)
}
// Post takes a path and http.Handler and adds them to the mux.
func (m *Router) Post(path string, h http.Handler) {
m.r.Handler(http.MethodPost, m.path+path, h)
}
// Put takes a path and http.Handler and adds them to the mux.
func (m *Router) Put(path string, h http.Handler) {
m.r.Handler(http.MethodPut, m.path+path, h)
}
// Patch takes a path and http.Handler and adds them to the mux.
func (m *Router) Patch(path string, h http.Handler) {
m.r.Handler(http.MethodPatch, m.path+path, h)
}
// Delete takes a path and http.Handler and adds them to the mux.
func (m *Router) Delete(path string, h http.Handler) {
m.r.Handler(http.MethodDelete, m.path+path, h)
}
// Head takes a path and http.Handler and adds them to the mux.
func (m *Router) Head(path string, h http.Handler) {
m.r.Handler(http.MethodHead, m.path+path, h)
}
// Trace takes a path and http.Handler and adds them to the mux.
func (m *Router) Trace(path string, h http.Handler) {
m.r.Handler(http.MethodTrace, m.path+path, h)
}
// Connect takes a path and http.Handler and adds them to the mux.
func (m *Router) Connect(path string, h http.Handler) {
m.r.Handler(http.MethodConnect, m.path+path, h)
}
// CORSMethods ... TODO:
func (m *Router) CORSMethods(path string, handlerWrappers ...func(http.Handler) http.Handler) {
ms := []string{http.MethodOptions}
for _, v := range methods {
if v == http.MethodOptions {
continue
}
h, _, s := m.r.Lookup(v, path)
if s {
h, _, _ = m.r.Lookup(v, path+"/")
}
if h == nil {
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.r.Handler(http.MethodOptions, path, fn)
}
// ServeHTTP satisfies the http.Handler interface.
func (m *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.r.ServeHTTP(w, r)
}