-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ghttp_request_middleware.go
171 lines (159 loc) · 4.97 KB
/
ghttp_request_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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package ghttp
import (
"context"
"net/http"
"reflect"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/util/gutil"
)
// middleware is the plugin for request workflow management.
type middleware struct {
served bool // Is the request served, which is used for checking response status 404.
request *Request // The request object pointer.
handlerIndex int // Index number for executing sequence purpose for handler items.
handlerMDIndex int // Index number for executing sequence purpose for bound middleware of handler item.
}
// Next calls the next workflow handler.
// It's an important function controlling the workflow of the server request execution.
func (m *middleware) Next() {
var item *HandlerItemParsed
var loop = true
for loop {
// Check whether the request is excited.
if m.request.IsExited() || m.handlerIndex >= len(m.request.handlers) {
break
}
item = m.request.handlers[m.handlerIndex]
// Filter the HOOK handlers, which are designed to be called in another standalone procedure.
if item.Handler.Type == HandlerTypeHook {
m.handlerIndex++
continue
}
// Current router switching.
m.request.Router = item.Handler.Router
// Router values switching.
m.request.routerMap = item.Values
ctx := m.request.context
gutil.TryCatch(ctx, func(ctx context.Context) {
// Execute bound middleware array of the item if it's not empty.
if m.handlerMDIndex < len(item.Handler.Middleware) {
md := item.Handler.Middleware[m.handlerMDIndex]
m.handlerMDIndex++
niceCallFunc(func() {
md(m.request)
})
loop = false
return
}
m.handlerIndex++
switch item.Handler.Type {
// Service object.
case HandlerTypeObject:
m.served = true
if m.request.IsExited() {
break
}
if item.Handler.InitFunc != nil {
niceCallFunc(func() {
item.Handler.InitFunc(m.request)
})
}
if !m.request.IsExited() {
m.callHandlerFunc(item.Handler.Info)
}
if !m.request.IsExited() && item.Handler.ShutFunc != nil {
niceCallFunc(func() {
item.Handler.ShutFunc(m.request)
})
}
// Service handler.
case HandlerTypeHandler:
m.served = true
if m.request.IsExited() {
break
}
niceCallFunc(func() {
m.callHandlerFunc(item.Handler.Info)
})
// Global middleware array.
case HandlerTypeMiddleware:
niceCallFunc(func() {
item.Handler.Info.Func(m.request)
})
// It does not continue calling next middleware after another middleware done.
// There should be a "Next" function to be called in the middleware in order to manage the workflow.
loop = false
}
}, func(ctx context.Context, exception error) {
if v, ok := exception.(error); ok && gerror.HasStack(v) {
// It's already an error that has stack info.
m.request.error = v
} else {
// Create a new error with stack info.
// Note that there's a skip pointing the start stacktrace
// of the real error point.
m.request.error = gerror.WrapCodeSkip(gcode.CodeInternalError, 1, exception, "")
}
m.request.Response.WriteStatus(http.StatusInternalServerError, exception)
loop = false
})
}
// Check the http status code after all handlers and middleware done.
if m.request.IsExited() || m.handlerIndex >= len(m.request.handlers) {
if m.request.Response.Status == 0 {
if m.request.Middleware.served {
m.request.Response.WriteHeader(http.StatusOK)
} else {
m.request.Response.WriteHeader(http.StatusNotFound)
}
}
}
}
func (m *middleware) callHandlerFunc(funcInfo handlerFuncInfo) {
niceCallFunc(func() {
if funcInfo.Func != nil {
funcInfo.Func(m.request)
} else {
var inputValues = []reflect.Value{
reflect.ValueOf(m.request.Context()),
}
if funcInfo.Type.NumIn() == 2 {
var inputObject reflect.Value
if funcInfo.Type.In(1).Kind() == reflect.Ptr {
inputObject = reflect.New(funcInfo.Type.In(1).Elem())
m.request.error = m.request.Parse(inputObject.Interface())
} else {
inputObject = reflect.New(funcInfo.Type.In(1).Elem()).Elem()
m.request.error = m.request.Parse(inputObject.Addr().Interface())
}
if m.request.error != nil {
return
}
inputValues = append(inputValues, inputObject)
}
// Call handler with dynamic created parameter values.
results := funcInfo.Value.Call(inputValues)
switch len(results) {
case 1:
if !results[0].IsNil() {
if err, ok := results[0].Interface().(error); ok {
m.request.error = err
}
}
case 2:
m.request.handlerResponse = results[0].Interface()
if !results[1].IsNil() {
if err, ok := results[1].Interface().(error); ok {
m.request.error = err
}
}
}
}
})
}