Saturday, August 28, 2010

Getting Rails and Sinatra to Share Sessions - Part I

Introduction

This is the first part of a series of blog posts that will show you how to setup your machine so that you can share sessions between Rails and Sinatra apps.

This idea was inspired by the In a World of Middleware, Who Needs Monolithic Applications? Mountain West Ruby Conference 2009 talk given by Corey Donohoe of Engine Yard.

However I will not be using Hancock or Hancock Client for Single Sign On. I think it is too complicated. I want something simple. My motivation is to build Rack compliant apps that are very focused on doing one thing really well and combining them to create a mashup.

At the end of the talk Corey said that in a world of why do we even need Rails? My opinion is that Rails app must be the last app in your Rack Stack and must be focused on your Core Domain. Anything else must be built as a Rack app. Because it is independent of your business and can be re-used across all your web apps.

Installation and Configuration

Environment: Ruby 1.8.7 managed by RVM

1)
gem install passenger

2)
passenger-install-apache2-module

Note: Steps 1 and 2 does not use sudo

3)
sudo mkdir /usr/local/apache2/conf

Add the lines shown during installation to httpd.conf file and save it in the directory created above.

In my case it is:

LoadModule passenger_module /Users/bparanj/.rvm/gems/ruby-1.8.7-p299/gems/passenger-2.2.15/ext/apache2/mod_passenger.so
PassengerRoot /Users/bparanj/.rvm/gems/ruby-1.8.7-p299/gems/passenger-2.2.15
PassengerRuby /Users/bparanj/.rvm/rubies/ruby-1.8.7-p299/bin/ruby

4) Setup folder to hold vhosts

sudo mkdir /usr/local/apache2/conf/vhosts

5) Add the following to the httpd.conf

NameVirtualHost *
Include /usr/local/apache2/conf/vhosts/*

6) Add virtual hosts to a file virtual_hosts.conf under /usr/local/apache2/conf/vhosts directory

# Example App

ServerName app.test
DocumentRoot /Users/benr/Rails/app/public
RailsEnv development



# Example App 2

ServerName app2.test
DocumentRoot /Users/benr/Rails/app2/public
RailsEnv development


7) Edit /etc/hosts file

sudo vi /etc/hosts

and add:

127.0.0.1 app.test app2.test

8)
sudo apachectl graceful or

sudo apachectl start

9) To restart app, create a file under RAILS_ROOT/tmp/restart.txt

when you run touch on restart.txt your Rails app will be reloaded

References:

http://benr75.com/2008/04/12/setup-mod_rails-phusion-mac-os-x-leopard
http://www.modrails.com/videos/passenger.mov