-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.go
51 lines (40 loc) · 917 Bytes
/
route.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
package lrpc
type Route struct {
Method string
Path string
Handler HandlerFunc
Before, After []HandlerFunc
// 可以存储一些类似于权限等信息,会在调用前写入到 local 中
Extra map[string]any
}
type RouteOption func(r *Route)
func RouteWithPrefix(prefix string) RouteOption {
return func(r *Route) {
r.Path = prefix + r.Path
}
}
func RouteWithBefore(before HandlerFunc) RouteOption {
return func(r *Route) {
r.Before = append(r.Before, before)
}
}
func RouteWithAfter(after HandlerFunc) RouteOption {
return func(r *Route) {
r.After = append(r.After, after)
}
}
func RouteWithExtra(extra map[string]any) RouteOption {
return func(r *Route) {
r.Extra = extra
}
}
func RouteWithMergeExtra(merge map[string]any) RouteOption {
return func(r *Route) {
if r.Extra == nil {
r.Extra = make(map[string]any)
}
for k, v := range merge {
r.Extra[k] = v
}
}
}