Skip to content

Commit

Permalink
fixed bugs , that's not match route , such as found '/user/doc' , whe…
Browse files Browse the repository at this point in the history
…n the route extis '/user/:id' node at one root node by mvc attribute route.
  • Loading branch information
yoyofx committed Feb 4, 2024
1 parent 23591c6 commit ede023e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
24 changes: 18 additions & 6 deletions web/endpoints/swagger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package endpoints

import (
"fmt"
"github.com/yoyofx/yoyogo/abstractions"
"github.com/yoyofx/yoyogo/abstractions/xlog"
"github.com/yoyofx/yoyogo/pkg/swagger"
"github.com/yoyofx/yoyogo/web/context"
Expand All @@ -17,11 +19,14 @@ func UseSwaggerDoc(router router.IRouterBuilder, info swagger.Info) {

// swagger.json
router.GET("/swagger.json", func(ctx *context.HttpContext) {
var env *abstractions.HostEnvironment
_ = ctx.RequiredServices.GetService(&env)

openapi := &swagger.OpenApi{
Openapi: "3.1.0",
Paths: make(map[string]map[string]swagger.Path)}
openapi.Info = info
GetSwaggerRouteInfomation(openapi, router)
GetSwaggerRouteInfomation(openapi, router, env)
ctx.JSON(200, openapi)
})

Expand Down Expand Up @@ -65,15 +70,22 @@ func UseSwaggerDoc(router router.IRouterBuilder, info swagger.Info) {
})
}

func GetSwaggerRouteInfomation(openapi *swagger.OpenApi, router router.IRouterBuilder) {
func GetSwaggerRouteInfomation(openapi *swagger.OpenApi, router router.IRouterBuilder, env *abstractions.HostEnvironment) {
builder := router.GetMvcBuilder()
controllerList := builder.GetControllerDescriptorList()
for _, controller := range controllerList {
FilterValidParams(controller, openapi)
FilterValidParams(controller, openapi, env)
}
}

func FilterValidParams(controller mvc.ControllerDescriptor, openapi *swagger.OpenApi) {
func FilterValidParams(controller mvc.ControllerDescriptor, openapi *swagger.OpenApi, env *abstractions.HostEnvironment) {
//serverPath := env.MetaData["server.path"]
mvcTemplate := env.MetaData["mvc.template"]
// mvc
mvcTemplate = strings.ReplaceAll(mvcTemplate, "{controller}", "%s")
mvcTemplate = strings.ReplaceAll(mvcTemplate, "{action}", "%s")
mvcTemplate = fmt.Sprintf("/%s/", mvcTemplate)

suf := len(controller.ControllerName) - 10
controllerName := controller.ControllerName[0:suf]
openapi.Tags = append(openapi.Tags, swagger.Tag{Name: controller.ControllerName, Description: controller.Descriptor})
Expand All @@ -83,8 +95,8 @@ func FilterValidParams(controller mvc.ControllerDescriptor, openapi *swagger.Ope
pathInfo := swagger.Path{}
pathInfo.Tags = []string{controller.ControllerName}
actionName := strings.ReplaceAll(strings.ToLower(act.ActionName), act.ActionMethod, "")
actPath := "/" + controllerName + "/" + actionName

//actPath := "/" + controllerName + "/" + actionName
actPath := fmt.Sprintf(mvcTemplate, controllerName, actionName)
// action params
if len(act.MethodInfo.Parameters) > 0 {
for _, param := range act.MethodInfo.Parameters {
Expand Down
44 changes: 29 additions & 15 deletions web/router/endpointhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package router
import (
"github.com/yoyofx/yoyogo/web/context"
"net/url"
"sort"
"strings"
)

// EndPointRouterHandler node.
type EndPointRouterHandler struct {
// 需要数组排序,Component是否包含:和*,如果包含则放在最后
children []*EndPointRouterHandler
param byte
Component string
Expand All @@ -25,14 +27,14 @@ func (endPoint *EndPointRouterHandler) Invoke(ctx *context.HttpContext, pathComp
}

// Insert a node into the tree.
func (t *EndPointRouterHandler) Insert(method, path string, handler func(ctx *context.HttpContext)) {
t.fullURL = &path
func (endPoint *EndPointRouterHandler) Insert(method, path string, handler func(ctx *context.HttpContext)) {
endPoint.fullURL = &path
components := strings.Split(path, "/")[1:]
Next:
for _, component := range components {
for _, child := range t.children {
for _, child := range endPoint.children {
if child.Component == component {
t = child
endPoint = child
continue Next
}
}
Expand All @@ -44,42 +46,54 @@ Next:
newNode.param = component[0]
}
}
t.children = append(t.children, newNode)
t = newNode
endPoint.children = append(endPoint.children, newNode)
endPoint = newNode
}
t.Methods[method] = handler
endPoint.Methods[method] = handler
}

func (t *EndPointRouterHandler) Match(ctx *context.HttpContext, pathComponents []string) (string, bool) {
node := t.search(pathComponents, ctx.Input.RouterData)
func (endPoint *EndPointRouterHandler) Match(ctx *context.HttpContext, pathComponents []string) (string, bool) {
node := endPoint.search(pathComponents, ctx.Input.RouterData)
if node != nil {
return *node.fullURL, true
}
return "", false
}

// Search the tree.
func (t *EndPointRouterHandler) search(components []string, params url.Values) *EndPointRouterHandler {
func (endPoint *EndPointRouterHandler) search(components []string, params url.Values) *EndPointRouterHandler {
Next:
for cidx, component := range components {
if t.Component == component && cidx == 0 {
if endPoint.Component == component && cidx == 0 {
continue
} else if t.Component != "/" && t.Component != component && cidx == 0 {
} else if endPoint.Component != "/" && endPoint.Component != component && cidx == 0 {
return nil
}
for _, child := range t.children {

sort.Slice(endPoint.children, endPoint.Less)
for _, child := range endPoint.children {

if child.Component == component || child.param == ':' || child.param == '*' {
if child.param == '*' {
return child
}
if child.param == ':' {
params.Add(child.Component[1:], component)
}
t = child
endPoint = child
continue Next
}
}
return nil // not found
}
return t
return endPoint
}

// Less sort by EndPointRouterHandler
// Less function for sort,
func (endPoint *EndPointRouterHandler) Less(i, j int) bool {
if endPoint.children[i].param == ':' || endPoint.children[i].param == '*' {
return false
}
return true
}

0 comments on commit ede023e

Please sign in to comment.