Skip to content

Commit

Permalink
optimize: router sort
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Apr 26, 2024
1 parent 637e0da commit 9a95dc8
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions cmd/hz/generator/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (routerNode *RouterNode) Update(method *HttpMethod, handlerType, handlerPkg
if paths[0] == "" {
paths = paths[1:]
}
parent, last := routerNode.FindNearest(paths, method.HTTPMethod)
parent, last := routerNode.FindNearest(paths, method.HTTPMethod, sortRouter)
if last == len(paths) {
return fmt.Errorf("path '%s' has been registered", method.Path)
}
Expand Down Expand Up @@ -246,18 +246,21 @@ func getHttpMethod(method string) string {
return strings.ToUpper(method)
}

func (routerNode *RouterNode) FindNearest(paths []string, method string) (*RouterNode, int) {
func (routerNode *RouterNode) FindNearest(paths []string, method string, sortRouter bool) (*RouterNode, int) {
ns := len(paths)
cur := routerNode
i := 0
path := paths[i]
for j := 0; j < len(cur.Children); j++ {
c := cur.Children[j]
tmpMethod := ""
if i == ns { // group do not have http method
tmpMethod := "" // group do not have http method
if i == ns { // only i==ns, the path is http method node
tmpMethod = method
}
if ("/"+path) == c.Path && strings.EqualFold(c.HttpMethod, tmpMethod) {
if ("/" + path) == c.Path {
if sortRouter && strings.EqualFold(c.HttpMethod, tmpMethod) {
continue
}
i++
if i == ns {
return cur, i - 1
Expand Down

0 comments on commit 9a95dc8

Please sign in to comment.