-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgroup.go
More file actions
87 lines (71 loc) · 2.78 KB
/
group.go
File metadata and controls
87 lines (71 loc) · 2.78 KB
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
package bon
import (
"net/http"
"strings"
)
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,
prefix: g.prefix,
}
}
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) {
// Safely combine prefix and pattern
fullPattern := g.prefix + resolvePatternPrefix(pattern)
// Remove consecutive slashes
for strings.Contains(fullPattern, "//") {
fullPattern = strings.ReplaceAll(fullPattern, "//", "/")
}
g.mux.Handle(method, fullPattern, handler, append(g.middlewares, middlewares...)...)
}
func (g *Group) FileServer(pattern, root string, middlewares ...Middleware) {
p := g.prefix + resolvePatternPrefix(pattern)
// Remove consecutive slashes
for strings.Contains(p, "//") {
p = strings.ReplaceAll(p, "//", "/")
}
contentsHandle(g, p, g.mux.newFileServer(p, root).contents, middlewares...)
}