From f255c52789a1c4f255b5533eead00766369c1611 Mon Sep 17 00:00:00 2001 From: Carl Jackson Date: Sun, 26 Oct 2014 17:44:00 -0700 Subject: [PATCH] Change the router interface slightly Make the bytecode runner return the route that we're going to use. It's up to the router itself to dispatch to that route. Besides feeling a teensy bit cleaner, this refactoring is to prepare for a "Router" middleware, which will allow application developers to control when in the middleware stack routing occurs. --- web/bytecode_runner.go | 11 +++++------ web/router.go | 5 +++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/web/bytecode_runner.go b/web/bytecode_runner.go index 9758401..706490a 100644 --- a/web/bytecode_runner.go +++ b/web/bytecode_runner.go @@ -20,13 +20,13 @@ func matchRoute(route route, m method, ms *method, r *http.Request, c *C) bool { return false } -func (rm routeMachine) route(c *C, w http.ResponseWriter, r *http.Request) (method, bool) { +func (rm routeMachine) route(c *C, w http.ResponseWriter, r *http.Request) (method, *route) { m := httpMethod(r.Method) var methods method p := r.URL.Path if len(rm.sm) == 0 { - return methods, false + return methods, nil } var i int @@ -68,15 +68,14 @@ func (rm routeMachine) route(c *C, w http.ResponseWriter, r *http.Request) (meth if match && sm&smRoute != 0 { si := rm.sm[i].i if matchRoute(rm.routes[si], m, &methods, r, c) { - rm.routes[si].handler.ServeHTTPC(*c, w, r) - return 0, true + return 0, &rm.routes[si] } i++ } else if (match && sm&smJumpOnMatch != 0) || (!match && sm&smJumpOnMatch == 0) { if sm&smFail != 0 { - return methods, false + return methods, nil } i = int(rm.sm[i].i) } else { @@ -84,5 +83,5 @@ func (rm routeMachine) route(c *C, w http.ResponseWriter, r *http.Request) (meth } } - return methods, false + return methods, nil } diff --git a/web/router.go b/web/router.go index c0f03e5..632d24e 100644 --- a/web/router.go +++ b/web/router.go @@ -113,8 +113,9 @@ func (rt *router) route(c *C, w http.ResponseWriter, r *http.Request) { rm = rt.compile() } - methods, ok := rm.route(c, w, r) - if ok { + methods, route := rm.route(c, w, r) + if route != nil { + route.handler.ServeHTTPC(*c, w, r) return }