Skip to content

Commit

Permalink
regular action
Browse files Browse the repository at this point in the history
  • Loading branch information
joy12825 committed Aug 14, 2023
1 parent 358599e commit 3f81c38
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
19 changes: 17 additions & 2 deletions example/httpserver/swagger/main.go
Expand Up @@ -16,20 +16,35 @@ type HelloAction struct {
type HelloRes struct {
Reply string `dc:"Reply content"`
}
type HelloV2Action struct {
g.Meta `path:"/HelloV2" action:"HelloV2" method:"get" sort:"1"`
Name string `v:"required" dc:"Your name"`
}

type HelloV2Res struct {
Reply string `dc:"Reply content"`
}

type Hello struct{}

func (Hello) Say(ctx context.Context, req *HelloAction) (res *HelloRes, err error) {
g.Log().Debugf(ctx, `receive say: %+v`, req)
res = &HelloRes{
Reply: fmt.Sprintf(`Hi %s`, req.Name),
Reply: fmt.Sprintf(`Hi Say %s`, req.Name),
}
return
}
func (Hello) SayV2(ctx context.Context, req *HelloV2Action) (res *HelloV2Res, err error) {
g.Log().Debugf(ctx, `receive say: %+v`, req)
res = &HelloV2Res{
Reply: fmt.Sprintf(`Hi SayV2 %s`, req.Name),
}
return
}
func MiddlewareTest(r *ghttp.Request) {
fmt.Println("r.Request.Header.Get(\"Content-Type\")")
fmt.Println(r.Request.Header.Get("Content-Type"))
r.Response.Write("ddd")
// r.Response.Write("ddd")
r.Middleware.Next()
}

Expand Down
1 change: 1 addition & 0 deletions net/ghttp/ghttp.go
Expand Up @@ -44,6 +44,7 @@ type (
serveTree map[string]interface{} // The route maps tree.
serveCache *gcache.Cache // Server caches for internal usage.
routesMap map[string][]*HandlerItem // Route map mainly for route dumps and repeated route checks.
actionsMap map[string][]*HandlerItem // Route map mainly for route dumps and repeated route checks.
statusHandlerMap map[string][]HandlerFunc // Custom status handler map.
sessionManager *gsession.Manager // Session manager.
openapi *goai.OpenApiV3 // The OpenApi specification management object.
Expand Down
1 change: 1 addition & 0 deletions net/ghttp/ghttp_server.go
Expand Up @@ -103,6 +103,7 @@ func GetServer(name ...interface{}) *Server {
serveTree: make(map[string]interface{}),
serveCache: gcache.New(),
routesMap: make(map[string][]*HandlerItem),
actionsMap: make(map[string][]*HandlerItem),
openapi: goai.New(),
registrar: gsvc.GetRegistry(),
}
Expand Down
27 changes: 27 additions & 0 deletions net/ghttp/ghttp_server_router.go
Expand Up @@ -164,6 +164,10 @@ func (s *Server) doSetHandler(

// Repeated router checks, this feature can be disabled by server configuration.
var routerKey = s.routerMapKey(handler.HookName, method, uri, 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 @@ -184,6 +188,23 @@ 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 Expand Up @@ -292,6 +313,12 @@ func (s *Server) doSetHandler(

// Append the route.
s.routesMap[routerKey] = append(s.routesMap[routerKey], handler)
if len(actionKey) > 0 {
if _, ok := s.actionsMap[actionKey]; !ok {
s.actionsMap[actionKey] = make([]*HandlerItem, 0)
}
s.actionsMap[actionKey] = append(s.actionsMap[actionKey], handler)
}
}

func (s *Server) isValidMethod(method string) bool {
Expand Down
5 changes: 3 additions & 2 deletions net/ghttp/ghttp_server_router_serve.go
Expand Up @@ -176,7 +176,8 @@ func (s *Server) searchHandlers(method, path, action, domain string) (parsedItem
}
}
if item.Router.Method == defaultMethod || item.Router.Method == method {
if item.Router.Action == action {
if len(item.Router.Action) > 0 && len(action) > 0 && strings.EqualFold(item.Router.Action, action) {

parsedItem := &HandlerItemParsed{item, nil}

switch item.Type {
Expand Down Expand Up @@ -204,7 +205,7 @@ func (s *Server) searchHandlers(method, path, action, domain string) (parsedItem
default:
panic(gerror.Newf(`invalid handler type %s`, item.Type))
}
} else if match, err := gregex.MatchString(item.Router.RegRule, path); err == nil && len(match) > 0 { // Note the rule having no fuzzy rules: len(match) == 1
} else if match, err := gregex.MatchString(item.Router.RegRule, path); err == nil && len(match) > 0 && !strings.EqualFold(item.Router.RegRule, "/") { // Note the rule having no fuzzy rules: len(match) == 1
parsedItem := &HandlerItemParsed{item, nil}
// If the rule contains fuzzy names,
// it needs paring the URL to retrieve the values for the names.
Expand Down

0 comments on commit 3f81c38

Please sign in to comment.