-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
67 lines (59 loc) · 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
package plover
import (
"strings"
)
type Group struct {
basic string
middlewares []Middleware
nodes map[string][]Controllers
}
func (group *Group) GetMethod(method string) []Controllers {
return group.nodes[method]
}
func (group *Group) GetMiddleware() []Middleware {
return group.middlewares
}
func (group *Group) HasBasic(PATH string) bool {
return strings.HasPrefix(PATH, group.basic)
}
func (group *Group) Use(middleware ...Middleware) {
group.middlewares = middleware
}
func (group *Group) Add(method string, PATH string, handle Handle, action Action, withoutMiddleware []bool) *Group {
if group.nodes == nil {
group.nodes = make(map[string][]Controllers)
}
method = strings.ToUpper(method)
PATH = strings.ToLower(strings.Trim(PATH, "/"))
if strings.EqualFold(PATH, "") {
PATH = "/"
}
var WithoutMiddleware bool
if len(withoutMiddleware) > 0 {
WithoutMiddleware = withoutMiddleware[0]
} else {
WithoutMiddleware = false
}
group.nodes[method] = append(group.nodes[method], Controllers{
PATH: PATH,
Handle: handle,
Action: action,
WithoutMiddleware: WithoutMiddleware,
})
return group
}
func (group *Group) Get(PATH string, handle Handle, action Action, withoutMiddleware ...bool) *Group {
return group.Add("GET", PATH, handle, action, withoutMiddleware)
}
func (group *Group) Post(PATH string, handle Handle, action Action, withoutMiddleware ...bool) *Group {
return group.Add("POST", PATH, handle, action, withoutMiddleware)
}
func (group *Group) Put(PATH string, handle Handle, action Action, withoutMiddleware ...bool) *Group {
return group.Add("PUT", PATH, handle, action, withoutMiddleware)
}
func (group *Group) Delete(PATH string, handle Handle, action Action, withoutMiddleware ...bool) *Group {
return group.Add("DELETE", PATH, handle, action, withoutMiddleware)
}
func (group *Group) Any(PATH string, handle Handle, action Action, withoutMiddleware ...bool) *Group {
return group.Add("*", PATH, handle, action, withoutMiddleware)
}