Skip to content

Commit

Permalink
Change the router interface slightly
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zenazn committed Oct 27, 2014
1 parent b12ae7f commit f255c52
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
11 changes: 5 additions & 6 deletions web/bytecode_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,21 +68,20 @@ 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 {
i++
}
}

return methods, false
return methods, nil
}
5 changes: 3 additions & 2 deletions web/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit f255c52

Please sign in to comment.