Skip to content

Instantly share code, notes, and snippets.

@jamiehodge
Created June 20, 2012 11:34
Show Gist options
  • Save jamiehodge/2959463 to your computer and use it in GitHub Desktop.
Save jamiehodge/2959463 to your computer and use it in GitHub Desktop.
sinatra/mount
require 'sinatra/base'
module Sinatra
module Mount
def mount(app, route="/#{app.name.downcase}")
before "#{route}*" do
halt app.call(
env.merge(
'SCRIPT_NAME' => route.split('/').last,
'PATH_INFO' => params.delete('splat').join('/')
)
)
end
end
end
register Mount
end
class Bar < Sinatra::Base
get '/' do
'bar'
end
get %r{^/(?<bar>[^/]+)$} do
"bar: #{params[:bar]}"
end
end
class Foo < Sinatra::Base
register Sinatra::Mount
get '/' do
'foo'
end
p mount Bar, '/:foo/bars'
get %r{^/(?<foo>[^/]+)$} do
"foo: #{params[:foo]}"
end
end
Foo.run!
@merqlove
Copy link

Also usable implementation of mount.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment