Skip to content

Commit

Permalink
添加EntryDecorator
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaojun03 committed Mar 11, 2020
1 parent c0c474b commit 3f86b20
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
7 changes: 7 additions & 0 deletions http/router/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ type Entry struct {
Labels map[string]string `json:"labels,omitempty"`
}

// AddLabel 添加Label
func (e *Entry) AddLabel(labels ...*Label) {
for i := range labels {
e.Labels[labels[i].Key()] = labels[i].Value()
}
}

// NewEntrySet 实例
func NewEntrySet() *EntrySet {
return &EntrySet{}
Expand Down
14 changes: 10 additions & 4 deletions http/router/httprouter/subrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r *subRouter) With(m ...router.Middleware) router.SubRouter {
return sr
}

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

r.add(e)

new := *r
return &new
}

func (r *subRouter) AddPublict(method, path string, h http.HandlerFunc) {
func (r *subRouter) AddPublict(method, path string, h http.HandlerFunc) router.EntryDecorator {
e := &entry{
Entry: &router.Entry{
Resource: r.resourceName,
Expand All @@ -69,12 +72,15 @@ func (r *subRouter) AddPublict(method, path string, h http.HandlerFunc) {
needAuth: false,
h: h,
}

r.add(e)

new := *r
return &new
}

func (r *subRouter) SetLabel(labels ...*router.Label) {
func (r *subRouter) SetLabel(labels ...*router.Label) router.EntryDecorator {
r.labels = append(r.labels, labels...)
return r
}

func (r *subRouter) ResourceRouter(resourceName string, labels ...*router.Label) router.ResourceRouter {
Expand Down
12 changes: 12 additions & 0 deletions http/router/httprouter/subrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestSubRouterTestSuit(t *testing.T) {
t.Run("AddPublictOK", suit.testAddPublictOK())
t.Run("ResourceRouterOK", suit.testResourceRouterOK())
t.Run("WithParamsOK", suit.testWithParams())
t.Run("SetEntryLabel", suit.testSetLabelWithEntry())
}

func newSubRouterTestSuit(t *testing.T) *subRouterTestSuit {
Expand Down Expand Up @@ -94,3 +95,14 @@ func (a *subRouterTestSuit) testWithParams() func(t *testing.T) {
should.Equal(200, w.Code)
}
}

func (a *subRouterTestSuit) testSetLabelWithEntry() func(t *testing.T) {
return func(t *testing.T) {
label := router.NewLable("k1", "v1")
a.sub.AddPublict("GET", "/index/entry/label", IndexHandler).SetLabel(label)

es := a.root.GetEndpoints()

a.should.Equal(es.GetEntry("/v1/index/entry/label", "GET").Labels["k1"], "v1")
}
}
30 changes: 17 additions & 13 deletions http/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,30 @@ type Router interface {
SubRouter(basePath string) SubRouter
}

// ResourceRouter 资源路由
type ResourceRouter interface {
SubRouter
// BasePath 设置资源路由的基础路径
BasePath(path string)
}

// SubRouter 子路由或者分组路由
type SubRouter interface {
EntryDecorator
// 添加中间件
Use(m Middleware)
// 添加受认证保护的路由
AddProtected(method, path string, h http.HandlerFunc)
// 添加公开路由, 所有人都可以访问
AddPublict(method, path string, h http.HandlerFunc)
// With独立作用于某一个Handler
With(m ...Middleware) SubRouter
// SetLabel 设置子路由标签, 作用于Entry上
SetLabel(...*Label)
// ResourceRouter 资源路由器, 主要用于设置路由标签和资源名称,
// 方便配置灵活的权限策略
// 添加受认证保护的路由
AddProtected(method, path string, h http.HandlerFunc) EntryDecorator
// 添加公开路由, 所有人都可以访问
AddPublict(method, path string, h http.HandlerFunc) EntryDecorator
// ResourceRouter 资源路由器, 主要用于设置路由标签和资源名称,方便配置灵活的权限策略
ResourceRouter(resourceName string, labels ...*Label) ResourceRouter
}

// ResourceRouter 资源路由
type ResourceRouter interface {
SubRouter
// BasePath 设置资源路由的基础路径
BasePath(path string)
// EntryDecorator 装饰
type EntryDecorator interface {
// SetLabel 设置子路由标签, 作用于Entry上
SetLabel(...*Label) EntryDecorator
}

0 comments on commit 3f86b20

Please sign in to comment.