-
Notifications
You must be signed in to change notification settings - Fork 4
/
group.go
73 lines (57 loc) · 2.44 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
package bon
import "net/http"
type Group struct {
mux *Mux
middlewares []Middleware
prefix string
}
func (g *Group) Group(pattern string, middlewares ...Middleware) *Group {
return &Group{
mux: g.mux,
middlewares: append(g.middlewares, middlewares...),
prefix: g.prefix + resolvePatternPrefix(pattern),
}
}
func (g *Group) Route(middlewares ...Middleware) *Route {
return &Route{
mux: g.mux,
middlewares: middlewares,
}
}
func (g *Group) Use(middlewares ...Middleware) {
g.middlewares = append(g.middlewares, middlewares...)
}
func (g *Group) Get(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware) {
g.Handle(http.MethodGet, pattern, handlerFunc, middlewares...)
}
func (g *Group) Post(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware) {
g.Handle(http.MethodPost, pattern, handlerFunc, middlewares...)
}
func (g *Group) Put(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware) {
g.Handle(http.MethodPut, pattern, handlerFunc, middlewares...)
}
func (g *Group) Delete(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware) {
g.Handle(http.MethodDelete, pattern, handlerFunc, middlewares...)
}
func (g *Group) Head(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware) {
g.Handle(http.MethodHead, pattern, handlerFunc, middlewares...)
}
func (g *Group) Options(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware) {
g.Handle(http.MethodOptions, pattern, handlerFunc, middlewares...)
}
func (g *Group) Patch(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware) {
g.Handle(http.MethodPatch, pattern, handlerFunc, middlewares...)
}
func (g *Group) Connect(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware) {
g.Handle(http.MethodConnect, pattern, handlerFunc, middlewares...)
}
func (g *Group) Trace(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware) {
g.Handle(http.MethodTrace, pattern, handlerFunc, middlewares...)
}
func (g *Group) Handle(method, pattern string, handler http.Handler, middlewares ...Middleware) {
g.mux.Handle(method, g.prefix+resolvePatternPrefix(pattern), handler, append(g.middlewares, middlewares...)...)
}
func (g *Group) FileServer(pattern, root string, middlewares ...Middleware) {
p := g.prefix + resolvePatternPrefix(pattern)
contentsHandle(g, p, g.mux.newFileServer(p, root).contents, middlewares...)
}