Sinatra session not preserved with Rack::FiberPool
The session is not preserved between requests, though I can't see what I'm 开发者_Python百科doing wrong. Code!
require 'sinatra'
require 'rack/fiber_pool'
class SessionTest < Sinatra::Base
use Rack::FiberPool
enable :sessions
set :session_secret, "foobar"
get '/' do
body { session.inspect } #This is always '{}'!
end
get '/a' do
session['user'] = "bob"
redirect '/'
end
end
run SessionTest.new
Try this instead:
require 'sinatra'
require 'rack/fiber_pool'
class SessionTest < Sinatra::Base
enable :sessions
set :session_secret, "foobar"
get '/' do
body { session.inspect } #This is always '{}'!
end
get '/a' do
session['user'] = "bob"
redirect '/'
end
end
use Rack::FiberPool
run SessionTest.new
Otherwise Sinatra will set up the fiber pool after the session middleware, which doesn't work. This is not a bug but caused by the way Rack::FiberPool
works.
Turns out replacing enable :sessions
with use Rack::Session::Cookie
is enough to make it work.
But why!?
精彩评论