-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmethod.go
97 lines (80 loc) · 1.44 KB
/
method.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
package brouter
import (
"errors"
)
var ErrMethod = errors.New("error method")
type method struct {
method [7]*tree
}
func (m *method) init() {
for k := range m.method {
if m.method[k] == nil {
m.method[k] = newTree()
}
}
}
func methodIndex(method string) (int, error) {
if len(method) <= 2 {
return 0, ErrMethod
}
switch method[0] {
case 'G':
return 0, nil
case 'P':
switch method[1] {
case 'O':
return 1, nil
case 'U':
return 2, nil
case 'A':
return 3, nil
default:
return 0, ErrMethod
}
case 'D':
return 4, nil
case 'H':
return 5, nil
case 'O':
return 6, nil
default:
return 0, ErrMethod
}
return 0, ErrMethod
}
func (m *method) getTree(method string) *tree {
index, err := methodIndex(method)
if err != nil {
return nil
}
return m.method[index]
}
func (m *method) save(method, path string, h HandleFunc) {
index, err := methodIndex(method)
if err != nil {
panic(err.Error())
}
m.method[index].insert(path, h)
}
/*
type method struct {
method map[string]*tree
}
func (m *method) init() {
if m.method == nil {
m.method = make(map[string]*tree)
}
for _, k := range []string{"GET", "POST", "DELETE", "HEAD", "OPTIONS", "PUT", "PATCH"} {
if m.method[k] == nil {
m.method[k] = newTree()
}
}
}
func (m *method) getTree(method string) *tree {
t, _ := m.method[method]
return t
}
func (m *method) save(method, path string, h HandleFunc) {
m.method[method].insert(path, h)
}
*/