class Padrino::Router
run routes
end
map(:path => “/”, :to => PadrinoWeb, :host => /*.padrino.local/)
routes = Padrino::Router.new do
run routes
end
map(:path => “/”, :to => Admin, :host => “admin.padrino.local”)
map(:path => “/”, :to => PadrinoWeb, :host => “padrino.local”)
routes = Padrino::Router.new do
==== Examples
:host- Map the app to the given host
:path - Map the app to the given path
:to -
The class of application that you want mount
==== Options
* Use hosts instead of server name for mappings (this help us with our vhost and doman aliases)
* Ignore server names (this solve issues with vhost and domain aliases)
* Map a path to the specified App
Features:
longest paths are tried first, since they are most specific.
Padrino::Router like Rack::URLMap dispatches in such a way that the
This class is an extended version of Rack::URLMap
#
- Map the app to the given path
- Map the app to the given host
def call(env)
def call(env) rPath = env["PATH_INFO"].to_s script_name = env['SCRIPT_NAME'] hHost, sName, sPort = env.values_at('HTTP_HOST','SERVER_NAME','SERVER_PORT') @mapping.each do |host, path, match, app| next unless host.nil? || hHost =~ host next unless rPath =~ match && rest = $1 next unless rest.empty? || rest[0] == ?/ rest = "/" if rest.empty? return app.call( env.merge( 'SCRIPT_NAME' => (script_name + path), 'PATH_INFO' => rest)) end [404, {"Content-Type" => "text/plain", "X-Cascade" => "pass"}, ["Not Found: #{rPath}"]] end
def initialize(*mapping, &block)
def initialize(*mapping, &block) @mapping = [] mapping.each { |m| map(m) } instance_eval(&block) if block end
def map(options={})
def map(options={}) path = options[:path] || "/" host = options[:host] app = options[:to] raise ArgumentError, "paths need to start with /" if path[0] != ?/ raise ArgumentError, "app is required" if app.nil? path = path.chomp('/') match = Regexp.new("^#{Regexp.quote(path).gsub('/', '/+')}(.*)", nil, 'n') host = Regexp.new("^#{Regexp.quote(host)}$", true, 'n') unless host.nil? || host.is_a?(Regexp) @mapping << [host, path, match, app] sort! end
def sort!
def sort! @mapping = @mapping.sort_by { |h, p, m, a| -p.size } end