Skip to content

Commit

Permalink
feat: 支持多路由方法
Browse files Browse the repository at this point in the history
  • Loading branch information
tangx committed Dec 9, 2021
1 parent b0e501d commit bf31c58
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
14 changes: 11 additions & 3 deletions internal/example/apis/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@ import (

"github.com/gin-gonic/gin"
"github.com/go-jarvis/rum-gonic/internal/example/injector/redis"
"github.com/go-jarvis/rum-gonic/pkg/httpx"
)

type Index struct {
httpx.MethodGet `path:"/index"`
Name string `query:"name"`
// httpx.MethodGet `path:"/index"`

// httpx.MethodRoutes `path:"/index"`
Name string `query:"name"`
}

// func (index *Index) Method() string {
// return "GET,HEAD"
// }
// func (index *Index) Path() string {
// return "/index"
// }

func (index *Index) Output(c *gin.Context) (interface{}, error) {

return logic(c, index), nil
Expand Down
20 changes: 19 additions & 1 deletion pkg/httpx/method.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package httpx

import net_http "net/http"
import (
net_http "net/http"
"strings"
)

type Method struct {
}
Expand Down Expand Up @@ -58,3 +61,18 @@ type MethodTrace struct{}
func (MethodTrace) Method() string {
return net_http.MethodTrace
}

type MethodMulti struct {
Routes []string
}

func (m MethodMulti) Method() string {
return MarshalMethods(m.Routes)
}

func MarshalMethods(ms []string) string {
return strings.Join(ms, ",")
}
func UnmarshalMethods(str string) []string {
return strings.Split(str, ",")
}
8 changes: 7 additions & 1 deletion rum/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package rum

import (
"net/http"
"strings"

"github.com/gin-gonic/gin"
"github.com/go-jarvis/rum-gonic/pkg/httpx"
"github.com/tangx/ginbinder"
)

Expand Down Expand Up @@ -110,7 +112,11 @@ func (r *RouterGroup) hanlde(op Operator) bool {

mop := op.(MethodOperator)

r.ginRG.Handle(mop.Method(), path, r.handlerfunc(op))
for _, method := range httpx.UnmarshalMethods(mop.Method()) {
method = strings.TrimSpace(method)
r.ginRG.Handle(method, path, r.handlerfunc(op))
}

return true
}

Expand Down

0 comments on commit bf31c58

Please sign in to comment.