Skip to content

Commit 5831384

Browse files
committed
添加base path
1 parent e4d7970 commit 5831384

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

http/router/httprouter/subrouter.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (r *subRouter) SetLabel(labels ...*router.Label) {
7676
r.labels = append(r.labels, labels...)
7777
}
7878

79-
func (r *subRouter) ResourceRouter(resourceName string, labels ...*router.Label) router.SubRouter {
79+
func (r *subRouter) ResourceRouter(resourceName string, labels ...*router.Label) router.ResourceRouter {
8080
return &subRouter{
8181
resourceName: resourceName,
8282
basePath: r.basePath,
@@ -85,6 +85,10 @@ func (r *subRouter) ResourceRouter(resourceName string, labels ...*router.Label)
8585
}
8686
}
8787

88+
func (r *subRouter) BasePath(path string) {
89+
r.basePath += "/" + path
90+
}
91+
8892
func (r *subRouter) add(e *entry) {
8993
// 子路由全局标签
9094
for i := range r.labels {

http/router/httprouter/subrouter_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ func (a *subRouterTestSuit) testAddPublictOK() func(t *testing.T) {
5656
return func(t *testing.T) {
5757
a.sub.AddPublict("GET", "/", IndexHandler)
5858

59-
t.Log(a.root.GetEndpoints())
60-
6159
w := httptest.NewRecorder()
62-
6360
req, _ := http.NewRequest("GET", "/v1/", nil)
6461
a.root.ServeHTTP(w, req)
6562

@@ -69,13 +66,15 @@ func (a *subRouterTestSuit) testAddPublictOK() func(t *testing.T) {
6966

7067
func (a *subRouterTestSuit) testResourceRouterOK() func(t *testing.T) {
7168
return func(t *testing.T) {
72-
a.sub.ResourceRouter("resourceName", router.NewLable("k1", "v1")).AddPublict("GET", "/resources", IndexHandler)
69+
rr := a.sub.ResourceRouter("resourceName", router.NewLable("k1", "v1"))
70+
rr.BasePath("resources")
71+
rr.AddPublict("GET", "/", IndexHandler)
7372

7473
w := httptest.NewRecorder()
75-
req, _ := http.NewRequest("GET", "/v1/resources", nil)
74+
req, _ := http.NewRequest("GET", "/v1/resources/", nil)
7675
a.root.ServeHTTP(w, req)
7776

78-
entry := a.root.GetEndpoints().GetEntry("/v1/resources", "GET")
77+
entry := a.root.GetEndpoints().GetEntry("/v1/resources/", "GET")
7978
a.should.Equal(w.Code, 200)
8079
a.should.Equal(entry.Labels["k1"], "v1")
8180
}

http/router/router.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,12 @@ type SubRouter interface {
4949
SetLabel(...*Label)
5050
// ResourceRouter 资源路由器, 主要用于设置路由标签和资源名称,
5151
// 方便配置灵活的权限策略
52-
ResourceRouter(resourceName string, labels ...*Label) SubRouter
52+
ResourceRouter(resourceName string, labels ...*Label) ResourceRouter
53+
}
54+
55+
// ResourceRouter 资源路由
56+
type ResourceRouter interface {
57+
SubRouter
58+
// BasePath 设置资源路由的基础路径
59+
BasePath(path string)
5360
}

0 commit comments

Comments
 (0)