-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrouter.go
193 lines (152 loc) · 5.38 KB
/
router.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
package gongular
import (
"bytes"
"log"
"net/http"
"path"
"time"
"github.com/julienschmidt/httprouter"
)
// Router holds the required states and does the mapping of requests
type Router struct {
engine *Engine
prefix string
handlers []RequestHandler
}
// NewRouter creates a new gongular2 Router
func newRouter(e *Engine) *Router {
r := Router{
engine: e,
prefix: "",
handlers: make([]RequestHandler, 0),
}
return &r
}
// GET registers the given handlers at the path for a GET request
func (r *Router) GET(path string, handlers ...RequestHandler) {
r.combineAndWrapHandlers(path, http.MethodGet, handlers)
}
// POST registers the given handlers at the path for a POST request
func (r *Router) POST(path string, handlers ...RequestHandler) {
r.combineAndWrapHandlers(path, http.MethodPost, handlers)
}
// PUT registers the given handlers at the path for a PUT request
func (r *Router) PUT(path string, handlers ...RequestHandler) {
r.combineAndWrapHandlers(path, http.MethodPut, handlers)
}
// HEAD registers the given handlers at the path for a HEAD request
func (r *Router) HEAD(path string, handlers ...RequestHandler) {
r.combineAndWrapHandlers(path, http.MethodHead, handlers)
}
// DELETE registers the given handlers at the path for a DELETE request
func (r *Router) DELETE(path string, handlers ...RequestHandler) {
r.combineAndWrapHandlers(path, http.MethodDelete, handlers)
}
// PATCH registers the given handlers at the path for a PATCH request
func (r *Router) PATCH(path string, handlers ...RequestHandler) {
r.combineAndWrapHandlers(path, http.MethodPatch, handlers)
}
// OPTIONS registers the given handlers at the path for a OPTIONS request
func (r *Router) OPTIONS(path string, handlers ...RequestHandler) {
r.combineAndWrapHandlers(path, http.MethodOptions, handlers)
}
// TRACE registers the given handlers at the path for a TRACE request
func (r *Router) TRACE(path string, handlers ...RequestHandler) {
r.combineAndWrapHandlers(path, http.MethodTrace, handlers)
}
// CONNECT registers the given handlers at the path for a CONNECT request
func (r *Router) CONNECT(path string, handlers ...RequestHandler) {
r.combineAndWrapHandlers(path, http.MethodConnect, handlers)
}
// Method registers the given handlers at the path for the given method request
func (r *Router) Method(method, path string, handlers ...RequestHandler) {
r.combineAndWrapHandlers(path, method, handlers)
}
// Group groups a given path with additional interfaces. It is useful to avoid
// repetitions while defining many paths
func (r *Router) Group(_path string, handlers ...RequestHandler) *Router {
newRouter := &Router{
engine: r.engine,
prefix: path.Join(r.prefix, _path),
}
// Copy previous handlers references
newRouter.handlers = make([]RequestHandler, len(r.handlers))
copy(newRouter.handlers, r.handlers)
// Append new handlers
newRouter.handlers = append(newRouter.handlers, handlers...)
return newRouter
}
// subpath initiates a new route with path and handlers, useful for grouping
func (r *Router) subpath(_path string, handlers []RequestHandler) (string, []RequestHandler) {
combinedHandlers := r.handlers
combinedHandlers = append(combinedHandlers, handlers...)
resultingPath := path.Join(r.prefix, _path)
return resultingPath, combinedHandlers
}
func (r *Router) combineAndWrapHandlers(path, method string, handlers []RequestHandler) {
resultingPath, combinedHandlers := r.subpath(path, handlers)
fn := r.transformRequestHandlers(resultingPath, method, combinedHandlers)
r.engine.actualRouter.Handle(method, resultingPath, fn)
}
func (r *Router) transformRequestHandlers(path string, method string, handlers []RequestHandler) httprouter.Handle {
middleHandlers := make([]*handlerContext, len(handlers))
for i, handler := range handlers {
mh, err := transformRequestHandler(path, method, r.engine.injector, handler)
if err != nil {
log.Fatal(err)
}
middleHandlers[i] = mh
}
fn := func(wr http.ResponseWriter, req *http.Request, ps httprouter.Params) {
st := time.Now()
routeStat := RouteStat{
Request: req,
MatchedPath: path,
Handlers: make([]HandlerStat, len(middleHandlers)),
}
// Create a logger for each request so that we can group the output
buf := new(bytes.Buffer)
logger := log.New(buf, "", log.LstdFlags)
// Create a context that wraps the request, writer and logger
ctx := contextFromRequest(path, wr, req, ps, logger)
// For each of the handler this route has, try to execute it
for idx, handler := range middleHandlers {
hc := HandlerStat{
FuncName: handler.name,
}
// Parse the parameters to the handler object
stHandler := time.Now()
fn := handler.RequestHandler
err := fn(ctx)
hc.Duration = time.Since(stHandler)
// If an error occurs, stop the chain
if err != nil {
ctx.StopChain()
r.engine.errorHandler(err, ctx)
// Put the route stats
hc.Error = err
hc.StopChain = true
routeStat.Handlers[idx] = hc
break
}
// Voluntarily stopped
if ctx.stopChain {
// Put the route stats
hc.Duration = time.Since(st)
hc.StopChain = true
routeStat.Handlers[idx] = hc
break
}
routeStat.Handlers[idx] = hc
}
// Save final stats
routeStat.ResponseSize = ctx.Finalize()
routeStat.ResponseCode = ctx.status
routeStat.TotalDuration = time.Since(st)
routeStat.Logs = buf
if r.engine.callback != nil {
r.engine.callback(routeStat)
}
}
return fn
}