58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
|
package web
|
||
|
|
||
|
type routesPath struct {
|
||
|
method string // get|post|put|delete
|
||
|
desc string
|
||
|
permit int
|
||
|
handlers []HandlerFunc
|
||
|
}
|
||
|
|
||
|
type routesNode struct {
|
||
|
path string
|
||
|
methods []*routesPath
|
||
|
child []*routesNode
|
||
|
}
|
||
|
|
||
|
func newRoutesNode(method, path string, desc string, permit int, handlers ...HandlerFunc) *routesNode {
|
||
|
n := &routesNode{
|
||
|
path: path,
|
||
|
}
|
||
|
n.addMethod(method, desc, permit, handlers)
|
||
|
return n
|
||
|
}
|
||
|
|
||
|
func (n *routesNode) addChildren(method, path string, desc string, permit int, handlers ...HandlerFunc) *routesNode {
|
||
|
for _, c := range n.child {
|
||
|
if c.path == path {
|
||
|
c.addMethod(method, desc, permit, handlers)
|
||
|
return c
|
||
|
}
|
||
|
}
|
||
|
|
||
|
children := newRoutesNode(method, path, desc, permit, handlers)
|
||
|
n.child = append(n.child, children)
|
||
|
return children
|
||
|
}
|
||
|
|
||
|
func (n *routesNode) addMethod(method string, desc string, permit int, handlers ...HandlerFunc) *routesNode {
|
||
|
n.methods = append(n.methods, &routesPath{method: method, desc: desc, permit: permit, handlers: handlers})
|
||
|
return n
|
||
|
}
|
||
|
|
||
|
func (n *routesNode) travel(parentPath string, parentDesc string, travelFun func(parentDesc string, path string, method string, handlers ...HandlerFunc)) {
|
||
|
curPath := parentPath + n.path
|
||
|
curNodeDesc := ""
|
||
|
if len(n.methods) > 0 {
|
||
|
for _, method := range n.methods {
|
||
|
if method.method == "" {
|
||
|
curNodeDesc = method.desc
|
||
|
continue
|
||
|
}
|
||
|
travelFun(curPath, parentDesc, method.method, method.handlers)
|
||
|
}
|
||
|
}
|
||
|
for _, c := range n.child {
|
||
|
c.travel(curPath, curNodeDesc, travelFun)
|
||
|
}
|
||
|
}
|