Skip to content

Commit df6e8f8

Browse files
committed
entrySet内置
1 parent d879250 commit df6e8f8

File tree

8 files changed

+115
-127
lines changed

8 files changed

+115
-127
lines changed

http/router/entry.go

Lines changed: 0 additions & 67 deletions
This file was deleted.

http/router/entry_test.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

http/router/httprouter/entriy.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package httprouter
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/infraboard/mcube/http/router"
8+
)
9+
10+
// Entry 路由条目
11+
type entry struct {
12+
*router.Entry
13+
14+
needAuth bool
15+
h http.HandlerFunc
16+
}
17+
18+
func newEntrySet() *entrySet {
19+
return &entrySet{
20+
items: map[string]*entry{},
21+
}
22+
}
23+
24+
// EntrySet 路由集合
25+
type entrySet struct {
26+
items map[string]*entry
27+
}
28+
29+
// AddEntry 添加理由条目
30+
func (s *entrySet) AddEntry(es ...*entry) error {
31+
for _, e := range es {
32+
key := s.ID(e.Path, e.Method)
33+
if _, ok := s.items[key]; ok {
34+
return fmt.Errorf("router entry %s has exist", key)
35+
}
36+
s.items[key] = e
37+
}
38+
39+
return nil
40+
}
41+
42+
// ShowEntries 显示理由条目
43+
func (s *entrySet) ShowEntries() []router.Entry {
44+
entries := make([]router.Entry, 0, len(s.items))
45+
for _, v := range s.items {
46+
entries = append(entries, *v.Entry)
47+
}
48+
49+
return entries
50+
}
51+
52+
// FindEntry 通过函数地址找到对于的路由条目
53+
// 由于该接口是高频接口, 为了不影响性能 采用指针输出, 请不要修改Entry
54+
func (s *entrySet) FindEntry(path, method string) *entry {
55+
id := s.ID(path, method)
56+
entry, ok := s.items[id]
57+
if !ok {
58+
return nil
59+
}
60+
61+
return entry
62+
}
63+
64+
func (s *entrySet) ID(path, mothod string) string {
65+
return path + "." + mothod
66+
}

http/router/httprouter/httprouter.go

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,12 @@ import (
1212
"github.com/julienschmidt/httprouter"
1313
)
1414

15-
// Entry 路由条目
16-
type entry struct {
17-
router.Entry
18-
19-
needAuth bool
20-
h http.HandlerFunc
21-
}
22-
2315
type httpRouter struct {
2416
r *httprouter.Router
2517
l logger.Logger
2618

2719
middlewareChain []router.Middleware
28-
entrySet *router.EntrySet
20+
entrySet *entrySet
2921
auther router.Auther
3022
mergedHandler http.Handler
3123
labels []*router.Label
@@ -36,7 +28,7 @@ type httpRouter struct {
3628
func New() router.Router {
3729
r := &httpRouter{
3830
notFound: http.HandlerFunc(http.NotFound),
39-
entrySet: router.NewEntrySet(),
31+
entrySet: newEntrySet(),
4032
r: &httprouter.Router{
4133
RedirectTrailingSlash: true,
4234
RedirectFixedPath: true,
@@ -58,7 +50,7 @@ func (r *httpRouter) Use(m router.Middleware) {
5850

5951
func (r *httpRouter) AddProtected(method, path string, h http.HandlerFunc) {
6052
e := &entry{
61-
Entry: router.Entry{
53+
Entry: &router.Entry{
6254
Method: method,
6355
Path: path,
6456
Protected: true,
@@ -71,7 +63,7 @@ func (r *httpRouter) AddProtected(method, path string, h http.HandlerFunc) {
7163

7264
func (r *httpRouter) AddPublict(method, path string, h http.HandlerFunc) {
7365
e := &entry{
74-
Entry: router.Entry{
66+
Entry: &router.Entry{
7567
Method: method,
7668
Path: path,
7769
Protected: false,
@@ -105,8 +97,8 @@ func (r *httpRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
10597
r.r.ServeHTTP(response.NewResponse(w), req)
10698
}
10799

108-
func (r *httpRouter) GetEndpoints() *router.EntrySet {
109-
return r.entrySet
100+
func (r *httpRouter) GetEndpoints() []router.Entry {
101+
return r.entrySet.ShowEntries()
110102
}
111103

112104
func (r *httpRouter) EnableAPIRoot() {
@@ -145,38 +137,38 @@ func (r *httpRouter) add(e *entry) {
145137
}
146138

147139
func (r *httpRouter) addHandler(protected bool, method, path string, h http.Handler) {
148-
r.r.Handle(method, path,
149-
func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
150-
entry := r.findEntry(method, path)
151-
if entry == nil {
152-
r.notFound.ServeHTTP(w, req)
140+
wrapper := func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
141+
entry := r.findEntry(method, path)
142+
if entry == nil {
143+
r.notFound.ServeHTTP(w, req)
144+
return
145+
}
146+
// 使用auther进行认证
147+
if protected && r.auther != nil {
148+
authInfo, err := r.auther.Auth(req.Header, *entry.Entry)
149+
if err != nil {
150+
response.Failed(w, err)
153151
return
154152
}
155-
// 使用auther进行认证
156-
if protected && r.auther != nil {
157-
authInfo, err := r.auther.Auth(req.Header, *entry)
158-
if err != nil {
159-
response.Failed(w, err)
160-
return
161-
}
162-
163-
rc := context.GetContext(req)
164-
rc.AuthInfo = authInfo
165-
req = context.WithContext(req, rc)
166-
}
167153

168-
// 未包含和auth为nil
169-
if !protected || r.auther == nil {
170-
rc := &context.ReqContext{
171-
PS: ps,
172-
}
154+
rc := context.GetContext(req)
155+
rc.AuthInfo = authInfo
156+
req = context.WithContext(req, rc)
157+
}
173158

174-
req = context.WithContext(req, rc)
159+
// 未包含和auth为nil
160+
if !protected || r.auther == nil {
161+
rc := &context.ReqContext{
162+
PS: ps,
175163
}
176164

177-
h.ServeHTTP(w, req)
178-
},
179-
)
165+
req = context.WithContext(req, rc)
166+
}
167+
168+
h.ServeHTTP(w, req)
169+
}
170+
171+
r.r.Handle(method, path, wrapper)
180172
}
181173

182174
func (r *httpRouter) addEntry(e *entry) {
@@ -186,12 +178,12 @@ func (r *httpRouter) addEntry(e *entry) {
186178
e.Labels[kv.Key()] = kv.Value()
187179
}
188180

189-
if err := r.entrySet.AddEntry(&e.Entry); err != nil {
181+
if err := r.entrySet.AddEntry(e); err != nil {
190182
panic(err)
191183
}
192184
}
193185

194-
func (r *httpRouter) findEntry(method, path string) *router.Entry {
186+
func (r *httpRouter) findEntry(method, path string) *entry {
195187
hander, paras, _ := r.r.Lookup(method, path)
196188
if hander == nil {
197189
return nil

http/router/httprouter/httprouter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func TestSetLabel(t *testing.T) {
118118
r.SetLabel(router.NewLable("k1", "v1"))
119119
r.AddProtected("GET", "/:id", WithContextHandler)
120120

121-
entries := r.GetEndpoints().ShowEntries()
121+
entries := r.GetEndpoints()
122122

123123
should.Equal(entries[0].Labels["k1"], "v1")
124124
}

http/router/httprouter/subrouter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (r *subRouter) With(m ...router.Middleware) router.SubRouter {
4141

4242
func (r *subRouter) AddProtected(method, path string, h http.HandlerFunc) {
4343
e := &entry{
44-
Entry: router.Entry{
44+
Entry: &router.Entry{
4545
Resource: r.resourceName,
4646
Method: method,
4747
Path: path,
@@ -56,7 +56,7 @@ func (r *subRouter) AddProtected(method, path string, h http.HandlerFunc) {
5656

5757
func (r *subRouter) AddPublict(method, path string, h http.HandlerFunc) {
5858
e := &entry{
59-
Entry: router.Entry{
59+
Entry: &router.Entry{
6060
Resource: r.resourceName,
6161
Method: method,
6262
Path: path,

http/router/httprouter/subrouter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (a *subRouterTestSuit) testSetLabel() func(t *testing.T) {
4646
a.sub.SetLabel(router.NewLable("k1", "v1"))
4747
a.sub.AddPublict("GET", "/index", IndexHandler)
4848

49-
entries := a.root.GetEndpoints().ShowEntries()
49+
entries := a.root.GetEndpoints()
5050

5151
a.should.Equal(entries[0].Labels["k1"], "v1")
5252
}
@@ -75,7 +75,7 @@ func (a *subRouterTestSuit) testResourceRouterOK() func(t *testing.T) {
7575
req, _ := http.NewRequest("GET", "/v1/resources/", nil)
7676
a.root.ServeHTTP(w, req)
7777

78-
t.Log(a.root.GetEndpoints().ShowEntries())
78+
t.Log(a.root.GetEndpoints())
7979
a.should.Equal(w.Code, 200)
8080
}
8181
}

http/router/router.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import (
66
"github.com/infraboard/mcube/logger"
77
)
88

9+
// Entry 路由条目
10+
type Entry struct {
11+
Path string `json:"path,omitempty"`
12+
Method string `json:"method,omitempty"`
13+
Resource string `json:"resource,omitempty"`
14+
Protected bool `json:"protected"`
15+
Labels map[string]string `json:"labels,omitempty"`
16+
}
17+
918
// Router 路由
1019
type Router interface {
1120
// 添加中间件
@@ -21,7 +30,7 @@ type Router interface {
2130
ServeHTTP(http.ResponseWriter, *http.Request)
2231

2332
// 获取当前的路由条目信息
24-
GetEndpoints() *EntrySet
33+
GetEndpoints() []Entry
2534

2635
// EnableAPIRoot 将服务路由表通过路径/暴露出去
2736
EnableAPIRoot()

0 commit comments

Comments
 (0)