-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgroup.go
More file actions
105 lines (89 loc) · 3.63 KB
/
group.go
File metadata and controls
105 lines (89 loc) · 3.63 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package celeris
import "strings"
// RouteGroup is a collection of routes that share a common path prefix and
// middleware. Use [Server.Group] to create one. A RouteGroup must not be used
// after [Server.Start] is called.
type RouteGroup struct {
prefix string
middleware []HandlerFunc
server *Server
}
func (g *RouteGroup) handle(method, path string, handlers ...HandlerFunc) *Route {
fullPath := g.prefix + path
chain := make([]HandlerFunc, 0, len(g.server.middleware)+len(g.middleware)+len(handlers))
chain = append(chain, g.server.middleware...)
chain = append(chain, g.middleware...)
chain = append(chain, handlers...)
return g.server.router.addRoute(method, fullPath, chain)
}
// Use adds middleware to this group. Group middleware runs after server-level
// middleware but before route handlers within this group. Middleware chains
// are composed at route registration time, so Use must be called before
// registering routes on this group.
func (g *RouteGroup) Use(middleware ...HandlerFunc) *RouteGroup {
g.middleware = append(g.middleware, middleware...)
return g
}
// GET registers a handler for GET requests.
func (g *RouteGroup) GET(path string, handlers ...HandlerFunc) *Route {
return g.handle("GET", path, handlers...)
}
// POST registers a handler for POST requests.
func (g *RouteGroup) POST(path string, handlers ...HandlerFunc) *Route {
return g.handle("POST", path, handlers...)
}
// PUT registers a handler for PUT requests.
func (g *RouteGroup) PUT(path string, handlers ...HandlerFunc) *Route {
return g.handle("PUT", path, handlers...)
}
// DELETE registers a handler for DELETE requests.
func (g *RouteGroup) DELETE(path string, handlers ...HandlerFunc) *Route {
return g.handle("DELETE", path, handlers...)
}
// PATCH registers a handler for PATCH requests.
func (g *RouteGroup) PATCH(path string, handlers ...HandlerFunc) *Route {
return g.handle("PATCH", path, handlers...)
}
// HEAD registers a handler for HEAD requests.
func (g *RouteGroup) HEAD(path string, handlers ...HandlerFunc) *Route {
return g.handle("HEAD", path, handlers...)
}
// OPTIONS registers a handler for OPTIONS requests.
func (g *RouteGroup) OPTIONS(path string, handlers ...HandlerFunc) *Route {
return g.handle("OPTIONS", path, handlers...)
}
// Any registers a handler for all HTTP methods, returning the [Route] for each.
func (g *RouteGroup) Any(path string, handlers ...HandlerFunc) []*Route {
methods := []string{"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"}
routes := make([]*Route, len(methods))
for i, method := range methods {
routes[i] = g.handle(method, path, handlers...)
}
return routes
}
// Handle registers a handler for the given HTTP method and path pattern.
func (g *RouteGroup) Handle(method, path string, handlers ...HandlerFunc) *Route {
return g.handle(method, path, handlers...)
}
// Static registers a GET handler that serves files from root under the given
// prefix. Uses FileFromDir for path traversal protection.
func (g *RouteGroup) Static(prefix, root string) *Route {
p := strings.TrimRight(prefix, "/") + "/*filepath"
return g.GET(p, func(c *Context) error {
return c.FileFromDir(root, c.Param("filepath"))
})
}
// Group creates a sub-group with the given path prefix. The sub-group inherits
// middleware from the parent group.
func (g *RouteGroup) Group(prefix string, middleware ...HandlerFunc) *RouteGroup {
return &RouteGroup{
prefix: g.prefix + prefix,
middleware: append(g.combineMiddleware(), middleware...),
server: g.server,
}
}
func (g *RouteGroup) combineMiddleware() []HandlerFunc {
combined := make([]HandlerFunc, len(g.middleware))
copy(combined, g.middleware)
return combined
}