-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmux_muti.go
53 lines (42 loc) · 941 Bytes
/
mux_muti.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
package twig
import (
"net/http"
)
// Matcher 决定一个路由是否匹配当前请求
type Matcher interface {
Match(req *http.Request) bool
}
type MatcherFunc func(req *http.Request) bool
func (f MatcherFunc) Match(req *http.Request) bool {
return f(req)
}
type matchedMux struct {
Muxer
Matcher
}
type muxes struct {
ms []*matchedMux
def Muxer
twig *Twig
}
func (m *muxes) Lookup(method string, path string, req *http.Request) MuxerCtx {
for _, mux := range m.ms {
if mux.Match(req) {
return mux.Lookup(method, path, req)
}
}
return m.def.Lookup(method, path, req)
}
func (m *muxes) AddHandler(method string, path string, h HandlerFunc, ms ...MiddlewareFunc) {
m.def.AddHandler(method, path, h, ms...)
}
func (m *muxes) Use(ms ...MiddlewareFunc) {
m.def.Use(ms...)
}
func (m *muxes) AddMuxer(mux Muxer, match Matcher) {
mf := &matchedMux{
Muxer: m,
Matcher: match,
}
m.ms = append(m.ms, mf)
}