-
Notifications
You must be signed in to change notification settings - Fork 5
/
group.go
98 lines (80 loc) · 2.2 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
package quick
import (
"net/http"
"github.com/jeffotoni/quick/internal/concat"
)
type Group struct {
prefix string
routes []Route
quick *Quick
}
func (q *Quick) Group(prefix string) *Group {
g := &Group{
prefix: prefix,
routes: []Route{},
quick: q,
}
q.groups = append(q.groups, *g)
return g
}
func (g *Group) Get(pattern string, handlerFunc HandleFunc) {
pattern = concat.String(g.prefix, pattern)
path, params, partternExist := extractParamsPattern(pattern)
route := Route{
Pattern: partternExist,
Path: path,
Params: params,
handler: extractParamsGet(g.quick, path, params, handlerFunc),
Method: http.MethodGet,
Group: g.prefix,
}
g.quick.appendRoute(&route)
g.quick.mux.HandleFunc(path, route.handler)
}
func (g *Group) Post(pattern string, handlerFunc HandleFunc) {
pattern = concat.String(g.prefix, pattern)
_, params, partternExist := extractParamsPattern(pattern)
pathPost := concat.String("post#", pattern)
route := Route{
Pattern: partternExist,
Path: pattern,
handler: extractParamsPost(g.quick, handlerFunc),
Method: http.MethodPost,
Params: params,
Group: g.prefix,
}
g.quick.appendRoute(&route)
g.quick.mux.HandleFunc(pathPost, route.handler)
}
func (g *Group) Put(pattern string, handlerFunc HandleFunc) {
pattern = concat.String(g.prefix, pattern)
_, params, partternExist := extractParamsPattern(pattern)
pathPut := concat.String("put#", pattern)
// Setting up the group
route := Route{
Pattern: partternExist,
Path: pattern,
handler: extractParamsPut(g.quick, handlerFunc),
Method: http.MethodPut,
Params: params,
Group: g.prefix,
}
g.quick.appendRoute(&route)
g.quick.mux.HandleFunc(pathPut, route.handler)
}
func (g *Group) Delete(pattern string, handlerFunc HandleFunc) {
pattern = concat.String(g.prefix, pattern)
_, params, partternExist := extractParamsPattern(pattern)
pathDelete := concat.String("delete#", pattern)
// Setting up the group
route := Route{
Pattern: partternExist,
Path: pattern,
Params: params,
handler: extractParamsDelete(g.quick, handlerFunc),
Method: http.MethodDelete,
Group: g.prefix,
}
g.quick.appendRoute(&route)
g.quick.mux.HandleFunc(pathDelete, route.handler)
}