Created
February 21, 2012 10:37
-
-
Save hassox/1875707 to your computer and use it in GitHub Desktop.
Mounted sinatra apps with http_router_sinatra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './base_app' | |
run BaseApp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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