-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandler.go
More file actions
141 lines (111 loc) · 3.48 KB
/
Copy pathhandler.go
File metadata and controls
141 lines (111 loc) · 3.48 KB
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
package uhttp
import (
"context"
"net/http"
"reflect"
"runtime"
"strings"
)
func NewHandler(opts ...HandlerOption) Handler {
mergedOpts := &handlerOptions{}
withDefaults().apply(mergedOpts)
for _, opt := range opts {
opt.apply(mergedOpts)
}
return Handler{opts: *mergedOpts}
}
type Handler struct {
opts handlerOptions
}
type HandlerFunc func(r *http.Request, returnCode *int) interface{}
type HandlerFuncWithModel func(r *http.Request, model interface{}, returnCode *int) interface{}
type PreProcessFunc func(ctx context.Context) error
func (h Handler) WsReady(u *UHTTP) Middleware {
c := chain(
parseModelMiddleware(u, h.opts, h.opts.postModel, h.opts.getModel, h.opts.deleteModel),
getParamsMiddleware(u, h.opts),
// Do not add logging here: a WS connection has more states which should be logged separately e.g. in the handler
)
// Add uhttp
c = chain(c, withUHTTP(u))
// Add original responseWriter
c = chain(c, withOriginalResponseWriter(u))
// Add contexts
for key, value := range u.requestContext {
c = chain(c, WithContextMiddleware(key, value))
}
// Add global middlewares
for key := range u.opts.globalMiddlewares {
c = chain(c, u.opts.globalMiddlewares[key])
}
// Add handler-specified middlewares
for key := range h.opts.middlewares {
c = chain(c, h.opts.middlewares[key])
}
// Add preProcess
return chain(c, preProcessMiddleware(u, h.opts.preProcess))
}
func (h Handler) HandlerFunc(u *UHTTP) http.HandlerFunc {
return h.handlerFuncExcludeMiddlewareByName(u, nil)
}
func (h Handler) handlerFuncExcludeMiddlewareByName(u *UHTTP, exclude *string) http.HandlerFunc {
// Outer middlewares
c := chain(
corsMiddleware(u),
jsonResponseMiddleware(u),
addLoggingMiddleware(u, &h, false),
)
// Add uhttp
c = chain(c, withUHTTP(u))
// Add original responseWriter
c = chain(c, withOriginalResponseWriter(u))
// Add contexts
for key, value := range u.requestContext {
c = chain(c, WithContextMiddleware(key, value))
}
// Add global middlewares
for key := range u.opts.globalMiddlewares {
f := u.opts.globalMiddlewares[key]
fName := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
if u.opts.logCustomMiddlewareRegistration {
u.opts.log.Infof("Registering custom-middleware for handler %s: %s", h.opts.handlerPattern, fName)
}
if exclude != nil {
if strings.Contains(fName, *exclude) {
continue
}
}
c = chain(c, u.opts.globalMiddlewares[key])
}
// Add parsers
c = chain(c, parseModelMiddleware(u, h.opts, h.opts.postModel, h.opts.getModel, h.opts.deleteModel))
c = chain(c, getParamsMiddleware(u, h.opts))
// Add handler-specified middlewares
for key := range h.opts.middlewares {
f := h.opts.middlewares[key]
// convenience feature: middleware can be nil (makes it easier to define handlers sometimes)
if f == nil {
continue
}
fName := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
if u.opts.logCustomMiddlewareRegistration {
u.opts.log.Infof("Registering custom-middleware for handler %s: %s", h.opts.handlerPattern, fName)
}
if exclude != nil {
if strings.Contains(fName, *exclude) {
continue
}
}
c = chain(c, h.opts.middlewares[key])
}
// Add preProcess
c = chain(c, preProcessMiddleware(u, h.opts.preProcess))
if h.opts.cacheEnable {
c = chain(c, cacheMiddleware(u, h))
}
// Timeouts
if h.opts.timeout != 0 {
return http.TimeoutHandler(c(selectMethodMiddleware(u, h.opts)), h.opts.timeout, h.opts.timeoutMessage).ServeHTTP
}
return c(selectMethodMiddleware(u, h.opts))
}