-
Notifications
You must be signed in to change notification settings - Fork 11
/
group.go
198 lines (170 loc) · 4.94 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package echo
type Group struct {
host *host
prefix string
middleware []interface{}
echo *Echo
meta H
}
func (g *Group) URL(h interface{}, params ...interface{}) string {
return g.echo.URL(h, params...)
}
func (g *Group) SetAlias(alias string) *Group {
if g.host != nil {
g.host.alias = alias
for a, v := range g.echo.hostAlias {
if v == g.host.name {
delete(g.echo.hostAlias, a)
}
}
if len(alias) > 0 {
g.echo.hostAlias[alias] = g.host.name
}
}
return g
}
func (g *Group) Alias(alias string) Hoster {
if name, ok := g.echo.hostAlias[alias]; ok {
hs, ok := g.echo.hosts[name]
if !ok || hs == nil || hs.group == nil {
return nil
}
return hs.group.host
}
return nil
}
func (g *Group) SetRenderer(r Renderer) {
g.echo.renderer = r
}
func (g *Group) Use(middleware ...interface{}) {
for _, m := range middleware {
g.echo.ValidMiddleware(m)
g.middleware = append(g.middleware, m)
if g.echo.MiddlewareDebug {
g.echo.logger.Debugf(`Middleware[Use](%p): [%s] -> %s`, m, g.prefix, HandlerName(m))
}
}
}
// Pre adds handler to the middleware chain.
func (g *Group) Pre(middleware ...interface{}) {
var middlewares []interface{}
for _, m := range middleware {
g.echo.ValidMiddleware(m)
middlewares = append(middlewares, m)
if g.echo.MiddlewareDebug {
g.echo.logger.Debugf(`Middleware[Pre](%p): [%s] -> %s`, m, g.prefix, HandlerName(m))
}
}
g.middleware = append(middlewares, g.middleware...)
}
func (g *Group) Connect(path string, h interface{}, m ...interface{}) IRouter {
return g.Add(CONNECT, path, h, m...)
}
func (g *Group) Delete(path string, h interface{}, m ...interface{}) IRouter {
return g.Add(DELETE, path, h, m...)
}
func (g *Group) Get(path string, h interface{}, m ...interface{}) IRouter {
return g.Add(GET, path, h, m...)
}
func (g *Group) Head(path string, h interface{}, m ...interface{}) IRouter {
return g.Add(HEAD, path, h, m...)
}
func (g *Group) Options(path string, h interface{}, m ...interface{}) IRouter {
return g.Add(OPTIONS, path, h, m...)
}
func (g *Group) Patch(path string, h interface{}, m ...interface{}) IRouter {
return g.Add(PATCH, path, h, m...)
}
func (g *Group) Post(path string, h interface{}, m ...interface{}) IRouter {
return g.Add(POST, path, h, m...)
}
func (g *Group) Put(path string, h interface{}, m ...interface{}) IRouter {
return g.Add(PUT, path, h, m...)
}
func (g *Group) Trace(path string, h interface{}, m ...interface{}) IRouter {
return g.Add(TRACE, path, h, m...)
}
func (g *Group) Any(path string, h interface{}, middleware ...interface{}) IRouter {
routes := Routes{}
for _, m := range methods {
routes = append(routes, g.Add(m, path, h, middleware...))
}
return routes
}
func (g *Group) Route(methods string, path string, h interface{}, middleware ...interface{}) IRouter {
return g.Match(splitHTTPMethod.Split(methods, -1), path, h, middleware...)
}
func (g *Group) Match(methods []string, path string, h interface{}, middleware ...interface{}) IRouter {
routes := Routes{}
for _, m := range methods {
routes = append(routes, g.Add(m, path, h, middleware...))
}
return routes
}
func (g *Group) Group(prefix string, middleware ...interface{}) *Group {
m := []interface{}{}
m = append(m, g.middleware...)
m = append(m, middleware...)
if g.host != nil {
subG, y := g.echo.hosts[g.host.name].groups[prefix]
if !y {
subG = &Group{host: g.host, prefix: prefix, echo: g.echo, meta: H{}}
g.echo.hosts[g.host.name].groups[prefix] = subG
if len(g.meta) > 0 {
subG.meta.DeepMerge(g.meta)
}
}
if len(m) > 0 {
subG.Use(m...)
}
return subG
}
return g.echo.subgroup(g, prefix, m...)
}
// Static implements `Echo#Static()` for sub-routes within the Group.
func (g *Group) Static(prefix, root string) {
static(g, prefix, root)
}
// File implements `Echo#File()` for sub-routes within the Group.
func (g *Group) File(path, file string) {
g.echo.File(g.prefix+path, file)
}
func (g *Group) Prefix() string {
return g.prefix
}
func (g *Group) Echo() *Echo {
return g.echo
}
// MetaHandler Add meta information about endpoint
func (g *Group) MetaHandler(m H, handler interface{}, requests ...interface{}) Handler {
return g.echo.MetaHandler(m, handler, requests...)
}
func (g *Group) Add(method, path string, h interface{}, middleware ...interface{}) *Route {
// Combine into a new slice to avoid accidentally passing the same slice for
// multiple routes, which would lead to later add() calls overwriting the
// middleware from earlier calls.
m := []interface{}{}
m = append(m, g.middleware...)
m = append(m, middleware...)
var host string
if g.host != nil {
host = g.host.name
}
r := g.echo.add(host, method, g.prefix, g.prefix+path, h, m...)
if len(g.meta) > 0 {
r.Meta = H{}
r.Meta.DeepMerge(g.meta)
}
return r
}
func (g *Group) SetMeta(meta H) *Group {
g.meta = meta
return g
}
func (g *Group) SetMetaKV(key string, value interface{}) *Group {
if g.meta == nil {
g.meta = H{}
}
g.meta[key] = value
return g
}