Skip to content

Instantly share code, notes, and snippets.

@rmanalan
Created August 4, 2009 19:09
Show Gist options
  • Save rmanalan/161461 to your computer and use it in GitHub Desktop.
Save rmanalan/161461 to your computer and use it in GitHub Desktop.
How to embed a Sinatra app inside a Rails app through a plugin (not Metal)
script/generate plugin HelloWorld
# vendor/plugins/hello_world/init.rb
Rails.configuration.gem "sinatra"
Rails.configuration.middleware.insert_before("ActionController::Failsafe", "HelloWorld")
# vendor/plugins/hello_world/lib/hello_world.rb
# your sinatra app goes here...
require 'sinatra/base'
class HelloWorld < Sinatra::Base
get "/hello" do
"Hello World!"
end
#this hackeration is required for sinatra to be a nice rack citizen
error 404 do
@app.call(env)
end
end
@cobbr2
Copy link

cobbr2 commented Aug 24, 2010

Under what circumstances and versions is the 'hackeration' actually necessary? In the version I'm running (Sinatra 1.0 based), if I use three middleware modules written this way, then 'halt 404, "foobar"' from my application, I'll get 2^3 - 1 (yes, 7) retries of the application URL handler. In addition, if I log something before the call on line 16, I never see that log message in any normal routing context.

@rmanalan
Copy link
Author

@cobbr2 I'm not entirely sure this is required anymore. This is old 2.3.x stuff... not sure if it's useful in rails 3 or even newer versions of rack and sinatra. I wrote this pre-sinatra 1.0

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