forked from OneOfOne/gserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.go
177 lines (148 loc) · 3.94 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package gserv
import (
"net/http"
"strings"
"github.com/alpineiq/gserv/router"
"github.com/alpineiq/oerrs"
)
type (
Route = *router.Route
)
var DefaultCodec Codec = &JSONCodec{}
// Handler is the default server Handler
// In a handler chain, returning a non-nil breaks the chain.
type Handler = func(ctx *Context) Response
type Group struct {
s *Server
nm string
path string
mw []Handler
}
// Use adds more middleware to the current group.
func (g *Group) Use(mw ...Handler) {
g.mw = append(g.mw, mw...)
}
// Routes returns the current routes set.
// Each route is returned in the order of group name, method, path.
func (g *Group) Routes() [][3]string {
return g.s.r.GetRoutes()
}
// AddRoute adds a handler (or more) to the specific method and path
// it is NOT safe to call this once you call one of the run functions
func (g *Group) AddRoute(method, path string, handlers ...Handler) Route {
ghc := groupHandlerChain{
hc: handlers,
g: g,
}
p := joinPath(g.path, path)
return g.s.r.AddRoute(g.nm, method, p, ghc.Serve)
}
// GET is an alias for AddRoute("GET", path, handlers...).
func (g *Group) GET(path string, handlers ...Handler) Route {
return g.AddRoute(http.MethodGet, path, handlers...)
}
// PUT is an alias for AddRoute("PUT", path, handlers...).
func (g *Group) PUT(path string, handlers ...Handler) Route {
return g.AddRoute(http.MethodPut, path, handlers...)
}
// POST is an alias for AddRoute("POST", path, handlers...).
func (g *Group) POST(path string, handlers ...Handler) Route {
return g.AddRoute(http.MethodPost, path, handlers...)
}
// DELETE is an alias for AddRoute("DELETE", path, handlers...).
func (g *Group) DELETE(path string, handlers ...Handler) Route {
return g.AddRoute(http.MethodDelete, path, handlers...)
}
// OPTIONS is an alias for AddRoute("OPTIONS", path, handlers...).
func (g *Group) OPTIONS(path string, handlers ...Handler) Route {
return g.AddRoute(http.MethodOptions, path, handlers...)
}
func (g *Group) DisableRoute(method, path string, disabled bool) bool {
return g.s.r.DisableRoute(method, joinPath(g.path, path), disabled)
}
func (g *Group) Static(path, localPath string, allowListing bool) Route {
path = strings.TrimSuffix(path, "/")
return g.AddRoute(http.MethodGet, joinPath(path, "*fp"), StaticDirStd(path, localPath, allowListing))
}
func (g *Group) StaticFile(path, localPath string) Route {
return g.AddRoute(http.MethodGet, path, func(ctx *Context) Response {
ctx.File(localPath)
return nil
})
}
// SubGroup returns a sub-handler group based on the current group's middleware
func (g *Group) SubGroup(name, path string, mw ...Handler) *Group {
return &Group{
nm: name,
mw: append(g.mw[:len(g.mw):len(g.mw)], mw...),
path: joinPath(g.path, path),
s: g.s,
}
}
func joinPath(p1, p2 string) string {
if p2 == "" {
return p1
}
if p1 != "" && p1[0] != '/' {
p1 = "/" + p1
}
if p2 != "" && p2[0] != '/' {
p2 = "/" + p2
}
return strings.Replace(p1+p2, "//", "/", -1)
}
type groupHandlerChain struct {
g *Group
hc []Handler
}
func (ghc *groupHandlerChain) Serve(rw http.ResponseWriter, req *http.Request, p router.Params) {
var (
ctx = getCtx(rw, req, p, ghc.g.s)
mwIdx, hIdx int
catchPanic func()
)
defer putCtx(ctx)
if ph := ghc.g.s.PanicHandler; ph != nil {
catchPanic = func() {
if v := recover(); v != nil {
fr := oerrs.Caller(2)
ghc.g.s.PanicHandler(ctx, v, fr)
}
}
}
ctx.nextMW = func() {
if catchPanic != nil {
defer catchPanic()
}
for mwIdx < len(ghc.g.mw) && !ctx.done {
h := ghc.g.mw[mwIdx]
mwIdx++
if r := h(ctx); r != nil {
if r != Break {
r.WriteToCtx(ctx)
} else {
ctx.next = nil
}
break
}
}
ctx.nextMW = nil
}
ctx.next = func() {
if catchPanic != nil {
defer catchPanic()
}
for hIdx < len(ghc.hc) && !ctx.done {
h := ghc.hc[hIdx]
hIdx++
if r := h(ctx); r != nil {
if r != Break {
r.WriteToCtx(ctx)
}
break
}
}
ctx.next = nil
}
ctx.Next()
}