forked from storezhang/echox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
group.go
93 lines (72 loc) · 2.85 KB
/
group.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
package echox
import (
`net/http`
`github.com/labstack/echo/v4`
)
// Group 分组,模拟echo.Group,并增加Context转换
// 使用代理设计模式
type Group struct {
proxy *echo.Group
}
func (g *Group) Use(middlewares ...MiddlewareFunc) {
g.proxy.Use(parseMiddlewares(middlewares...)...)
}
func (g *Group) Connect(path string, handler handlerFunc, middlewares ...MiddlewareFunc) *Route {
return g.Add(http.MethodConnect, path, handler, middlewares...)
}
func (g *Group) Delete(path string, handler handlerFunc, middlewares ...MiddlewareFunc) *Route {
return g.Add(http.MethodDelete, path, handler, middlewares...)
}
func (g *Group) Get(path string, handler handlerFunc, middlewares ...MiddlewareFunc) *Route {
return g.Add(http.MethodGet, path, handler, middlewares...)
}
func (g *Group) Head(path string, handler handlerFunc, middlewares ...MiddlewareFunc) *Route {
return g.Add(http.MethodHead, path, handler, middlewares...)
}
func (g *Group) Options(path string, handler handlerFunc, middlewares ...MiddlewareFunc) *Route {
return g.Add(http.MethodOptions, path, handler, middlewares...)
}
func (g *Group) Patch(path string, handler handlerFunc, middlewares ...MiddlewareFunc) *Route {
return g.Add(http.MethodPatch, path, handler, middlewares...)
}
func (g *Group) Post(path string, handler handlerFunc, middlewares ...MiddlewareFunc) *Route {
return g.Add(http.MethodPost, path, handler, middlewares...)
}
func (g *Group) Put(path string, handler handlerFunc, middlewares ...MiddlewareFunc) *Route {
return g.Add(http.MethodPut, path, handler, middlewares...)
}
func (g *Group) Trace(path string, handler handlerFunc, middlewares ...MiddlewareFunc) *Route {
return g.Add(http.MethodTrace, path, handler, middlewares...)
}
func (g *Group) Any(path string, handler handlerFunc, middlewares ...MiddlewareFunc) (routes []*Route) {
routes = make([]*Route, len(methods))
for index, method := range methods {
routes[index] = g.Add(method, path, handler, middlewares...)
}
return
}
func (g *Group) Match(methods []string, path string, handler handlerFunc, middlewares ...MiddlewareFunc) (routes []*Route) {
routes = make([]*Route, len(methods))
for index, method := range methods {
routes[index] = g.Add(method, path, handler, middlewares...)
}
return
}
func (g *Group) Group(prefix string, middlewares ...MiddlewareFunc) (ag *Group) {
return &Group{
proxy: g.proxy.Group(prefix, parseMiddlewares(middlewares...)...),
}
}
func (g *Group) Static(prefix string, root string) {
g.proxy.Static(prefix, root)
}
func (g *Group) File(path string, file string) {
g.proxy.File(path, file)
}
func (g *Group) Add(method string, path string, handler handlerFunc, middlewares ...MiddlewareFunc) *Route {
return &Route{
Route: g.proxy.Add(method, path, func(ctx echo.Context) error {
return handler(parseContext(ctx))
}, parseMiddlewares(middlewares...)...),
}
}