hey Alex,
Glad to see you're interested in Thin!
That's very easy w/ Rack. You can build a wrapper around the Rails
adapter. Those wrappers are called Middlewares in Rack and Handlers
are called Adapter.
Here's a very simple example:
class MyAwesomeMiddleware
def initialize(rails_app)
@rails_app = rails_app
end
def call(env)
# Select the app to send the request to ...
app = if forward_to_rails?(env)
@rails_app
else
@some_other_app
end
status, headers, body = app.call(env)
# You can alter the status
# the response headers and the body.
body += 'crazy delicious footer!'
headers['HTTP_X_AWESOME'] = 'fo sho yo'
status, headers, body
end
def forward_to_rails?(env)
# You have access to all request headers like PATH_INFO through
then env Hash.
end
end
== In your Rack config file: config.ru
use MyAwesomeMiddleware, YourOtherApp.new
run Rack::Adapter::Rails.new(:root => '/path/to/your/
rails_app', :environment => 'production')
==
then start thin w/: thin start -r config.ru
Rack also comes with a couple authenticators, maybe that could be
useful to you:
protected_app = Rack::Auth::Basic.new(rails_app) do |username,
password|
'secret' == password
end
protected_app.realm = 'Awesome 2.0'
run protected_app
You can get more info in Rack doc http://rack.rubyforge.org/doc/
Or let me know if you need help w/ that.
On Feb 20, 5:17 pm, Alex Payne <a...@al3x.net> wrote:
> We're keeping our eyes on Thin over at Twitter, and I have a question
> about its capabilities (although this may be more in Rack's
> territory).
> I'd like to write a Thin handler that takes in a request, does some
> logic, and optionally forwards the request to Rails. Is this
> possible ? If it is, is possible for the handler to re-acquire the
> response after the request has been filled?
> An example use case: a request with HTTP Basic Authentication comes
> in. The Thin handler catches it and talks to the database to verify
> the credentials. If the requesting user isn't authorized, the Thin
> handler returns an 'Unauthorized' response and exits. If the
> requesting user is authorized, the request is forwarded to Rails to be
> fulfilled.
> Thanks for your thoughts!