-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.go
64 lines (50 loc) · 1.55 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
package gors
import (
"github.com/gin-gonic/gin"
"golang.org/x/exp/slices"
)
// Route 基础路由,定义一个接口路由,包括method、path和处理器
type Route interface {
Method() string
Path() string
Handler() gin.HandlerFunc
}
type route struct {
method string
path string
handler gin.HandlerFunc
}
func (r *route) Method() string { return r.method }
func (r *route) Path() string { return r.path }
func (r *route) Handler() gin.HandlerFunc { return r.handler }
// RichRoute 富路由,在 Route 基础上,为路由增加了中间件功能。
type RichRoute interface {
Method() string
Path() string
Handlers() []gin.HandlerFunc
}
type richRoute struct {
route Route
middlewares []gin.HandlerFunc
}
func (r *richRoute) Method() string { return r.route.Method() }
func (r *richRoute) Path() string { return r.route.Path() }
func (r *richRoute) Handlers() []gin.HandlerFunc {
return append(slices.Clone(r.middlewares), r.route.Handler())
}
// NewRoute 创建一个路由
func NewRoute(method string, path string, handler gin.HandlerFunc) Route {
return &route{method: method, path: path, handler: handler}
}
// NewRichRoute 创建一个富路由
func NewRichRoute(route Route, middlewares ...gin.HandlerFunc) RichRoute {
return &richRoute{route: route, middlewares: middlewares}
}
// NewRichRoutes 创建一个多个富路由
func NewRichRoutes(routes []Route, middlewares ...gin.HandlerFunc) []RichRoute {
var rs []RichRoute
for _, route := range routes {
rs = append(rs, NewRichRoute(route, middlewares...))
}
return rs
}