@@ -28,48 +28,44 @@ const (
28
28
MethodTrace = "TRACE"
29
29
)
30
30
31
- var treeGroup map [string ]* Tree
32
-
33
- func init () {
34
- methods := []string {"GET" , "HEAD" , "POST" , "PUT" , "PATCH" , "DELETE" , "CONNECT" , "OPTIONS" , "TRACE" }
35
- treeGroup = make (map [string ]* Tree )
36
- for _ , method := range methods {
37
- tree := newTree ()
38
- tree .name = string (method )
39
- treeGroup [method ] = tree
40
- }
41
- }
42
-
43
31
type PreTree struct {
32
+ treeGroup map [string ]* Tree
44
33
}
45
34
46
35
// 初始化对象
47
36
//
48
37
// Initialize object
49
38
func NewPreTree () * PreTree {
50
- return & PreTree {}
39
+ p := & PreTree {}
40
+ methods := []string {"GET" , "HEAD" , "POST" , "PUT" , "PATCH" , "DELETE" , "CONNECT" , "OPTIONS" , "TRACE" }
41
+ p .treeGroup = make (map [string ]* Tree )
42
+ for _ , method := range methods {
43
+ tree := newTree ()
44
+ tree .name = string (method )
45
+ p .treeGroup [method ] = tree
46
+ }
47
+ return p
51
48
}
52
49
53
50
// 存储路由规则
54
51
//
55
52
// Store routing rules
56
53
func (p * PreTree ) Store (method , urlRule string ) {
57
- t := treeGroup [method ]
54
+ t := p . treeGroup [method ]
58
55
t .insert (urlRule )
59
56
}
60
57
61
58
// 查询URL匹配的树节点并返回变量
62
59
//
63
60
// Query the tree node with matching URL and return variables
64
- func (P * PreTree ) Query (method , urlPath string ) (isExist bool , rule string , vars map [string ]string ) {
65
- t := treeGroup [method ]
61
+ func (p * PreTree ) Query (method , urlPath string ) (isExist bool , rule string , vars map [string ]string ) {
62
+ t := p . treeGroup [method ]
66
63
isExist , node , vars := t .match (urlPath )
67
64
if isExist {
68
65
return true , node .Rule (), vars
69
66
} else {
70
67
return false , "" , vars
71
68
}
72
-
73
69
}
74
70
75
71
// 前缀树数据结构
0 commit comments