-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathmiddleware.go
70 lines (62 loc) · 2.02 KB
/
middleware.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
package goku
// middlewarer interface
// execute order: OnBeginRequest -> OnBeginMvcHandle -> {controller} -> OnEndMvcHandle -> OnEndRequest
// notice:
// OnBeginRequest & OnEndRequest: All requests will be through these
// OnBeginMvcHandle & OnEndMvcHandle: not matched route & static file are not through these
type Middlewarer interface {
OnBeginRequest(ctx *HttpContext) (ActionResulter, error)
OnBeginMvcHandle(ctx *HttpContext) (ActionResulter, error)
OnEndMvcHandle(ctx *HttpContext) (ActionResulter, error)
OnEndRequest(ctx *HttpContext) (ActionResulter, error)
}
// middleware handler, handle the middleware how tu execute
type MiddlewareHandler interface {
BeginRequest(ctx *HttpContext) (ar ActionResulter, err error)
BeginMvcHandle(ctx *HttpContext) (ar ActionResulter, err error)
EndMvcHandle(ctx *HttpContext) (ar ActionResulter, err error)
EndRequest(ctx *HttpContext) (ar ActionResulter, err error)
}
// the defaultmiddleware handler
type DefaultMiddlewareHandle struct {
Middlewares []Middlewarer
}
func (mh *DefaultMiddlewareHandle) AddMiddleware(mw Middlewarer) {
mh.Middlewares = append(mh.Middlewares, mw)
}
func (mh *DefaultMiddlewareHandle) BeginRequest(ctx *HttpContext) (ar ActionResulter, err error) {
for _, mw := range mh.Middlewares {
ar, err = mw.OnBeginRequest(ctx)
if err != nil || ar != nil {
return
}
}
return
}
func (mh *DefaultMiddlewareHandle) EndRequest(ctx *HttpContext) (ar ActionResulter, err error) {
for _, mw := range mh.Middlewares {
ar, err = mw.OnEndRequest(ctx)
if err != nil || ar != nil {
return
}
}
return
}
func (mh *DefaultMiddlewareHandle) BeginMvcHandle(ctx *HttpContext) (ar ActionResulter, err error) {
for _, mw := range mh.Middlewares {
ar, err = mw.OnBeginMvcHandle(ctx)
if err != nil || ar != nil {
return
}
}
return
}
func (mh *DefaultMiddlewareHandle) EndMvcHandle(ctx *HttpContext) (ar ActionResulter, err error) {
for _, mw := range mh.Middlewares {
ar, err = mw.OnEndMvcHandle(ctx)
if err != nil || ar != nil {
return
}
}
return
}