-
Notifications
You must be signed in to change notification settings - Fork 44
/
subrouter.go
163 lines (137 loc) · 3.82 KB
/
subrouter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package httprouter
import (
"fmt"
"net/http"
"path"
"github.com/infraboard/mcube/v2/http/router"
httppb "github.com/infraboard/mcube/v2/pb/http"
)
func newSubRouter(basePath string, root *httpRouter) *subRouter {
return &subRouter{
basePath: basePath,
root: root,
authEnable: root.authEnable,
permissionEnable: root.permissionEnable,
auditLog: root.auditLog,
requiredNamespace: root.requiredNamespace,
}
}
type subRouter struct {
basePath string
resourceName string
resourceBasePath string
root *httpRouter
labels []*httppb.Label
middlewareChain []router.Middleware
authEnable bool
permissionEnable bool
allow []string
auditLog bool
requiredNamespace bool
}
func (r *subRouter) Use(m router.Middleware) {
r.middlewareChain = append(r.middlewareChain, m)
}
func (r *subRouter) With(m ...router.Middleware) router.SubRouter {
// 继承原有的中间件
sr := &subRouter{
basePath: r.basePath,
root: r.root,
middlewareChain: r.middlewareChain,
}
// 添加新中间件
sr.middlewareChain = append(sr.middlewareChain, m...)
return sr
}
func (r *subRouter) Handle(method, path string, h http.HandlerFunc) httppb.EntryDecorator {
e := &entry{
Entry: &httppb.Entry{
Resource: r.resourceName,
Method: method,
Path: path,
FunctionName: router.GetHandlerFuncName(h),
Labels: map[string]string{},
AuthEnable: r.authEnable,
PermissionEnable: r.permissionEnable,
AuditLog: r.auditLog,
Allow: r.allow,
RequiredNamespace: r.requiredNamespace,
},
h: h,
}
r.add(e)
return e.Entry
}
func (r *subRouter) SetLabel(labels ...*httppb.Label) {
r.labels = append(r.labels, labels...)
}
func (r *subRouter) Auth(isEnable bool) {
r.authEnable = isEnable
}
func (r *subRouter) Permission(isEnable bool) {
r.permissionEnable = isEnable
}
func (r *subRouter) Allow(targets ...fmt.Stringer) {
for i := range targets {
r.allow = append(r.allow, targets[i].String())
}
}
func (r *subRouter) AuditLog(isEnable bool) {
r.auditLog = isEnable
}
func (r *subRouter) RequiredNamespace(isEnable bool) {
r.requiredNamespace = isEnable
}
func (r *subRouter) ResourceRouter(resourceName string, labels ...*httppb.Label) router.ResourceRouter {
return &subRouter{
resourceName: resourceName,
resourceBasePath: r.basePath,
basePath: r.basePath,
root: r.root,
labels: append(r.labels, labels...),
authEnable: r.authEnable,
permissionEnable: r.permissionEnable,
auditLog: r.auditLog,
allow: r.allow,
requiredNamespace: r.requiredNamespace,
}
}
func (r *subRouter) BasePath(path string) {
r.basePath = r.resourceBasePath + "/" + path
}
func (r *subRouter) add(e *entry) {
// 子路由全局标签
for i := range r.labels {
kv := r.labels[i]
e.Labels[kv.Key()] = kv.Value()
}
mergedHandler := r.combineHandler(e)
e.Path = r.calculateAbsolutePath(e.Path)
r.root.addHandler(e.Method, e.Path, mergedHandler)
r.root.addEntry(e)
}
func (r *subRouter) combineHandler(e *entry) http.Handler {
var mergedHandler http.Handler
mergedHandler = http.HandlerFunc(e.h)
for i := len(r.middlewareChain) - 1; i >= 0; i-- {
mergedHandler = r.middlewareChain[i].Handler(mergedHandler)
}
return mergedHandler
}
func (r *subRouter) calculateAbsolutePath(relativePath string) string {
if relativePath == "" {
return r.basePath
}
finalPath := path.Join(r.basePath, relativePath)
appendSlash := lastChar(relativePath) == '/' && lastChar(finalPath) != '/'
if appendSlash {
return finalPath + "/"
}
return finalPath
}
func lastChar(str string) uint8 {
if str == "" {
panic("The length of the string can't be 0")
}
return str[len(str)-1]
}