-
Notifications
You must be signed in to change notification settings - Fork 33
/
middleware.go
212 lines (179 loc) · 7.1 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// Source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package aah
import (
"net/http"
"reflect"
"aahframe.work/essentials"
"aahframe.work/log"
)
const (
// Interceptor Action Name
incpBeforeActionName = "Before"
incpAfterActionName = "After"
incpPanicActionName = "Panic"
incpFinallyActionName = "Finally"
)
// MiddlewareFunc func type is aah framework middleware signature.
type MiddlewareFunc func(ctx *Context, m *Middleware)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Package methods
//______________________________________________________________________________
// ToMiddleware method expands the possibilities. It helps aah users to
// register the third-party or your own net/http middleware into `aah.MiddlewareFunc`.
//
// It is highly recommended to refactored to `aah.MiddlewareFunc`.
//
// You can register below handler types:
//
// 1) aah.ToMiddleware(h http.Handler)
//
// 2) aah.ToMiddleware(h http.HandlerFunc)
//
// 3) aah.ToMiddleware(func(w http.ResponseWriter, r *http.Request))
func ToMiddleware(handler interface{}) MiddlewareFunc {
switch handler.(type) {
case MiddlewareFunc:
return handler.(MiddlewareFunc)
case http.Handler:
h := handler.(http.Handler)
return func(ctx *Context, m *Middleware) {
h.ServeHTTP(ctx.Res, ctx.Req.Unwrap())
m.Next(ctx)
}
case func(http.ResponseWriter, *http.Request):
return ToMiddleware(http.HandlerFunc(handler.(func(http.ResponseWriter, *http.Request))))
default:
log.Errorf("Not a vaild handler: %s", ess.GetFunctionInfo(handler).QualifiedName)
return nil
}
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Middleware methods
//______________________________________________________________________________
// Middleware struct is to implement aah framework middleware chain.
type Middleware struct {
next MiddlewareFunc
further *Middleware
}
// Next method calls next middleware in the chain if available.
func (mw *Middleware) Next(ctx *Context) {
if ctx.abort {
// abort, not to proceed further
return
}
if mw.next != nil {
mw.next(ctx, mw.further)
}
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// engine - Middleware
//______________________________________________________________________________
// Middlewares method adds given middleware into middleware stack
func (e *HTTPEngine) Middlewares(middlewares ...MiddlewareFunc) {
e.mwStack = append(e.mwStack, middlewares...)
e.invalidateMwChain()
}
func (e *HTTPEngine) invalidateMwChain() {
e.mwChain = nil
cnt := len(e.mwStack)
e.mwChain = make([]*Middleware, cnt)
for idx := 0; idx < cnt; idx++ {
e.mwChain[idx] = &Middleware{next: e.mwStack[idx]}
}
for idx := cnt - 1; idx > 0; idx-- {
e.mwChain[idx-1].further = e.mwChain[idx]
}
e.mwChain[cnt-1].further = &Middleware{}
}
type beforeInterceptor interface {
Before()
}
type afterInterceptor interface {
After()
}
type finallyInterceptor interface {
Finally()
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Action middleware
//______________________________________________________________________________
// ActionMiddleware performs
// - Executes Interceptors (Before, Before<ActionName>, After, After<ActionName>,
// Panic, Panic<ActionName>, Finally, Finally<ActionName>)
// - Invokes Controller Action
func ActionMiddleware(ctx *Context, m *Middleware) {
if err := ctx.setTarget(ctx.route); err == errTargetNotFound {
// No controller or action found for the route
ctx.Reply().NotFound().Error(newError(ErrControllerOrActionNotFound, http.StatusNotFound))
return
}
// Finally action and method. Always executed if present
defer func() {
if finallyActionMethod := ctx.targetrv.MethodByName(incpFinallyActionName + ctx.action.Name); finallyActionMethod.IsValid() {
ctx.Log().Debugf("Calling interceptor: %s.%s", ctx.controller.FqName, incpFinallyActionName+ctx.action.Name)
finallyActionMethod.Call(emptyArg)
}
// Finally: executes always if its implemented. Its applicable for every action in the controller
if cntrl, ok := ctx.target.(finallyInterceptor); ok {
ctx.Log().Debugf("Calling interceptor: %s.Finally", ctx.controller.FqName)
cntrl.Finally()
}
}()
// Panic action and method
defer func() {
if r := recover(); r != nil {
// if ctx.abort {
// return
// }
if panicActionMethod := ctx.targetrv.MethodByName(incpPanicActionName + ctx.action.Name); panicActionMethod.IsValid() {
ctx.Log().Debugf("Calling interceptor: %s.%s", ctx.controller.FqName, incpPanicActionName+ctx.action.Name)
rv := append([]reflect.Value{}, reflect.ValueOf(r))
panicActionMethod.Call(rv)
} else if panicAction := ctx.targetrv.MethodByName(incpPanicActionName); panicAction.IsValid() {
ctx.Log().Debugf("Calling interceptor: %s.%s", ctx.controller.FqName, incpPanicActionName)
rv := append([]reflect.Value{}, reflect.ValueOf(r))
panicAction.Call(rv)
} else { // propagate it
panic(r)
}
}
}()
// Before: executes before every action in the controller
if cntrl, ok := ctx.target.(beforeInterceptor); ok {
ctx.Log().Debugf("Calling interceptor: %s.Before", ctx.controller.FqName)
cntrl.Before()
}
// Before action method
if !ctx.abort {
if beforeActionMethod := ctx.targetrv.MethodByName(incpBeforeActionName + ctx.action.Name); beforeActionMethod.IsValid() {
ctx.Log().Debugf("Calling interceptor: %s.%s", ctx.controller.FqName, incpBeforeActionName+ctx.action.Name)
beforeActionMethod.Call(emptyArg)
}
}
if !ctx.abort {
// Parse Action Parameters
actionArgs, err := ctx.parseParameters()
if err != nil { // Any error of parameter parsing result in 400 Bad Request
ctx.Reply().BadRequest().Error(err)
return
}
ctx.Log().Debugf("Calling action: %s.%s", ctx.controller.FqName, ctx.action.Name)
ctx.actionrv.Call(actionArgs)
}
// After action method
if !ctx.abort {
if afterActionMethod := ctx.targetrv.MethodByName(incpAfterActionName + ctx.action.Name); afterActionMethod.IsValid() {
ctx.Log().Debugf("Calling interceptor: %s.%s", ctx.controller.FqName, incpAfterActionName+ctx.action.Name)
afterActionMethod.Call(emptyArg)
}
}
// After: executes after every action in the controller
if !ctx.abort {
if cntrl, ok := ctx.target.(afterInterceptor); ok {
ctx.Log().Debugf("Calling interceptor: %s.After", ctx.controller.FqName)
cntrl.After()
}
}
}