How do you configure WEBrick to use SSL in Rails?

While the scripts directory in Rails 4 is gone, the bin directory remains. You can get WEBrick working with an SSL certificate by editing the bin/rails script. Tested on Rails 4 and Ruby 2.1.1, installed with rbenv. Much of this is from this blog post and this Stack Overflow question. #!/usr/bin/env ruby require ‘rails/commands/server’ require … Read more

How to read POST data in rack request

From reading the docs for POST, looks like it is giving you parsed data based on other content types. If you want to process “application/json”, you probably need to JSON.parse( req.body.read ) instead. To check this, try puts req.body.read where you currently have puts req.POST. req.body is an I/O object, not a string. See the … Read more

Get ordered list of middleware in a generic rack application?

$ rake middleware use ActionDispatch::Static use Rack::Lock use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007ffd148f9468> use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::Migration::CheckPending use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use Rack::Head use Rack::ConditionalGet use Rack::ETag run RackTest::Application.routes http://pothibo.com/2013/11/ruby-on-rails-inside-actiondispatch-and-rack/

Rack concurrency – rack.multithread, async.callback, or both?

Note: I use Thin as synonym for all web servers implementing the async Rack extension (i.e. Rainbows!, Ebb, future versions of Puma, …) Q1. Correct. It will wrap the response generation (aka call) in EventMachine.defer { … }, which will cause EventMachine to push it onto its built-in thread pool. Q2. Using async.callback in conjunction … Read more

Rack::Request – how do I get all headers?

The HTTP headers are available in the Rack environment passed to your app: HTTP_ Variables: Variables corresponding to the client-supplied HTTP request headers (i.e., variables whose names begin with HTTP_). The presence or absence of these variables should correspond with the presence or absence of the appropriate HTTP header in the request. So the HTTP … Read more

How do I redirect without www using Rails 3 / Rack?

In Ruby on Rails 4, removing www. from any URL whilst maintaining the pathname can be achieved simply by using: # config/routes.rb constraints subdomain: ‘www’ do get ‘:any’, to: redirect(subdomain: nil, path: ‘/%{any}’), any: /.*/ end In contrast, adding www. to the beginning of any URL that doesn’t already have it can be achieved by: … Read more

Any success with Sinatra working together with EventMachine WebSockets?

Did not try it, but should not be too hard: require ’em-websocket’ require ‘sinatra/base’ require ‘thin’ EM.run do class App < Sinatra::Base # Sinatra code here end EM::WebSocket.start(:host => ‘0.0.0.0’, :port => 3001) do # Websocket code here end # You could also use Rainbows! instead of Thin. # Any EM based Rack handler should … Read more

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)