Skip to content

Commit 8750bae

Browse files
committed
router: func
1 parent 393ae28 commit 8750bae

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

router.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,68 @@ func (r *router) releaseCtx(ctx *context) {
4141
ctx.requestCtx = nil
4242
r.pool.Put(ctx)
4343
}
44+
45+
func (r *router) handle(method, path string, handlers handlersChain) {
46+
if path == "" {
47+
panic("path is empty")
48+
} else if method == "" {
49+
panic("method is empty")
50+
} else if path[0] != '/' {
51+
panic("path must begin with '/' in path '" + path + "'")
52+
} else if len(handlers) == 0 {
53+
panic("no handlers provided with path '" + path + "'")
54+
}
55+
56+
if r.trees == nil {
57+
r.trees = make(map[string]*node)
58+
}
59+
60+
root := r.trees[method]
61+
if root == nil {
62+
root = createRootNode()
63+
r.trees[method] = root
64+
}
65+
66+
root.addRoute(path, handlers)
67+
}
68+
69+
func (r *router) allowed(reqMethod, path string, ctx *context) string {
70+
var allow string
71+
72+
pathLen := len(path)
73+
74+
if (pathLen == 1 && path[0] == '*') || (pathLen > 1 && path[1] == '*') {
75+
for method := range r.trees {
76+
if method == MethodOptions {
77+
continue
78+
}
79+
80+
if allow != "" {
81+
allow += ", " + method
82+
} else {
83+
allow = method
84+
}
85+
}
86+
return allow
87+
}
88+
89+
for method, tree := range r.trees {
90+
if method == reqMethod || method == MethodOptions {
91+
continue
92+
}
93+
94+
handlers := tree.matchRoute(path, ctx)
95+
if handlers != nil {
96+
if allow != "" {
97+
allow += ", " + method
98+
} else {
99+
allow = method
100+
}
101+
}
102+
}
103+
104+
if len(allow) > 0 {
105+
allow += ", " + MethodOptions
106+
}
107+
return allow
108+
}

0 commit comments

Comments
 (0)