/
mux.go
70 lines (63 loc) · 1.36 KB
/
mux.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
package wtf
import (
"net/http"
"strings"
)
type (
mux_node interface {
match_self(string, RESTParams) (bool, string, RESTParams)
match(string, RESTParams) (bool, func(Context), RESTParams)
merge(string, func(Context)) bool
deep_clone() mux_node
}
wtf_mux struct {
nodes map[string]mux_node
}
)
func NewWTFMux() Mux {
ret := &wtf_mux{make(map[string]mux_node)}
return ret
}
func (sm *wtf_mux) handle_to_method(h func(Context), p string, method string) Error {
if mux, exist := sm.nodes[method]; exist {
tmp := mux.deep_clone()
tmp.merge(p, h)
sm.nodes[method] = tmp
} else {
sm.nodes[method] = parse_path(p, h)
}
return nil
}
func (sm *wtf_mux) Handle(h func(Context), p string, args ...string) Error {
methods := []string{}
if len(args) > 0 {
for _, m := range args {
switch strings.ToUpper(m) {
case "ALL":
methods = AllSupportMethods()
break
default:
if ValidMethod(m) {
methods = append(methods, strings.ToUpper(m))
}
}
}
} else {
methods = AllSupportMethods()
}
for _, m := range methods {
sm.handle_to_method(h, p, m)
}
return nil
}
func (sm *wtf_mux) Match(req *http.Request) (func(Context), Rest) {
up := RESTParams{}
method := strings.ToUpper(req.Method)
if mux, exist := sm.nodes[method]; exist {
_, h, up := mux.match(req.URL.Path, up)
if h != nil {
return h, up
}
}
return nil, up
}