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