Skip to content

Commit

Permalink
fix: user must provide route path at least in one way
Browse files Browse the repository at this point in the history
  • Loading branch information
tangx committed Dec 14, 2021
1 parent 8fa875e commit af8d5d5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
3 changes: 2 additions & 1 deletion internal/example/apis/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
type Index struct {
// httpx.MethodAny `path:"/index.any"`
httpx.MethodMulti `path:"/index" methods:"GET,HEAD"`
Name string `query:"name"`
// httpx.MethodMulti `path:"" methods:"GET,HEAD"`
Name string `query:"name"`
}

func (index *Index) Output(c *gin.Context) (interface{}, error) {
Expand Down
2 changes: 1 addition & 1 deletion rum/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (e *Engine) ListenAndServe(addrs ...string) error {
// Shutdown 用户自定义退出。 如果不是使用 ListenAndServe 启动则不能使用。
func (e *Engine) Shutdown(ctx context.Context) error {
if e.srv == nil {
return errors.New(NotRunWithListenAndServe)
return errors.New(Err_NotRunWithListenAndServe)
}

return e.srv.Shutdown(ctx)
Expand Down
6 changes: 4 additions & 2 deletions rum/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package rum

var (
NotRunWithListenAndServe = "rum not run with ListenAndServe method"
BindingRequestError = "binding request params failed"
Err_NotRunWithListenAndServe = "rum not run with ListenAndServe method"
Err_BindingRequest = "binding request params failed"

Err_NoPathProvide = "no route path provide"
)
22 changes: 16 additions & 6 deletions rum/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,25 @@ func (r *RouterGroup) use(op Operator) bool {
// handle 添加路由
func (r *RouterGroup) hanlde(op Operator) bool {

// 通过反射获取 path
path := routePath(op)
path := ""

// 通过断言接口获取 path
if path == "" {
h := op.(PathOperator)
// user path method first. 通过断言接口获取 path
if h, ok := op.(PathOperator); path == "" && ok {
path = h.Path()
}

// if path is not provided by Path method, emtpy.
// try to get path value from tag by reflection
// user must provide a path value at least one ways
if path == "" {
p, err := routePath(op)
if err != nil {
panic(err)
}

path = p
}

mop := op.(MethodOperator)

for _, method := range httpx.UnmarshalMethods(mop.Method()) {
Expand All @@ -132,7 +142,7 @@ func (r *RouterGroup) handlerfunc(op Operator) HandlerFunc {

err := ginbinder.ShouldBindRequest(c, op)
if err != nil {
err = statuserrors.Wrap(err, http.StatusBadRequest, BindingRequestError)
err = statuserrors.Wrap(err, http.StatusBadRequest, Err_BindingRequest)
r.output(c, nil, err)
return
}
Expand Down
7 changes: 4 additions & 3 deletions rum/group_utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package rum

import (
"fmt"
"reflect"
)

func routePath(v interface{}) string {
func routePath(v interface{}) (string, error) {
rv := reflect.ValueOf(v)
rv = reflect.Indirect(rv)

Expand Down Expand Up @@ -34,9 +35,9 @@ func routePath(v interface{}) string {

// 寻找路径
if val, ok := ft.Tag.Lookup("path"); ok {
return val
return val, nil
}

}
return ""
return "", fmt.Errorf(Err_NoPathProvide)
}

0 comments on commit af8d5d5

Please sign in to comment.