-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.go
117 lines (102 loc) · 4.16 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
108
109
110
111
112
113
114
115
116
117
package go_http_kit
import (
"log"
"net/http"
)
// Group represents a group of routes with a common URL pattern and middlewares.
type Group struct {
// hk - Reference to the parent HttpKit instance.
hk *HttpKit
// pattern - Common URL pattern for all routes in the group.
pattern string
// middlewares - Middlewares to be applied to all routes in the group.
middlewares []Middleware
// routes - List of routes belonging to this group.
routes []*Route
}
// Group creates a new group with the specified URL pattern and middlewares.
func (hk *HttpKit) Group(pattern string, middlewares ...Middleware) *Group {
g := &Group{
hk: hk,
pattern: pattern,
middlewares: middlewares,
}
hk.mu.Lock()
hk.groupRoutes = append(hk.groupRoutes, g)
hk.mu.Unlock()
return g
}
// GroupFunc creates a new group with the specified URL pattern, executes the provided function, and applies middlewares.
func (hk *HttpKit) GroupFunc(pattern string, f func(g *Group), middlewares ...Middleware) *Group {
g := &Group{
hk: hk,
pattern: pattern,
middlewares: middlewares,
}
hk.mu.Lock()
hk.groupRoutes = append(hk.groupRoutes, g)
hk.mu.Unlock()
f(g)
return g
}
func (g *Group) Group(pattern string, middlewares ...Middleware) *Group {
group := &Group{
hk: g.hk,
pattern: g.pattern + pattern,
middlewares: append(g.middlewares, middlewares...),
}
g.hk.mu.Lock()
g.hk.groupRoutes = append(g.hk.groupRoutes, group)
g.hk.mu.Unlock()
return g
}
func (g *Group) GroupFunc(pattern string, f func(g *Group), middlewares ...Middleware) *Group {
group := &Group{
hk: g.hk,
pattern: g.pattern + pattern,
}
group.middlewares = append(group.middlewares, g.middlewares...)
group.middlewares = append(group.middlewares, middlewares...)
g.hk.mu.Lock()
g.hk.groupRoutes = append(g.hk.groupRoutes, group)
g.hk.mu.Unlock()
f(group)
return g
}
// GET method for creating a new GET route with the specified pattern and handler.
func (g *Group) GET(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
log.Println(g.pattern + pattern)
return g.hk.addRoute(g.pattern+pattern, http.MethodGet, handler, g)
}
// HEAD method for creating a new HEAD route with the specified pattern and handler.
func (g *Group) HEAD(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
return g.hk.addRoute(g.pattern+pattern, http.MethodHead, handler, g)
}
// POST method for creating a new POST route with the specified pattern and handler.
func (g *Group) POST(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
return g.hk.addRoute(g.pattern+pattern, http.MethodPost, handler, g)
}
// PUT method for creating a new PUT route with the specified pattern and handler.
func (g *Group) PUT(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
return g.hk.addRoute(g.pattern+pattern, http.MethodPut, handler, g)
}
// PATCH method for creating a new PATCH route with the specified pattern and handler.
func (g *Group) PATCH(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
return g.hk.addRoute(g.pattern+pattern, http.MethodPatch, handler, g)
}
// DELETE method for creating a new DELETE route with the specified pattern and handler.
func (g *Group) DELETE(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
return g.hk.addRoute(g.pattern+pattern, http.MethodDelete, handler, g)
}
// CONNECT method for creating a new CONNECT route with the specified pattern and handler.
func (g *Group) CONNECT(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
return g.hk.addRoute(g.pattern+pattern, http.MethodConnect, handler, g)
}
// OPTIONS method for creating a new OPTIONS route with the specified pattern and handler.
func (g *Group) OPTIONS(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
return g.hk.addRoute(g.pattern+pattern, http.MethodOptions, handler, g)
}
// TRACE method for creating a new TRACE route with the specified pattern and handler.
func (g *Group) TRACE(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
return g.hk.addRoute(g.pattern+pattern, http.MethodTrace, handler, g)
}