-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgroup.go
44 lines (34 loc) · 1.06 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
package xun
import (
"net/http"
)
type group struct {
prefix string
middlewares []Middleware
app *App
}
func (g *group) Use(middleware ...Middleware) {
g.middlewares = append(g.middlewares, middleware...)
}
func (g *group) Get(pattern string, hf HandleFunc, opts ...RoutingOption) {
g.HandleFunc(http.MethodGet+" "+g.prefix+pattern, hf, opts...)
}
func (g *group) Post(pattern string, hf HandleFunc, opts ...RoutingOption) {
g.HandleFunc(http.MethodPost+" "+g.prefix+pattern, hf, opts...)
}
func (g *group) Put(pattern string, hf HandleFunc, opts ...RoutingOption) {
g.HandleFunc(http.MethodPut+" "+g.prefix+pattern, hf, opts...)
}
func (g *group) Delete(pattern string, hf HandleFunc, opts ...RoutingOption) {
g.HandleFunc(http.MethodDelete+" "+g.prefix+pattern, hf, opts...)
}
func (g *group) HandleFunc(pattern string, hf HandleFunc, opts ...RoutingOption) {
g.app.createHandler(pattern, hf, opts, g)
}
func (g *group) Next(hf HandleFunc) HandleFunc {
next := hf
for i := len(g.middlewares); i > 0; i-- {
next = g.middlewares[i-1](next)
}
return next
}