How do I access the Rack environment from within Rails?

I’m pretty sure you can use the Rack::Request object for passing request-scope variables: # middleware: def call(env) request = Rack::Request.new(env) # no matter how many times you do ‘new’ you always get the same object request[:foo] = ‘bar’ @app.call(env) end # Controller: def index if params[:foo] == ‘bar’ … end end Alternatively, you can get … Read more

How to serve static files via Rack?

To redirect every request to a particular path, use Rack::File (for some reason this class is absent in recent documentation, but it is still part of the latest Rack): run Rack::File.new(“/my/path”) To redirect every request, and add an HTML index of all files in the target dir, use Rack::Directory: run Rack::Directory.new(“/my/path”) To combine several directories … Read more

Where do you put your Rack middleware files and requires?

As of Rails 3.2, Rack middleware belongs in the app/middleware directory. It works “out-of-the-box” without any explicit require statements. Quick example: I’m using a middleware class called CanonicalHost which is implemented in app/middleware/canonical_host.rb. I’ve added the following line to production.rb (note that the middleware class is explicitly given, rather than as a quoted string, which … Read more

Logging in Sinatra?

Sinatra 1.3 will ship with such a logger object, exactly usable as above. You can use edge Sinatra as described in “The Bleeding Edge“. Won’t be that long until we’ll release 1.3, I guess. To use it with Sinatra 1.2, do something like this: require ‘sinatra’ use Rack::Logger helpers do def logger request.logger end end

Foreman: Use different Procfile in development and production

You could use two Procfiles (e.g. Procfile and Procfile.dev) and use foremans -f option to select a different one to use in dev: In dev (Procfile.dev contains your shotgun web process): foreman start -f Procfile.dev In production, foreman start will pick up the normal Procfile. Alternatively you could create a bin directory in your app … Read more