- A router is similar to a Rack middleware.
- The main difference is
that it doesn’t wrap a single Rack endpoint, but keeps a list of
endpoints, just like Rack::Cascade does.
- Depending on some criteria,
usually the requested path, the router will then decide what endpoint
to hand the request to.
- Most routers differ in the way they decide which endpoint to hand
the request to.
- All routers meant for general usage do offer routing
based on the path, but how complex their path matching might be
varies.
- While Rack::URLMap only matches prefixes, most other routers
allow simple wildcard matching.
- Both Rack::Mount, which is used by
Rails, and Sinatra allow arbitrary matching logic.
- However, such flexibility comes at a price: Rack::Mount and Sinatra
have a routing complexity of O(n), meaning that in the worst-case
scenario an incoming request has to be matched against all the
defined routes.
- Rack::Mount is known to produce fast routing, however its API is
not meant to be used directly but rather by other libraries, like
the Rails routes DSL.
[~/local/src/ruby/sinatra/rack/rack-mount]$ cat config.ru
require 'sinatra/base'
require 'rack/mount'
class Foo < Sinatra::Base
get('/foo') { 'foo' }
get('/fou') { 'fou' }
end
class Bar < Sinatra::Base
get('/bar') { 'bar' }
get('/ba') { 'ba' }
end
Routes = Rack::Mount::RouteSet.new do |set|
set.add_route Foo, :path_info => %r{^/fo[ou]$}
set.add_route Bar, :path_info => %r{^/bar?$}
end
run Routes
Casiano Rodriguez León
2015-01-07