This repository was archived by the owner on Jan 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
164 lines (146 loc) · 3.09 KB
/
handler.go
File metadata and controls
164 lines (146 loc) · 3.09 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package mint
import (
"net/http"
"github.com/gorilla/mux"
)
//HandlerFunc handles requests for an URL
type HandlerFunc func(*Context)
//Handlers chain of Handler
type Handlers []*HandlerContext
//HandlerContext #
type HandlerContext struct {
Mint *Mint
middleware []HandlerFunc
handlers []HandlerFunc
validator HandlerFunc
count int
methods []string
schemes []string
headers []string
queries []string
path string
name string
compressed bool
}
//HandlerBuilder new handerContext
func HandlerBuilder() *HandlerContext {
return new(HandlerContext)
}
func (hc *HandlerContext) build(router *mux.Router) {
if hc == nil {
return
}
hc.handlers = append(hc.middleware, hc.handlers...)
hc.count = len(hc.handlers)
route := router.Handle(hc.path, hc)
addFilters(hc, route)
}
func addFilters(hc *HandlerContext, route *mux.Route) {
if len(hc.methods) > 0 {
route.Methods(hc.methods...)
}
if len(hc.schemes) > 0 {
route.Schemes(hc.schemes...)
}
if len(hc.headers) > 0 {
route.Headers(hc.headers...)
}
if len(hc.queries) > 0 {
route.Queries(hc.queries...)
}
if len(hc.name) > 0 {
route.Name(hc.name)
}
}
func (hc *HandlerContext) buildWithRoute(route *mux.Route) {
if hc == nil {
return
}
hc.handlers = append(hc.middleware, hc.handlers...)
hc.count = len(hc.handlers)
route1 := route.Handler(hc)
addFilters(hc, route1)
}
//Methods #
func (hc *HandlerContext) Methods(methods ...string) *HandlerContext {
if hc == nil {
return hc
}
hc.methods = append(hc.methods, methods...)
return hc
}
//Handle #
func (hc *HandlerContext) Handle(handlers ...HandlerFunc) *HandlerContext {
if hc == nil {
return hc
}
hc.handlers = append(hc.handlers, handlers...)
return hc
}
//Schemes #
func (hc *HandlerContext) Schemes(schemes ...string) *HandlerContext {
if hc == nil {
return hc
}
hc.schemes = append(hc.schemes, schemes...)
return hc
}
//Headers #
func (hc *HandlerContext) Headers(headers ...string) *HandlerContext {
if hc == nil {
return hc
}
hc.headers = append(hc.headers, headers...)
return hc
}
//Queries #
func (hc *HandlerContext) Queries(queries ...string) *HandlerContext {
if hc == nil {
return hc
}
hc.queries = append(hc.queries, queries...)
return hc
}
//Path #
func (hc *HandlerContext) Path(path string) *HandlerContext {
if hc == nil {
return hc
}
hc.path = path
return hc
}
//Name #
func (hc *HandlerContext) Name(name string) *HandlerContext {
if hc == nil {
return hc
}
hc.name = name
return hc
}
//Compressed #
func (hc *HandlerContext) Compressed(isCompressed bool) *HandlerContext {
if hc == nil {
return hc
}
hc.compressed = isCompressed
return hc
}
//ServeHTTP #
func (hc *HandlerContext) ServeHTTP(w http.ResponseWriter, req *http.Request) {
c := hc.Mint.contextPool.Get().(*Context)
c.Reset()
c.HandlerContext = hc
c.params = mux.Vars(req)
c.Req = req
c.Res = w
c.Next()
hc.Mint.contextPool.Put(c)
}
//Use registers middleware
func (hc *HandlerContext) Use(handler ...HandlerFunc) *HandlerContext {
if hc == nil {
return hc
}
hc.middleware = append(hc.middleware, handler...)
return hc
}