A very simple Rack-mount example with multiple Rack apps

Language: Ruby

# To run this example:
#   1. put this code in a config.ru file
#   2. run "shotgun" on the command line (http://github.com/rtomayko/shotgun)
#   3. go to http://localhost:9393/

require 'rack/mount'

class MoonApp
  def self.call(env)
    case env['rack.routing_args'][:action]
    when 'moon'
      html = %(Moon | <a href="#{Routes.url(:noir)}">Noir</a> | <a href="#{Routes.url(:boosh)}">Boosh</a><h1>Howard Moon's Homepage</h1>)
      [200, {'Content-Type' => 'text/html'}, [html]]
    when 'boosh'
      env['moon.message'] = "Hello from Moon"
      # Rack-mount will look for other matches when 417 is returned
      [417, {}, []]
    end
  end
end

class NoirApp
  def self.call(env)
    html = case env['rack.routing_args'][:action]
    when 'noir'
      %(<a href="#{Routes.url(:moon)}">Moon</a> | Noir | <a href="#{Routes.url(:boosh)}">Boosh</a><h1>Vince Noir's Homepage</h1>)
    when 'boosh'
      %(<a href="#{Routes.url(:moon)}">Moon</a> | <a href="#{Routes.url(:noir)}">Noir</a> | Boosh<h1>Boosh Homepage</h1><p>#{env['moon.message']}</p>)
    end
    [200, {'Content-Type' => 'text/html'}, [html]]
  end
end

Routes = Rack::Mount::RouteSet.new do |set|
  set.add_route MoonApp, { :path_info => '/moon' }, { :action => 'moon' }, :moon
  set.add_route NoirApp, { :path_info => '/noir' }, { :action => 'noir' }, :noir
  set.add_route MoonApp, { :path_info => '/' }, { :action => 'boosh' }, :boosh
  set.add_route NoirApp, { :path_info => '/' }, { :action => 'boosh' }
end

run Routes
Reveal More
Added over 2 years ago by Devo_normal gbuesing