-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkira_router_group.go
87 lines (69 loc) · 2.7 KB
/
kira_router_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
package kira
import (
"net/http"
"github.com/julienschmidt/httprouter"
)
// GroupFunc represent Group function.
type GroupFunc func(Group)
// Group represent routes group.
type Group struct {
app *App
prefix string
middlewares []Middleware
}
// Group adds a prefix to the given routes.
func (app *App) Group(prefix string, group GroupFunc, mds ...Middleware) {
g := Group{
app: app,
prefix: prefix,
middlewares: mds,
}
// Register the group routes.
group(g)
}
func (g Group) path(path string) string {
return httprouter.CleanPath(g.prefix + httprouter.CleanPath(path))
}
func (g Group) buildMeddlewares(md ...Middleware) (mds []Middleware) {
mds = append(mds, md...)
mds = append(mds, g.middlewares...)
return mds
}
// Group adds ap refix to another grouped routes.
func (g Group) Group(prefix string, group GroupFunc, middlewares ...Middleware) {
g.app.Group(httprouter.CleanPath(g.prefix)+httprouter.CleanPath(prefix), func(g Group) {
group(g)
}, g.buildMeddlewares(middlewares...)...)
}
// Get is a shortcut for app.Get with the group prefix.
func (g Group) Get(path string, handler HandlerFunc, middlewares ...Middleware) {
g.app.Get(g.path(path), handler, g.buildMeddlewares(middlewares...)...)
}
// Head is a shortcut for app.Head with the group prefix.
func (g Group) Head(path string, handler HandlerFunc, middlewares ...Middleware) {
g.app.Head(g.path(path), handler, g.buildMeddlewares(middlewares...)...)
}
// Post is a shortcut for app.Post with the group prefix.
func (g Group) Post(path string, handler HandlerFunc, middlewares ...Middleware) {
g.app.Post(g.path(path), handler, g.buildMeddlewares(middlewares...)...)
}
// Put is a shortcut for app.Put with the group prefix.
func (g Group) Put(path string, handler HandlerFunc, middlewares ...Middleware) {
g.app.Put(g.path(path), handler, g.buildMeddlewares(middlewares...)...)
}
// Patch is a shortcut for app.Patch with the group prefix.
func (g Group) Patch(path string, handler HandlerFunc, middlewares ...Middleware) {
g.app.Patch(g.path(path), handler, g.buildMeddlewares(middlewares...)...)
}
// Delete is a shortcut for app.Delete with the group prefix.
func (g Group) Delete(path string, handler HandlerFunc, middlewares ...Middleware) {
g.app.Delete(g.path(path), handler, g.buildMeddlewares(middlewares...)...)
}
// Options is a shortcut for app.Delete with the group prefix.
func (g Group) Options(path string, handler HandlerFunc, middlewares ...Middleware) {
g.app.Options(g.path(path), handler, g.buildMeddlewares(middlewares...)...)
}
// ServeFiles is a shortcut for app.ServeFiles with the group prefix.
func (g Group) ServeFiles(path string, root http.FileSystem) {
g.app.ServeFiles(g.path(path), root)
}