Skip to content

Commit

Permalink
fixed action,add group
Browse files Browse the repository at this point in the history
  • Loading branch information
joy12825 committed Oct 12, 2023
1 parent 010c0e6 commit 69d28ed
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 44 deletions.
2 changes: 1 addition & 1 deletion example/httpserver/swagger/main.go
Expand Up @@ -17,7 +17,7 @@ type HelloRes struct {
Reply string `dc:"Reply content"`
}
type HelloV2Action struct {
g.Meta `action:"Hello2" method:"get" sort:"1"`
g.Meta `group:"system" action:"Hello" method:"get" sort:"1"`
Name string `v:"required" dc:"Your name"`
}

Expand Down
54 changes: 11 additions & 43 deletions net/ghttp/ghttp_server_router.go
Expand Up @@ -39,28 +39,27 @@ func (s *Server) routerMapKey(hook HookName, method, group, action, domain strin

// parsePattern parses the given pattern to domain, method and path variable.
func (s *Server) parsePattern(pattern string) (domain, method, path, action string, err error) {
// path = strings.TrimSpace(pattern)
path = strings.TrimSpace(pattern)
domain = DefaultDomainName
method = defaultMethod
if array, err := gregex.MatchString(`([a-zA-Z]+):(.+)`, pattern); len(array) > 1 && err == nil {
// path = strings.TrimSpace(array[2])
path = strings.TrimSpace(array[2])
if v := strings.TrimSpace(array[1]); v != "" {
method = v
}
}
if array, err := gregex.MatchString(`(.+)@([\w\.\-]+)`, path); len(array) > 1 && err == nil {
// path = strings.TrimSpace(array[1])
path = strings.TrimSpace(array[1])
if v := strings.TrimSpace(array[2]); v != "" {
domain = v
}
}
// if path == "" {
//err = gerror.NewCode(gcode.CodeInvalidParameter, "invalid pattern: URI should not be empty")
// }
path = "default"
//if path != "/" {
// path = strings.TrimRight(path, "/")
//}
if path == "" {
err = gerror.NewCode(gcode.CodeInvalidParameter, "invalid pattern: URI should not be empty")
}
if path != "/" {
path = strings.TrimRight(path, "/")
}
return
}

Expand Down Expand Up @@ -100,6 +99,8 @@ func (s *Server) setHandler(ctx context.Context, in setHandlerInput) {
var objectReq = reflect.New(handler.Info.Type.In(1))
if v := gmeta.Get(objectReq, gtag.Group); !v.IsEmpty() {
group = v.String()
} else {
group = "default"
}
if v := gmeta.Get(objectReq, gtag.Action); !v.IsEmpty() {
action = v.String()
Expand Down Expand Up @@ -147,25 +148,9 @@ func (s *Server) doSetHandler(
method, supportedHttpMethods, defaultMethod,
)
}
// Prefix for URI feature.
//if prefix != "" {
// uri = prefix + "/" + strings.TrimLeft(uri, "/")
//}
//uri = strings.TrimRight(uri, "/")
//if group == "" {
// group = "default"
//}
//
//if len(uri) == 0 || uri[0] != '/' {
// s.Logger().Fatalf(ctx, `invalid pattern "%s", URI should lead with '/'`, pattern)
//}

// Repeated router checks, this feature can be disabled by server configuration.
var routerKey = s.routerMapKey(handler.HookName, method, group, action, domain)
//var actionKey string
//if len(action) > 0 {
// actionKey = s.routerMapKey(handler.HookName, "", "/", action, domain)
//}
if !s.config.RouteOverWrite {
switch handler.Type {
case HandlerTypeHandler, HandlerTypeObject:
Expand All @@ -186,23 +171,6 @@ func (s *Server) doSetHandler(
)
}
}
//if items, ok := s.actionsMap[actionKey]; ok {
// var duplicatedHandler *HandlerItem
// for i, item := range items {
// switch item.Type {
// case HandlerTypeHandler, HandlerTypeObject:
// duplicatedHandler = items[i]
// break
// }
// }
// if duplicatedHandler != nil {
// s.Logger().Fatalf(
// ctx,
// `duplicated action registry "%s" at %s , already registered at %s`,
// pattern, handler.Source, duplicatedHandler.Source,
// )
// }
//}
}
}
// Unique id for each handler.
Expand Down

0 comments on commit 69d28ed

Please sign in to comment.