|
91 | 91 | // Router is a http.Handler which can be used to dispatch requests to different
|
92 | 92 | // handler functions via configurable routes
|
93 | 93 | type Router struct {
|
94 |
| - parent *Router |
95 |
| - beginPath string |
96 |
| - trees map[string]*node |
| 94 | + parent *Router |
| 95 | + beginPath string |
| 96 | + trees map[string]*node |
| 97 | + registeredPaths map[string][]string |
97 | 98 |
|
98 | 99 | // Enables automatic redirection if the current route can't be matched but a
|
99 | 100 | // handler for the path with (without) the trailing slash exists.
|
@@ -149,6 +150,8 @@ type Router struct {
|
149 | 150 | func New() *Router {
|
150 | 151 | return &Router{
|
151 | 152 | beginPath: "/",
|
| 153 | + trees: make(map[string]*node), |
| 154 | + registeredPaths: make(map[string][]string), |
152 | 155 | RedirectTrailingSlash: true,
|
153 | 156 | RedirectFixedPath: true,
|
154 | 157 | HandleMethodNotAllowed: true,
|
@@ -223,16 +226,14 @@ func (r *Router) Handle(method, path string, handle fasthttp.RequestHandler) {
|
223 | 226 | return
|
224 | 227 | }
|
225 | 228 |
|
226 |
| - if r.trees == nil { |
227 |
| - r.trees = make(map[string]*node) |
228 |
| - } |
229 |
| - |
230 | 229 | root := r.trees[method]
|
231 | 230 | if root == nil {
|
232 | 231 | root = new(node)
|
233 | 232 | r.trees[method] = root
|
234 | 233 | }
|
235 | 234 |
|
| 235 | + r.registeredPaths[method] = append(r.registeredPaths[method], path) |
| 236 | + |
236 | 237 | optionalPaths := getOptionalPaths(path)
|
237 | 238 |
|
238 | 239 | // if not has optional paths, adds the original
|
@@ -310,18 +311,6 @@ func (r *Router) ServeFilesCustom(path string, fs *fasthttp.FS) {
|
310 | 311 | })
|
311 | 312 | }
|
312 | 313 |
|
313 |
| -// Lookup allows the manual lookup of a method + path combo. |
314 |
| -// This is e.g. useful to build a framework around this router. |
315 |
| -// If the path was found, it returns the handle function and the path parameter |
316 |
| -// values. Otherwise the third return value indicates whether a redirection to |
317 |
| -// the same path with an extra / without the trailing slash should be performed. |
318 |
| -func (r *Router) Lookup(method, path string, ctx *fasthttp.RequestCtx) (fasthttp.RequestHandler, bool) { |
319 |
| - if root := r.trees[method]; root != nil { |
320 |
| - return root.getValue(path, ctx) |
321 |
| - } |
322 |
| - return nil, false |
323 |
| -} |
324 |
| - |
325 | 314 | // Handler makes the router implement the fasthttp.ListenAndServe interface.
|
326 | 315 | func (r *Router) Handler(ctx *fasthttp.RequestCtx) {
|
327 | 316 | if r.PanicHandler != nil {
|
@@ -418,6 +407,23 @@ func (r *Router) Handler(ctx *fasthttp.RequestCtx) {
|
418 | 407 | }
|
419 | 408 | }
|
420 | 409 |
|
| 410 | +// Lookup allows the manual lookup of a method + path combo. |
| 411 | +// This is e.g. useful to build a framework around this router. |
| 412 | +// If the path was found, it returns the handle function and the path parameter |
| 413 | +// values. Otherwise the third return value indicates whether a redirection to |
| 414 | +// the same path with an extra / without the trailing slash should be performed. |
| 415 | +func (r *Router) Lookup(method, path string, ctx *fasthttp.RequestCtx) (fasthttp.RequestHandler, bool) { |
| 416 | + if root := r.trees[method]; root != nil { |
| 417 | + return root.getValue(path, ctx) |
| 418 | + } |
| 419 | + return nil, false |
| 420 | +} |
| 421 | + |
| 422 | +// List returns all registered routes grouped by method |
| 423 | +func (r *Router) List() map[string][]string { |
| 424 | + return r.registeredPaths |
| 425 | +} |
| 426 | + |
421 | 427 | func (r *Router) allowed(path, reqMethod string) (allow string) {
|
422 | 428 | if path == "*" || path == "/*" { // server-wide
|
423 | 429 | for method := range r.trees {
|
|
0 commit comments