-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathgroup.go
112 lines (95 loc) · 2.95 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
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package fiber
import (
"log"
"reflect"
)
// Group struct
type Group struct {
app *App
prefix string
}
// Use registers a middleware route.
// Middleware matches requests beginning with the provided prefix.
// Providing a prefix is optional, it defaults to "/".
//
// - group.Use(handler)
// - group.Use("/api", handler)
// - group.Use("/api", handler, handler)
func (grp *Group) Use(args ...interface{}) *Route {
var path = ""
var handlers []Handler
for i := 0; i < len(args); i++ {
switch arg := args[i].(type) {
case string:
path = arg
case Handler:
handlers = append(handlers, arg)
default:
log.Fatalf("Use: Invalid Handler %v", reflect.TypeOf(arg))
}
}
return grp.app.register("USE", getGroupPath(grp.prefix, path), handlers...)
}
// Get ...
func (grp *Group) Get(path string, handlers ...Handler) *Route {
return grp.Add(MethodGet, path, handlers...)
}
// Head ...
func (grp *Group) Head(path string, handlers ...Handler) *Route {
return grp.Add(MethodHead, path, handlers...)
}
// Post ...
func (grp *Group) Post(path string, handlers ...Handler) *Route {
return grp.Add(MethodPost, path, handlers...)
}
// Put ...
func (grp *Group) Put(path string, handlers ...Handler) *Route {
return grp.Add(MethodPut, path, handlers...)
}
// Delete ...
func (grp *Group) Delete(path string, handlers ...Handler) *Route {
return grp.Add(MethodDelete, path, handlers...)
}
// Connect ...
func (grp *Group) Connect(path string, handlers ...Handler) *Route {
return grp.Add(MethodConnect, path, handlers...)
}
// Options ...
func (grp *Group) Options(path string, handlers ...Handler) *Route {
return grp.Add(MethodOptions, path, handlers...)
}
// Trace ...
func (grp *Group) Trace(path string, handlers ...Handler) *Route {
return grp.Add(MethodTrace, path, handlers...)
}
// Patch ...
func (grp *Group) Patch(path string, handlers ...Handler) *Route {
return grp.Add(MethodPatch, path, handlers...)
}
// Add ...
func (grp *Group) Add(method, path string, handlers ...Handler) *Route {
return grp.app.register(method, getGroupPath(grp.prefix, path), handlers...)
}
// Static ...
func (grp *Group) Static(prefix, root string, config ...Static) *Route {
return grp.app.registerStatic(getGroupPath(grp.prefix, prefix), root, config...)
}
// All ...
func (grp *Group) All(path string, handlers ...Handler) []*Route {
routes := make([]*Route, len(intMethod))
for i, method := range intMethod {
routes[i] = grp.Add(method, path, handlers...)
}
return routes
}
// Group is used for Routes with common prefix to define a new sub-router with optional middleware.
func (grp *Group) Group(prefix string, handlers ...Handler) *Group {
prefix = getGroupPath(grp.prefix, prefix)
if len(handlers) > 0 {
grp.app.register("USE", prefix, handlers...)
}
return grp.app.Group(prefix)
}