Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Rack::Session::Pool with Sinatra

I'm exploring Sinatra and I want to use sessions but I don't want them to be stored in a Cookie, I found Rack::Session::Pool which works very well.

Now I want sessions to expire after a certain duration but I don't understand how to instanciate the Rack::Session::Pool and them use it in Sinatra.

Any Clue ?

like image 685
toofancy Avatar asked Jun 06 '09 17:06

toofancy


2 Answers

Sinatra is quite powerful, the trick from The Wicked Flea didn't work but this did :

use Rack::Session::Pool, :domain => 'example.com', :expire_after => 60 * 60 * 24 * 365

Thanks !

like image 122
toofancy Avatar answered Nov 14 '22 22:11

toofancy


In your rackup file:

%w(rubygems rack sinatra).each { |dependency| require dependency }
disable :run

require 'myapp'

sessioned = Rack::Session::Pool.new(
  Sinatra::Application,
  :domain       => 'example.com',
  :expire_after => 60 * 60 * 24 * 365 # expire after 1 year
)
run sessioned

To launch run rackup app.ru, or use Passenger, etc. This ought to wrap your application in the session pool and enable its functionality. I don't entirely know why it doesn't need use like most other middleware.

Understand that I haven't tested this at all, I haven't had something that needed session pools yet. I wrote this from the documentation for Rack::Session::Pool, which had an example at the top of the page. So, I can't tell you exactly how to use it either.

like image 42
Robert K Avatar answered Nov 15 '22 00:11

Robert K