Skip to content

Instantly share code, notes, and snippets.

@hassox
Created February 21, 2012 10:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hassox/1875707 to your computer and use it in GitHub Desktop.
Save hassox/1875707 to your computer and use it in GitHub Desktop.
Mounted sinatra apps with http_router_sinatra
require 'sinatra/base'
require 'http_router_sinatra'
# With this simplistic approach, Sinatra apps must be leaf nodes in the rack graph
# Mounter nodes may be nested arbitrarily deeply however.
# Include Warden to authenticate whole sub-branches of the graph
class Mounter < HttpRouter
def mount(path, app)
route = add(path).partial.to(app)
mounter = app.respond_to?(:url_mount) ? app : nil
mounter ||= app.respond_to?(:router) && app.router.respond_to?(:url_mount) ? app.router : nil
if mounter
mount = UrlMount.new(route.path, route.default_values)
mount.url_mount = url_mount if url_mount
mounter.url_mount = mount
end
route
end
end
class MountOne < Sinatra::Base
register HttpRouter::Sinatra::Extension
get "/foo", :name => :foo do
out =<<-TEXT
Mount One Foo
paths:
one foo: #{MountOne.generate(:foo)}
one foo: #{MountOne.generate(:bar)}
two foo: #{MountTwo.generate(:foo)}
two bar: #{MountTwo.generate(:bar)}
three foo: #{MountThree.generate(:foo)}
three bar: #{MountThree.generate(:bar)}
TEXT
end
get "/bar", :name => :bar do
out =<<-TEXT
Mount One Bar
paths:
one foo: #{MountOne.generate(:foo)}
one foo: #{MountOne.generate(:bar)}
two foo: #{MountTwo.generate(:foo)}
two bar: #{MountTwo.generate(:bar)}
three foo: #{MountThree.generate(:foo)}
three bar: #{MountThree.generate(:bar)}
TEXT
end
end
class MountTwo < Sinatra::Base
register HttpRouter::Sinatra::Extension
get "/foo", :name => :foo do
out =<<-TEXT
Mount Two Foo
paths:
one foo: #{MountOne.generate(:foo)}
one foo: #{MountOne.generate(:bar)}
two foo: #{MountTwo.generate(:foo)}
two bar: #{MountTwo.generate(:bar)}
three foo: #{MountThree.generate(:foo)}
three bar: #{MountThree.generate(:bar)}
TEXT
end
get "/bar", :name => :bar do
out =<<-TEXT
Mount Two Bar
paths:
one foo: #{MountOne.generate(:foo)}
one foo: #{MountOne.generate(:bar)}
two foo: #{MountTwo.generate(:foo)}
two bar: #{MountTwo.generate(:bar)}
three foo: #{MountThree.generate(:foo)}
three bar: #{MountThree.generate(:bar)}
TEXT
end
end
class MountThree < Sinatra::Base
register HttpRouter::Sinatra::Extension
get "/foo", :name => :foo do
out =<<-TEXT
Mount Three Foo
paths:
one foo: #{MountOne.generate(:foo)}
one foo: #{MountOne.generate(:bar)}
two foo: #{MountTwo.generate(:foo)}
two bar: #{MountTwo.generate(:bar)}
three foo: #{MountThree.generate(:foo)}
three bar: #{MountThree.generate(:bar)}
TEXT
end
get "/bar", :name => :bar do
out =<<-TEXT
Mount Three Bar
paths:
one foo: #{MountOne.generate(:foo)}
one foo: #{MountOne.generate(:bar)}
two foo: #{MountTwo.generate(:foo)}
two bar: #{MountTwo.generate(:bar)}
three foo: #{MountThree.generate(:foo)}
three bar: #{MountThree.generate(:bar)}
TEXT
end
end
BaseApp = Mounter.new
MountedApp = Mounter.new
BaseApp.mount("/one" , MountOne)
BaseApp.mount("/two" , MountTwo)
BaseApp.mount("/mounted", MountedApp)
MountedApp.mount("/three", MountThree)
require './base_app'
run BaseApp
$ curl localhost:9393/mounted/three/bar
Mount Three Bar
paths:
one foo: /one/foo
one foo: /one/bar
two foo: /two/foo
two bar: /two/bar
three foo: /mounted/three/foo
three bar: /mounted/three/bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment