forked from qiangxue/fasthttp-routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
107 lines (90 loc) · 3.75 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
// Copyright 2016 Qiang Xue. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package routing
import (
"strings"
)
// RouteGroup represents a group of routes that share the same path prefix.
type RouteGroup struct {
prefix string
router *Router
handlers []Handler
}
// newRouteGroup creates a new RouteGroup with the given path prefix, router, and handlers.
func newRouteGroup(prefix string, router *Router, handlers []Handler) *RouteGroup {
return &RouteGroup{
prefix: prefix,
router: router,
handlers: handlers,
}
}
// Get adds a GET route to the router with the given route path and handlers.
func (r *RouteGroup) Get(path string, handlers ...Handler) *Route {
return newRoute(path, r).Get(handlers...)
}
// Post adds a POST route to the router with the given route path and handlers.
func (r *RouteGroup) Post(path string, handlers ...Handler) *Route {
return newRoute(path, r).Post(handlers...)
}
// Put adds a PUT route to the router with the given route path and handlers.
func (r *RouteGroup) Put(path string, handlers ...Handler) *Route {
return newRoute(path, r).Put(handlers...)
}
// Patch adds a PATCH route to the router with the given route path and handlers.
func (r *RouteGroup) Patch(path string, handlers ...Handler) *Route {
return newRoute(path, r).Patch(handlers...)
}
// Delete adds a DELETE route to the router with the given route path and handlers.
func (r *RouteGroup) Delete(path string, handlers ...Handler) *Route {
return newRoute(path, r).Delete(handlers...)
}
// Connect adds a CONNECT route to the router with the given route path and handlers.
func (r *RouteGroup) Connect(path string, handlers ...Handler) *Route {
return newRoute(path, r).Connect(handlers...)
}
// Head adds a HEAD route to the router with the given route path and handlers.
func (r *RouteGroup) Head(path string, handlers ...Handler) *Route {
return newRoute(path, r).Head(handlers...)
}
// Options adds an OPTIONS route to the router with the given route path and handlers.
func (r *RouteGroup) Options(path string, handlers ...Handler) *Route {
return newRoute(path, r).Options(handlers...)
}
// Trace adds a TRACE route to the router with the given route path and handlers.
func (r *RouteGroup) Trace(path string, handlers ...Handler) *Route {
return newRoute(path, r).Trace(handlers...)
}
// Any adds a route with the given route, handlers, and the HTTP methods as listed in routing.Methods.
func (r *RouteGroup) Any(path string, handlers ...Handler) *Route {
route := newRoute(path, r)
for _, method := range Methods {
route.add(method, handlers)
}
return route
}
// To adds a route to the router with the given HTTP methods, route path, and handlers.
// Multiple HTTP methods should be separated by commas (without any surrounding spaces).
func (r *RouteGroup) To(methods, path string, handlers ...Handler) *Route {
route := newRoute(path, r)
for _, method := range strings.Split(methods, ",") {
route.add(method, handlers)
}
return route
}
// Group creates a RouteGroup with the given route path prefix and handlers.
// The new group will combine the existing path prefix with the new one.
// If no handler is provided, the new group will inherit the handlers registered
// with the current group.
func (r *RouteGroup) Group(prefix string, handlers ...Handler) *RouteGroup {
if len(handlers) == 0 {
handlers = make([]Handler, len(r.handlers))
copy(handlers, r.handlers)
}
return newRouteGroup(r.prefix+prefix, r.router, handlers)
}
// Use registers one or multiple handlers to the current route group.
// These handlers will be shared by all routes belong to this group and its subgroups.
func (r *RouteGroup) Use(handlers ...Handler) {
r.handlers = append(r.handlers, handlers...)
}