Google Groups Home
Help | Sign in
Bouncing from handlers into Rails with Thin?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Alex Payne  
View profile
 More options Feb 20, 5:17 pm
From: Alex Payne <a...@al3x.net>
Date: Wed, 20 Feb 2008 14:17:23 -0800 (PST)
Local: Wed, Feb 20 2008 5:17 pm
Subject: Bouncing from handlers into Rails with Thin?
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!


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adrian Madrid  
View profile
 More options Feb 20, 5:20 pm
From: "Adrian Madrid" <aemad...@gmail.com>
Date: Wed, 20 Feb 2008 15:20:27 -0700
Local: Wed, Feb 20 2008 5:20 pm
Subject: Re: Bouncing from handlers into Rails with Thin?

I think that follows more into Rack. You might want to look into the Cascade
module that tries the request on a set apps and returns the first hit for
inspiration.

AEM

--
Adrian Esteban Madrid
Lead Developer, Prefab Markets
http://www.prefabmarkets.com

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
macournoyer  
View profile
 More options Feb 20, 7:43 pm
From: macournoyer <macourno...@gmail.com>
Date: Wed, 20 Feb 2008 16:43:58 -0800 (PST)
Local: Wed, Feb 20 2008 7:43 pm
Subject: Re: Bouncing from handlers into Rails with Thin?
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:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google