Browse Source

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.
Carl Jackson 11 years ago
parent
commit
f255c52789
2 changed files with 8 additions and 8 deletions
  1. +5
    -6
      web/bytecode_runner.go
  2. +3
    -2
      web/router.go

+ 5
- 6
web/bytecode_runner.go View File

@ -20,13 +20,13 @@ func matchRoute(route route, m method, ms *method, r *http.Request, c *C) bool {
return false 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) m := httpMethod(r.Method)
var methods method var methods method
p := r.URL.Path p := r.URL.Path
if len(rm.sm) == 0 { if len(rm.sm) == 0 {
return methods, false
return methods, nil
} }
var i int 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 { if match && sm&smRoute != 0 {
si := rm.sm[i].i si := rm.sm[i].i
if matchRoute(rm.routes[si], m, &methods, r, c) { 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++ i++
} else if (match && sm&smJumpOnMatch != 0) || } else if (match && sm&smJumpOnMatch != 0) ||
(!match && sm&smJumpOnMatch == 0) { (!match && sm&smJumpOnMatch == 0) {
if sm&smFail != 0 { if sm&smFail != 0 {
return methods, false
return methods, nil
} }
i = int(rm.sm[i].i) i = int(rm.sm[i].i)
} else { } else {
@ -84,5 +83,5 @@ func (rm routeMachine) route(c *C, w http.ResponseWriter, r *http.Request) (meth
} }
} }
return methods, false
return methods, nil
} }

+ 3
- 2
web/router.go View File

@ -113,8 +113,9 @@ func (rt *router) route(c *C, w http.ResponseWriter, r *http.Request) {
rm = rt.compile() 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 return
} }


Loading…
Cancel
Save