Skip to content

Commit e4d7970

Browse files
committed
resource router仅用于标签和Resource标识
1 parent 8ca5d8c commit e4d7970

File tree

5 files changed

+22
-16
lines changed

5 files changed

+22
-16
lines changed

http/router/httprouter/httprouter.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ func (r *httpRouter) AddProtected(method, path string, h http.HandlerFunc) {
5454
Method: method,
5555
Path: path,
5656
FunctionName: router.GetHandlerFuncName(h),
57-
Protected: true,
5857
Labels: map[string]string{},
58+
Protected: true,
5959
},
6060
h: h,
6161
}
@@ -68,7 +68,6 @@ func (r *httpRouter) AddPublict(method, path string, h http.HandlerFunc) {
6868
Method: method,
6969
Path: path,
7070
FunctionName: router.GetHandlerFuncName(h),
71-
Protected: false,
7271
Labels: map[string]string{},
7372
},
7473
h: h,

http/router/httprouter/httprouter_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,17 @@ func TestAPIRootOrderOK(t *testing.T) {
151151

152152
r.AddProtected("GET", "/test1", IndexHandler)
153153
r.AddProtected("GET", "/test2", IndexHandler)
154-
r.AddProtected("GET", "/test3", IndexHandler)
154+
r.AddPublict("GET", "/test3", IndexHandler)
155155
r.EnableAPIRoot()
156156
r.ServeHTTP(w, req)
157157

158158
es := router.NewEntrySet()
159159
if should.NoError(response.GetDataFromBody(w.Result().Body, es)) {
160-
should.NotNil("/test1", es.Items[0].Path)
161-
should.NotNil("/test2", es.Items[1].Path)
162-
should.NotNil("/test3", es.Items[2].Path)
160+
should.Equal("/test1", es.Items[0].Path)
161+
should.Equal("/test2", es.Items[1].Path)
162+
should.Equal(true, es.Items[1].Protected)
163+
should.Equal("/test3", es.Items[2].Path)
164+
should.Equal(false, es.Items[2].Protected)
165+
163166
}
164167
}

http/router/httprouter/subrouter.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func (r *subRouter) AddProtected(method, path string, h http.HandlerFunc) {
4747
Path: path,
4848
FunctionName: router.GetHandlerFuncName(h),
4949
Labels: map[string]string{},
50+
Protected: true,
5051
},
5152
needAuth: true,
5253
h: h,
@@ -75,11 +76,12 @@ func (r *subRouter) SetLabel(labels ...*router.Label) {
7576
r.labels = append(r.labels, labels...)
7677
}
7778

78-
func (r *subRouter) ResourceRouter(resourceName string) router.SubRouter {
79+
func (r *subRouter) ResourceRouter(resourceName string, labels ...*router.Label) router.SubRouter {
7980
return &subRouter{
8081
resourceName: resourceName,
81-
basePath: r.basePath + "/" + resourceName,
82+
basePath: r.basePath,
8283
root: r.root,
84+
labels: append(r.labels, labels...),
8385
}
8486
}
8587

http/router/httprouter/subrouter_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"github.com/infraboard/mcube/http/router"
99
"github.com/infraboard/mcube/http/router/httprouter"
10-
"github.com/stretchr/testify/require"
10+
"github.com/stretchr/testify/assert"
1111
)
1212

1313
func TestSubRouterTestSuit(t *testing.T) {
@@ -22,14 +22,14 @@ func TestSubRouterTestSuit(t *testing.T) {
2222

2323
func newSubRouterTestSuit(t *testing.T) *subRouterTestSuit {
2424
return &subRouterTestSuit{
25-
should: require.New(t),
25+
should: assert.New(t),
2626
}
2727
}
2828

2929
type subRouterTestSuit struct {
3030
root router.Router
3131
sub router.SubRouter
32-
should *require.Assertions
32+
should *assert.Assertions
3333
}
3434

3535
func (s *subRouterTestSuit) SetUp() {
@@ -69,13 +69,14 @@ func (a *subRouterTestSuit) testAddPublictOK() func(t *testing.T) {
6969

7070
func (a *subRouterTestSuit) testResourceRouterOK() func(t *testing.T) {
7171
return func(t *testing.T) {
72-
a.sub.ResourceRouter("resources").AddPublict("GET", "/", IndexHandler)
72+
a.sub.ResourceRouter("resourceName", router.NewLable("k1", "v1")).AddPublict("GET", "/resources", IndexHandler)
7373

7474
w := httptest.NewRecorder()
75-
req, _ := http.NewRequest("GET", "/v1/resources/", nil)
75+
req, _ := http.NewRequest("GET", "/v1/resources", nil)
7676
a.root.ServeHTTP(w, req)
7777

78-
t.Log(a.root.GetEndpoints())
78+
entry := a.root.GetEndpoints().GetEntry("/v1/resources", "GET")
7979
a.should.Equal(w.Code, 200)
80+
a.should.Equal(entry.Labels["k1"], "v1")
8081
}
8182
}

http/router/router.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type SubRouter interface {
4747
With(m ...Middleware) SubRouter
4848
// SetLabel 设置子路由标签, 作用于Entry上
4949
SetLabel(...*Label)
50-
// ResourceRouter 资源路由器
51-
ResourceRouter(resourceName string) SubRouter
50+
// ResourceRouter 资源路由器, 主要用于设置路由标签和资源名称,
51+
// 方便配置灵活的权限策略
52+
ResourceRouter(resourceName string, labels ...*Label) SubRouter
5253
}

0 commit comments

Comments
 (0)