Skip to content

Commit

Permalink
fixed bug for attribute route, that problem is access route path not …
Browse files Browse the repository at this point in the history
…match the mvc template path splits of length.
  • Loading branch information
yoyofx committed Jan 24, 2024
1 parent feefadd commit f555af9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
7 changes: 6 additions & 1 deletion examples/simpleweb/contollers/usercontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewUserController(userAction models.IUserAction, sd servicediscovery.IServi
}

type RegisterRequest struct {
mvc.RequestBody `route:"/v1/users/register"`
mvc.RequestBody `route:"/api/users/register"`

UserName string `uri:"userName"`
Password string `uri:"password"`
Expand Down Expand Up @@ -66,6 +66,11 @@ func (controller UserController) GetHtmlBody() actionresult.IActionResult {
})
}

func (controller UserController) GetDoc() mvc.ApiDocResult[string] {

return mvc.ApiDocumentResult[string]().Success().Data("ok").Message("hello").Build()
}

func (controller UserController) GetInfo() mvc.ApiResult {

return controller.OK(controller.userAction.Login("zhang"))
Expand Down
52 changes: 52 additions & 0 deletions web/mvc/api_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,55 @@ func FailWithMsgFunc(data interface{}, fc func() string) ApiResult {
Message: fc(),
}
}

type ApiDocResult[T any] struct {
Success bool
Message string
Data T
Status int
}

type ApiDocResultBuilder[T any] struct {
result *ApiDocResult[T]
}

func (arb *ApiDocResultBuilder[T]) Success() *ApiDocResultBuilder[T] {
arb.result.Status = 200
arb.result.Success = true
return arb
}

func (arb *ApiDocResultBuilder[T]) Fail() *ApiDocResultBuilder[T] {
arb.result.Success = false
return arb
}

func (arb *ApiDocResultBuilder[T]) Message(msg string) *ApiDocResultBuilder[T] {
arb.result.Message = msg
return arb
}

func (arb *ApiDocResultBuilder[T]) MessageWithFunc(fc func() string) *ApiDocResultBuilder[T] {
arb.result.Message = fc()
return arb
}

func (arb *ApiDocResultBuilder[T]) Data(data T) *ApiDocResultBuilder[T] {
arb.result.Data = data
return arb
}

func (arb *ApiDocResultBuilder[T]) StatusCode(statusCode int) *ApiDocResultBuilder[T] {
arb.result.Status = statusCode
return arb
}

func ApiDocumentResult[T any]() *ApiDocResultBuilder[T] {
return &ApiDocResultBuilder[T]{
result: &ApiDocResult[T]{Status: 200},
}
}

func (arb *ApiDocResultBuilder[T]) Build() ApiDocResult[T] {
return *arb.result
}
2 changes: 1 addition & 1 deletion web/mvc/route_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewRouteTemplate(temp string) *RouteTemplate {
}

func (template *RouteTemplate) Match(pathComponents []string, matchinfo *MatchMvcInfo) bool {
if len(pathComponents) >= template.pathLen {
if len(pathComponents) == template.pathLen {
matchinfo.ControllerName = pathComponents[template.GetControllerIndex()]
matchinfo.ControllerName = strings.ToLower(matchinfo.ControllerName)
if !strings.Contains(matchinfo.ControllerName, "controller") {
Expand Down
4 changes: 2 additions & 2 deletions web/mvc/router_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func NewMvcRouterHandler() *RouterHandler {

func (handler *RouterHandler) Invoke(ctx *context.HttpContext, pathComponents []string) func(ctx *context.HttpContext) {
matchInfo := MatchMvcInfo{}
foundRoute := handler.ActionRoutesAttributes.Match(ctx, pathComponents, &matchInfo)
foundRoute := handler.Options.Template.Match(pathComponents, &matchInfo)
if !foundRoute {
foundRoute = handler.Options.Template.Match(pathComponents, &matchInfo)
foundRoute = handler.ActionRoutesAttributes.Match(ctx, pathComponents, &matchInfo)
}
if !foundRoute {
return nil
Expand Down

0 comments on commit f555af9

Please sign in to comment.