Skip to content

Commit 3f86b20

Browse files
committed
添加EntryDecorator
1 parent c0c474b commit 3f86b20

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

http/router/entry.go

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ type Entry struct {
1010
Labels map[string]string `json:"labels,omitempty"`
1111
}
1212

13+
// AddLabel 添加Label
14+
func (e *Entry) AddLabel(labels ...*Label) {
15+
for i := range labels {
16+
e.Labels[labels[i].Key()] = labels[i].Value()
17+
}
18+
}
19+
1320
// NewEntrySet 实例
1421
func NewEntrySet() *EntrySet {
1522
return &EntrySet{}

http/router/httprouter/subrouter.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (r *subRouter) With(m ...router.Middleware) router.SubRouter {
4040
return sr
4141
}
4242

43-
func (r *subRouter) AddProtected(method, path string, h http.HandlerFunc) {
43+
func (r *subRouter) AddProtected(method, path string, h http.HandlerFunc) router.EntryDecorator {
4444
e := &entry{
4545
Entry: &router.Entry{
4646
Resource: r.resourceName,
@@ -55,9 +55,12 @@ func (r *subRouter) AddProtected(method, path string, h http.HandlerFunc) {
5555
}
5656

5757
r.add(e)
58+
59+
new := *r
60+
return &new
5861
}
5962

60-
func (r *subRouter) AddPublict(method, path string, h http.HandlerFunc) {
63+
func (r *subRouter) AddPublict(method, path string, h http.HandlerFunc) router.EntryDecorator {
6164
e := &entry{
6265
Entry: &router.Entry{
6366
Resource: r.resourceName,
@@ -69,12 +72,15 @@ func (r *subRouter) AddPublict(method, path string, h http.HandlerFunc) {
6972
needAuth: false,
7073
h: h,
7174
}
72-
7375
r.add(e)
76+
77+
new := *r
78+
return &new
7479
}
7580

76-
func (r *subRouter) SetLabel(labels ...*router.Label) {
81+
func (r *subRouter) SetLabel(labels ...*router.Label) router.EntryDecorator {
7782
r.labels = append(r.labels, labels...)
83+
return r
7884
}
7985

8086
func (r *subRouter) ResourceRouter(resourceName string, labels ...*router.Label) router.ResourceRouter {

http/router/httprouter/subrouter_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func TestSubRouterTestSuit(t *testing.T) {
1919
t.Run("AddPublictOK", suit.testAddPublictOK())
2020
t.Run("ResourceRouterOK", suit.testResourceRouterOK())
2121
t.Run("WithParamsOK", suit.testWithParams())
22+
t.Run("SetEntryLabel", suit.testSetLabelWithEntry())
2223
}
2324

2425
func newSubRouterTestSuit(t *testing.T) *subRouterTestSuit {
@@ -94,3 +95,14 @@ func (a *subRouterTestSuit) testWithParams() func(t *testing.T) {
9495
should.Equal(200, w.Code)
9596
}
9697
}
98+
99+
func (a *subRouterTestSuit) testSetLabelWithEntry() func(t *testing.T) {
100+
return func(t *testing.T) {
101+
label := router.NewLable("k1", "v1")
102+
a.sub.AddPublict("GET", "/index/entry/label", IndexHandler).SetLabel(label)
103+
104+
es := a.root.GetEndpoints()
105+
106+
a.should.Equal(es.GetEntry("/v1/index/entry/label", "GET").Labels["k1"], "v1")
107+
}
108+
}

http/router/router.go

+17-13
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,30 @@ type Router interface {
3535
SubRouter(basePath string) SubRouter
3636
}
3737

38+
// ResourceRouter 资源路由
39+
type ResourceRouter interface {
40+
SubRouter
41+
// BasePath 设置资源路由的基础路径
42+
BasePath(path string)
43+
}
44+
3845
// SubRouter 子路由或者分组路由
3946
type SubRouter interface {
47+
EntryDecorator
4048
// 添加中间件
4149
Use(m Middleware)
42-
// 添加受认证保护的路由
43-
AddProtected(method, path string, h http.HandlerFunc)
44-
// 添加公开路由, 所有人都可以访问
45-
AddPublict(method, path string, h http.HandlerFunc)
4650
// With独立作用于某一个Handler
4751
With(m ...Middleware) SubRouter
48-
// SetLabel 设置子路由标签, 作用于Entry上
49-
SetLabel(...*Label)
50-
// ResourceRouter 资源路由器, 主要用于设置路由标签和资源名称,
51-
// 方便配置灵活的权限策略
52+
// 添加受认证保护的路由
53+
AddProtected(method, path string, h http.HandlerFunc) EntryDecorator
54+
// 添加公开路由, 所有人都可以访问
55+
AddPublict(method, path string, h http.HandlerFunc) EntryDecorator
56+
// ResourceRouter 资源路由器, 主要用于设置路由标签和资源名称,方便配置灵活的权限策略
5257
ResourceRouter(resourceName string, labels ...*Label) ResourceRouter
5358
}
5459

55-
// ResourceRouter 资源路由
56-
type ResourceRouter interface {
57-
SubRouter
58-
// BasePath 设置资源路由的基础路径
59-
BasePath(path string)
60+
// EntryDecorator 装饰
61+
type EntryDecorator interface {
62+
// SetLabel 设置子路由标签, 作用于Entry上
63+
SetLabel(...*Label) EntryDecorator
6064
}

0 commit comments

Comments
 (0)