-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_router.go
74 lines (59 loc) · 2.15 KB
/
group_router.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
package dipra
import (
"net/http"
)
type (
Group interface {
GET(path string, handler HandlerFunc, middleware ...MiddlewareFunc)
POST(path string, handler HandlerFunc, middleware ...MiddlewareFunc)
PUT(path string, handler HandlerFunc, middleware ...MiddlewareFunc)
PATCH(path string, handler HandlerFunc, middleware ...MiddlewareFunc)
DELETE(path string, handler HandlerFunc, middleware ...MiddlewareFunc)
ANY(path string, handler HandlerFunc, middleware ...MiddlewareFunc)
use(m ...MiddlewareFunc)
}
groupRoute struct {
prefix string
middleware []MiddlewareFunc
dipra *Engine
}
)
func NewGroupRoute(prefix string, dipra *Engine) Group {
return &groupRoute{
dipra: dipra,
prefix: prefix,
middleware: []MiddlewareFunc{},
}
}
func (g *groupRoute) addGroupRoute(path string, method string, handler HandlerFunc, middleware ...MiddlewareFunc) {
if path == "/" {
path = ""
}
middleware = append(middleware, g.middleware...)
fullpath := g.prefix + path
g.dipra.AddRoute(fullpath, method, handler, middleware...)
g.dipra.group = append(g.dipra.group, *g)
}
func (g *groupRoute) GET(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.addGroupRoute(path, http.MethodGet, handler, middleware...)
}
func (g *groupRoute) POST(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.addGroupRoute(path, http.MethodPost, handler, middleware...)
}
func (g *groupRoute) PUT(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.addGroupRoute(path, http.MethodPut, handler, middleware...)
}
func (g *groupRoute) PATCH(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.addGroupRoute(path, http.MethodPatch, handler, middleware...)
}
func (g *groupRoute) DELETE(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.addGroupRoute(path, http.MethodDelete, handler, middleware...)
}
func (g *groupRoute) ANY(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
for _, method := range allowMethod {
g.addGroupRoute(path, method, handler, middleware...)
}
}
func (g *groupRoute) use(m ...MiddlewareFunc) {
g.middleware = append(g.middleware, m...)
}