5

I am trying to split my current Ruby on Rails 3 web-application and it's web-services (API). My web-application is running on Heroku and implements API as a namespaced route within my application. For example /events returns a HTML page and /api/v1/events returns a JSON data.

According to some best practices, I want to split those into two different applications. I have chosen Sinatra to implement the API application. It works now for simple requests where authentication is not required.

My Ruby on Rails 3 application is using Devise to authenticate users. There's also ability to login with Facebook account. Now what I want to achieve, is HTTP Basic Authentication of users (including registration) through my Sinatra-based API by using Warden.

What is the best way to do that? Or maybe I can use something different then Warden?

Keep in mind that I am not very familiar with Rack :)

6
  • My personal preference is to subclass ActionController::Metal apidock.com/rails/ActionController/Metal. Chances are that you'll have a faster and more consistent app using this approach, rather than mixing frameworks. Sinatra has IMHO no benefits whatsoever in your scenario.
    – Roman
    Apr 22, 2011 at 16:00
  • @Roman which framework if any would you recommend? I think Rails is to heavy for this kind of work.
    – Ivan
    Apr 23, 2011 at 22:50
  • As I said - metal is often overlooked rails' feature, that gives very good performance with a minimal overhead. Basically it's a rack module which can leverage the rails' router.
    – Roman
    Apr 24, 2011 at 15:13
  • 2
    Also not an answer to your question, but you might want to look at Grape (github.com/intridea/grape), a Sinatra-based framework specifically for creating apis. Apr 29, 2011 at 0:15
  • Thanks @amiel-martin This is actually what I've also been looking for!
    – Ivan
    Apr 29, 2011 at 8:21

1 Answer 1

10

I was able to get it working. There were a few main aspects:

  • Get Devise working with Rails (Devise is a Rails app, won't work without it)
  • Setup the mapping (route) on Rack level to support both Rails and Sinatra
  • Share the sessions between Rails and Sinatra
  • Setup Warden and make it available to Sinatra

Here is most relevant part of code from /config.ru:

    #

    # ...

    # Rest with Rails
    map "/" do
      run MyApp::Application
    end

    # Anything urls starting with /slim will go to Sinatra
    map "/slim" do

      # make sure :key and :secret be in-sync with initializers/secret_store.rb initializers/secret_token.rb
      use Rack::Session::Cookie, :key => '<< see, initializers/secret_store.rb >>', :secret => '<< copy from initializers/secret_token.rb >>'

      # Point Warden to the Sinatra App
      use Warden::Manager do |manager|
        manager.failure_app = AppMain
        manager.default_scope = Devise.default_scope
      end

      # Borrowed from https://gist.github.com/217362
      Warden::Manager.before_failure do |env, opts|
        env['REQUEST_METHOD'] = "POST"
      end

      run AppMain
    end

See, http://labnote.beedesk.com/sinatra-warden-rails-devise for a complete solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.